fix(nuxt): skip vue render when redirecting (#21412)

This commit is contained in:
Daniel Roe 2023-06-07 13:18:50 +01:00 committed by GitHub
parent 6d62b65f97
commit 7710ed30fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 5 deletions

View File

@ -118,7 +118,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
const fullPath = typeof to === 'string' || isExternal ? toPath : router.resolve(to).fullPath || '/'
const location = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, fullPath)
async function redirect () {
async function redirect (response: any) {
// TODO: consider deprecating in favour of `app:rendered` and removing
await nuxtApp.callHook('app:redirected')
const encodedLoc = location.replace(/"/g, '%22')
@ -127,16 +127,16 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
body: `<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=${encodedLoc}"></head></html>`,
headers: { location }
}
return inMiddleware ? /* abort route navigation */ false : undefined
return response
}
// We wait to perform the redirect last in case any other middleware will intercept the redirect
// and redirect somewhere else instead.
if (!isExternal && inMiddleware) {
router.afterEach(final => (final.fullPath === fullPath) ? redirect() : undefined)
router.afterEach(final => final.fullPath === fullPath ? redirect(false) : undefined)
return to
}
return redirect()
return redirect(!inMiddleware ? undefined : /* abort route navigation */ false)
}
}

View File

@ -41,6 +41,7 @@ if (process.server) {
await nuxt.hooks.callHook('app:error', err)
nuxt.payload.error = (nuxt.payload.error || err) as any
}
if (ssrContext?._renderResponse) { throw new Error('skipping render') }
return vueApp
}

View File

@ -247,6 +247,9 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
}
const _rendered = await renderer.renderToString(ssrContext).catch(async (error) => {
// We use error to bypass full render if we have an early response we can make
if (ssrContext._renderResponse && error.message === 'skipping render') { return {} as ReturnType<typeof renderer['renderToString']> }
// Use explicitly thrown error in preference to subsequent rendering errors
const _err = (!ssrError && ssrContext.payload?.error) || error
await ssrContext.nuxt?.hooks.callHook('app:error', _err)

View File

@ -633,6 +633,13 @@ describe('navigate', () => {
expect(status).toEqual(302)
})
it('should not run setup function in path redirected to', async () => {
const { headers, status } = await fetch('/navigate-to-error', { redirect: 'manual' })
expect(headers.get('location')).toEqual('/setup-should-not-run')
expect(status).toEqual(302)
})
it('supports directly aborting navigation on SSR', async () => {
const { status } = await fetch('/navigate-to-false', { redirect: 'manual' })

View File

@ -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('"62.6k"')
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.8k"')
const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2285k"')

View File

@ -0,0 +1,14 @@
<template>
<div>You should not see me</div>
</template>
<script setup>
definePageMeta({
alias: ['/setup-should-not-run'],
middleware: to => to.path === '/navigate-to-error' ? navigateTo('/setup-should-not-run') : undefined
})
console.log('running setup')
useNuxtApp().hook('app:rendered', () => {
throw new Error('this should not run')
})
</script>