Redirect to Page 404



  • Hi.
    I need to redirect the user to page 404.vue after a query in ssr mode.

    preFetch ({ store, currentRoute, redirect }) {
        const p1 = store.dispatch('main/fetchCategoria', currentRoute.params.slugCategoria)
        p1.then((response) => {
          // Empleos por categoria
          const params = `categoria__slug=${currentRoute.params.slugCategoria}`
          return store.dispatch('main/fetchEmpleosCategoria', params)
        }).catch((e) => {
          if (e.response.status === 404) {
            redirect({ path: '404/' }) // this does not redirect
          }
          if (e.response.status === 500) {
            redirect({ name: 'error500' })
          }
        })
      },
    

    any suggestion?
    Thanks



  • @jhon said in Redirect to Page 404:

    if (e.response.status === 404) {
    redirect({ path: ‘404/’ }) // this does not redirect
    }

    could it be as simple as this:

     if (e.response.status === 404) {
            redirect({ path: '/404' }) //  /404 instead of 404/
          }
    


  • @jhon As dobbel said you might have to change your path, but also with preFetch, if you want quasar to wait for any asynchronous call, you need to either return a promise or use async preFetch. Since you have two store dispatches, using async is probably easier:

      async preFetch ({ store, currentRoute, redirect }) {
        try {
          await store.dispatch('main/fetchCategoria', currentRoute.params.slugCategoria)
          // Empleos por categoria
          const params = `categoria__slug=${currentRoute.params.slugCategoria}`
          await store.dispatch('main/fetchEmpleosCategoria', params)
        } catch(e) {
          // console.log(e) here, as e.response.status may not longer be correct
          if (e.response.status === 404) {
            redirect({ path: '404/' }) // this does not redirect
          }
          if (e.response.status === 500) {
            redirect({ name: 'error500' })
          }
        }
      },
    
    


  • @beets Excelent…
    Muchas gracias…


Log in to reply