1

An error occurred when deploying a development using Laravel on the back end and Nuxtjs on the front end. I was able to develop without problems when developing in the local environment, but an error occurred during deployment.

netlify deploy log

6:23:49 PM:   1. Build command from Netlify app                             
6:23:49 PM: ────────────────────────────────────────────────────────────────
6:23:49 PM: ​
6:23:49 PM: $ npm run generate
6:23:49 PM: > xxx_frontend@1.0.0 generate /opt/build/repo
6:23:49 PM: > nuxt generate
6:23:50 PM: [warn] When using `nuxt generate`, you should set `target: 'static'` in your `nuxt.config`
6:23:50 PM:         Learn more about it on https://go.nuxtjs.dev/static-target
6:24:03 PM: [error] [BABEL] Note: The code generator has deoptimised the styling of /opt/build/repo/node_modules/bootstrap-vue/src/icons/icons.js as it exceeds the max of 500KB.
6:24:24 PM: [error]  /loginBtn
6:24:24 PM: 
6:24:24 PM: TypeError: Cannot read property 'headers' of undefined
6:24:24 PM:     at Store.nuxtServerInit (store/index.js:53:0)
6:24:24 PM:     at Array.wrappedActionHandler (/opt/build/repo/node_modules/vuex/dist/vuex.common.js:853:23)
6:24:24 PM:     at Store.dispatch (/opt/build/repo/node_modules/vuex/dist/vuex.common.js:518:15)
6:24:24 PM:     at Store.boundDispatch [as dispatch] (/opt/build/repo/node_modules/vuex/dist/vuex.common.js:408:21)
6:24:24 PM:     at module.exports.__webpack_exports__.default (.nuxt/server.js:131:0)
6:24:24 PM:     at runNextTicks (internal/process/task_queues.js:62:5)
6:24:24 PM:     at listOnTimeout (internal/timers.js:518:9)
6:24:24 PM:     at processTimers (internal/timers.js:492:7)

nuxt code The source code below is the code to get the user information every time the page is loaded.

  1. Get the token of the cookie and request it as headers information.
  2. Save the value returned from the backend as user information.
export const actions = {
  async nuxtServerInit({ commit },{ req }){
      const token = 'Bearer ' + cookieparser.parse(req.headers.cookie).token
      let user = ''; 
      try {
          user = await this.$axios.$get('/user',{ 
            headers:{ 
              'Authorization': token,
              'Accept':'application/json'
            }
          })
        } catch (err) {
          console.log(err);
        }
      commit('setToken',{ token:cookieparser.parse(req.headers.cookie).token});
      commit('setUser',user);
    }
}

As far as I read the error, it seems to be an error with headers in const token ='Bearer' + cookieparser.parse (req.headers.cookie) .token. Is there any relationship between cookieparser and netlify?

kissu
  • 40,416
  • 14
  • 65
  • 133
tmasu
  • 17
  • 5

1 Answers1

0

The error is pretty self-explanatory here

When using nuxt generate, you should set target: 'static' in your nuxt.config

What is the content of your nuxt.config.js? It does have target: server as of right now I guess.

Either use target: static and nuxt generate or target: server and nuxt build but not both. Of course, each of the targets will have consequences and it depends of your needs.

More info can be found here: https://stackoverflow.com/a/63638062/8816585


EDIT, also your axios configuration is not correct. It should look something like this

this.$axios.$get('/user', 
  { "body": "super cool data" },
  headers: {
    'Content-Type': 'application/json'
  })

while you wrote it like this

this.$axios.$get('/user', 
  headers: {
    'Content-Type': 'application/json'
  })
kissu
  • 40,416
  • 14
  • 65
  • 133