DEV Community

sium_hossain
sium_hossain

Posted on

4 3

#Short_note- Redirect and catch errors in fetch hook Nuxt (SSR)

This is just a short note about how to redirect after catching error in fetch hook in server side rendering- Nuxt js application.

async fetch() {

    try{
        // whatever you need 

        const route = this.$router.history.current.path;
        this.routeName = route   
       const finalTitle = route.replace(/\\|\//g, "");
       const res = await 
       this.$axios.$get(`productBySlug/${finalTitle}/`);
       this.obj = res.data;

   }
   catch({response}){
       // others code goes here 
       this.$nuxt.context.redirect({path:'/'})


   }

},
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more