Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nuxt): call app:error in SSR before rendering error page #20511

Merged
merged 3 commits into from Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/1.getting-started/8.error-handling.md
Expand Up @@ -41,6 +41,7 @@ This includes:

* running Nuxt plugins
* processing `app:created` and `app:beforeMount` hooks
* rendering your Vue app to HTML (on the server)
* mounting the app (on client-side), though you should handle this case with `onErrorCaptured` or with `vue:error`
* processing the `app:mounted` hook

Expand Down
9 changes: 5 additions & 4 deletions packages/nuxt/src/app/composables/cookie.ts
Expand Up @@ -48,11 +48,12 @@ export function useCookie <T = string | null | undefined> (name: string, _opts?:
}
}
const unhook = nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue)
nuxtApp.hooks.hookOnce('app:redirected', () => {
// don't write cookie subsequently when app:rendered is called
unhook()
const writeAndUnhook = () => {
unhook() // don't write cookie subsequently when app:rendered is called
return writeFinalCookieValue()
})
}
nuxtApp.hooks.hookOnce('app:error', writeAndUnhook)
nuxtApp.hooks.hookOnce('app:redirected', writeAndUnhook)
}

return cookie as CookieRef<T>
Expand Down
6 changes: 4 additions & 2 deletions packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -244,9 +244,11 @@ export default defineRenderHandler(async (event) => {
writeEarlyHints(event, link)
}

const _rendered = await renderer.renderToString(ssrContext).catch((error) => {
const _rendered = await renderer.renderToString(ssrContext).catch(async (error) => {
// Use explicitly thrown error in preference to subsequent rendering errors
throw (!ssrError && ssrContext.payload?.error) || error
const _err = (!ssrError && ssrContext.payload?.error) || error
await ssrContext.nuxt?.hooks.callHook('app:error', _err)
throw _err
})
await ssrContext.nuxt?.hooks.callHook('app:rendered', { ssrContext })

Expand Down
1 change: 1 addition & 0 deletions test/basic.test.ts
Expand Up @@ -571,6 +571,7 @@ describe('errors', () => {

it('should render a HTML error page', async () => {
const res = await fetch('/error')
expect(res.headers.get('Set-Cookie')).toBe('some-error=was%20set; Path=/')
expect(await res.text()).toContain('This is a custom error')
})

Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Expand Up @@ -45,7 +45,7 @@ describe.skipIf(isWindows || process.env.TEST_BUILDER === 'webpack' || process.e

it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"67.2k"')
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"67.3k"')

const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2657k"')
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/basic/pages/error.vue
Expand Up @@ -11,6 +11,7 @@ const { data, error } = await useAsyncData(() => {
}, { server: true })

if (error.value) {
useCookie('some-error').value = 'was set'
throw createError({ statusCode: 422, fatal: true, statusMessage: 'This is a custom error' })
}

Expand Down