Static GitHub Issues

[2427] server.js :#126 swallows third party errors during server side rendering

prev: Help wanted: Possible to get child depth in asyncData?
next: nuxt发布生产环境后,大概首页加载有1.2m大,请问该如何优化文件大小

Symptom:

Developer installs a new plugin, module, etc via package manager and configures nuxt.config.js to include it. Then on page request nuxt throws Cannot read property 'middleware' of undefined on server.js L#126:15. This is because the layout variable is undefined.

// Set layout
  let layout = Components.length ? Components[0].options.layout : NuxtError.layout
  if (typeof layout === 'function') layout = layout(ctx)
  await _app.loadLayout(layout)
  layout = _app.setLayout(layout)
  // ...Set layout to __NUXT__
  context.nuxt.layout = _app.layoutName

  // Call middleware (layout + pages)
  if (!context.nuxt.error) {
    midd = []
    if (layout && layout.middleware) midd = midd.concat(layout.middleware)

I'm not totally familiar with the internals here, but I did simply add

    if (layout && layout.middleware) midd = midd.concat(layout.middleware)

which then surfaced my real error when my error.vue layout rendered. I suspect this isn't the exact behavior the nuxt team expects in this use case, but at least I could troubleshoot my error. In my particular case I updated to FontAwesome 5 and its not very friendly to SSR. FontAwesome is throwing Cannot read property 'doScroll' of undefined trying to look for window on the server side. 😢

Regardless, it's probably a good idea to check that layout variable is set here, or if another approach is suggested I'm happy to test and submit a PR with that feedback.

Thanks!

<!--cmty--><!--cmty_prevent_hook--><div align="right"><sub><em>This question is available on <a href="https://nuxtjs.cmty.io">Nuxt.js</a> community (<a href="https://nuxtjs.cmty.io/nuxt/nuxt.js/issues/c2106">#c2106</a>)</em></sub></div>