diff --git a/packages/vue-app/template/index.js b/packages/vue-app/template/index.js index 765c543a35..409a582431 100644 --- a/packages/vue-app/template/index.js +++ b/packages/vue-app/template/index.js @@ -246,9 +246,13 @@ async function createApp(ssrContext, config = {}) { // If server-side, wait for async component to be resolved first if (process.server && ssrContext && ssrContext.url) { await new Promise((resolve, reject) => { - router.push(ssrContext.url, resolve, () => { + router.push(ssrContext.url, resolve, (err) => { + // https://github.com/vuejs/vue-router/blob/v3.3.4/src/history/errors.js + if (!err._isRouter) return reject(err) + if (err.type !== 1 /* NavigationFailureType.redirected */) return resolve() + // navigated to a different route in router guard - const unregister = router.afterEach(async (to, from, next) => { + const unregister = router.afterEach(async (to, from) => { ssrContext.url = to.fullPath app.context.route = await getRouteData(to) app.context.params = to.params || {} diff --git a/test/dev/basic.ssr.test.js b/test/dev/basic.ssr.test.js index b2a47737a2..567c1536d1 100644 --- a/test/dev/basic.ssr.test.js +++ b/test/dev/basic.ssr.test.js @@ -350,6 +350,27 @@ describe('basic ssr', () => { expect(html.includes('Router Guard')).toBe(false) }) + test('/router-guard-error', async () => { + const { html, error } = await nuxt.server.renderRoute('/router-guard-error') + + expect(error).toBe(null) + expect(html.includes('Page content')).toBe(false) + }) + + test('/router-guard-error?error=zepe', async () => { + const { html, error } = await nuxt.server.renderRoute('/router-guard-error?error=zepe') + + expect(html.includes('Page content')).toBe(false) + expect(html).toContain('zepe') + expect(error.message).toContain('zepe') + expect(error.statusCode).toBe(500) + }) + + test('/router-guard-error?throw=ezae', async () => { + await expect(nuxt.server.renderRoute('/router-guard-error?throw=ezae')) + .rejects.toMatchObject({ message: 'ezae' }) + }) + test('/jsx', async () => { const { html } = await nuxt.server.renderRoute('/jsx') expect(html).toContain('