Repository: nuxt/nuxt
File: packages/vue-app/template/server.js (JavaScript)

Code before:

  // ...If .validate() returned false
  if (!isValid) {
    // Render a 404 error page
    return render404Page()
  }
  <% } %>

  // If no Components found, returns 404
  if (!Components.length) {
  return render404Page()
}

  <% if (features.asyncData || features.fetch) { %>
  // Call asyncData & fetch hooks on components matched by the route.
  const asyncDatas = await Promise.all(Components.map((Component) => {
  const promises = []

    <% if (features.asyncData) { %>
    // Call asyncData(context)
    if (Component.options.asyncData && typeof Component.options.asyncData === 'function') {
        const promise = promisify(Component.options.asyncData, app.context)
        promise.then((asyncDataResult) => {
          ssrContext.asyncData[Component.cid] = asyncDataResult
          applyAsyncData(Component)
        }).catch(() => {})
        promises.push(promise)
      } else {
        promises.push(null)
      }
    <% } %>

    <% if (features.fetch) { %>
    // Call fetch(context)
    if (Component.options.fetch && Component.options.fetch.length) {
      promises.push(Component.options.fetch(app.context))
    } else {
      promises.push(null)
    }
    <% } %>

    return Promise.all(promises)
}))

    <% if (debug) { %>if (process.env.DEBUG && asyncDatas.length) console.debug('Data fetching ' + ssrContext.url + ': ' + (Date.now() - s) + 'ms') <% } %>

      // datas are the first row of each
      ssrContext.nuxt.data = asyncDatas.map(r => r[0] || {})
        <% } %>

  // ...If there is a redirect or an error, stop the process

Review comment (bug):
After discussing this a bit with @danielroe, I realized then maybe the intention here was to push this chained promise to `promises` rather than the original one? So instead of the current code do:

```js
        let promise = promisify(Component.options.asyncData, app.context)
        promise = promise.then((asyncDataResult) => {
          ssrContext.asyncData[Component.cid] = asyncDataResult
          applyAsyncData(Component)
          return asyncDataResult
        })
        promises.push(promise)

```

(with or without extra `.catch()`).

But that's more risky change as crash in `.then` handler would then affect other code up the chain. Maybe that would be the right thing to do but the change I made is at least safe from the perspective of not changing the behavior apart from catching unhandled rejection.

Code after:

  // ...If .validate() returned false
  if (!isValid) {
    // Render a 404 error page
    return render404Page()
  }
  <% } %>

  // If no Components found, returns 404
  if (!Components.length) {
  return render404Page()
}

  <% if (features.asyncData || features.fetch) { %>
  // Call asyncData & fetch hooks on components matched by the route.
  const asyncDatas = await Promise.all(Components.map((Component) => {
  const promises = []

    <% if (features.asyncData) { %>
    // Call asyncData(context)
    if (Component.options.asyncData && typeof Component.options.asyncData === 'function') {
      const promise = promisify(Component.options.asyncData, app.context)
        .then((asyncDataResult) => {
          ssrContext.asyncData[Component.cid] = asyncDataResult
          applyAsyncData(Component)
          return asyncDataResult
        })
      promises.push(promise)
    } else {
      promises.push(null)
    }
    <% } %>

    <% if (features.fetch) { %>
    // Call fetch(context)
    if (Component.options.fetch && Component.options.fetch.length) {
      promises.push(Component.options.fetch(app.context))
    } else {
      promises.push(null)
    }
    <% } %>

    return Promise.all(promises)
}))

    <% if (debug) { %>if (process.env.DEBUG && asyncDatas.length) console.debug('Data fetching ' + ssrContext.url + ': ' + (Date.now() - s) + 'ms') <% } %>

      // datas are the first row of each
      ssrContext.nuxt.data = asyncDatas.map(r => r[0] || {})
        <% } %>