Search code examples
vue.jshttp-redirectnuxt.js

Difference between redirect() and req.writehead() in Nuxt.js


In Nuxt.js SSR what is the difference between redirect(301, url) and req.writehead(301, { Location: url }) when using them inside async asyncData? As far as I can se, I get the same result with both of them when I use the following code in /pages/folder/_.vue.

export default {
  async asyncData({ redirect, req }) {
    redirect(301, url)
    // VS
    req.writehead(301, { location: url })
  }
}

Solution

  • redirect is mainly a helper available on Nuxt's context. I didn't checked the source code but it is totally doable that it's doing the same stuff under the hood with the benefit of being easier to use/access.

    There are several keys like this one in Nuxt context (like params): https://nuxtjs.org/docs/concepts/context-helpers