From ab324f10fd2e61a40e88b913143f26a138829007 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 11 Feb 2022 16:02:41 +0000 Subject: [PATCH 1/5] fix(nuxt3): return 401 code on blocked initial navigation (hotfix) --- packages/nuxt3/src/pages/runtime/router.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nuxt3/src/pages/runtime/router.ts b/packages/nuxt3/src/pages/runtime/router.ts index 246f303c2d9..8a5af05d6b3 100644 --- a/packages/nuxt3/src/pages/runtime/router.ts +++ b/packages/nuxt3/src/pages/runtime/router.ts @@ -105,7 +105,12 @@ export default defineNuxtPlugin((nuxtApp) => { if (process.server) { router.push(nuxtApp.ssrContext.url) - router.afterEach((to) => { + router.afterEach((to, from, failure) => { + if (failure) { + // TODO: https://github.com/nuxt/framework/discussions/559 + nuxtApp.ssrContext.res.statusCode = 401 + nuxtApp.ssrContext.res.end() + } if (to.fullPath !== nuxtApp.ssrContext.url) { nuxtApp.ssrContext.res.setHeader('Location', to.fullPath) } From ef6eb9965310c172a61997e45fb3dddb387ce041 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 12 Feb 2022 20:34:42 +0000 Subject: [PATCH 2/5] Update packages/nuxt3/src/pages/runtime/router.ts Co-authored-by: pooya parsa --- packages/nuxt3/src/pages/runtime/router.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/nuxt3/src/pages/runtime/router.ts b/packages/nuxt3/src/pages/runtime/router.ts index 8a5af05d6b3..2781de6bfaf 100644 --- a/packages/nuxt3/src/pages/runtime/router.ts +++ b/packages/nuxt3/src/pages/runtime/router.ts @@ -107,7 +107,6 @@ export default defineNuxtPlugin((nuxtApp) => { router.afterEach((to, from, failure) => { if (failure) { - // TODO: https://github.com/nuxt/framework/discussions/559 nuxtApp.ssrContext.res.statusCode = 401 nuxtApp.ssrContext.res.end() } From 187329352786c8f5c9e420b29cd76f819acf6f7c Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 12 Feb 2022 20:48:08 +0000 Subject: [PATCH 3/5] refactor: throw error when nav is aborted on ssr --- packages/nuxt3/src/pages/runtime/router.ts | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/nuxt3/src/pages/runtime/router.ts b/packages/nuxt3/src/pages/runtime/router.ts index 2781de6bfaf..80b5d51985b 100644 --- a/packages/nuxt3/src/pages/runtime/router.ts +++ b/packages/nuxt3/src/pages/runtime/router.ts @@ -93,6 +93,11 @@ export default defineNuxtPlugin((nuxtApp) => { } const result = await callWithNuxt(nuxtApp, middleware, [to, from]) + if (process.server) { + if (result === false || result instanceof Error) { + throw result || new Error(`Route navigation aborted: ${nuxtApp.ssrContext.url}`) + } + } if (result || result === false) { return result } } }) @@ -105,24 +110,25 @@ export default defineNuxtPlugin((nuxtApp) => { if (process.server) { router.push(nuxtApp.ssrContext.url) - router.afterEach((to, from, failure) => { - if (failure) { - nuxtApp.ssrContext.res.statusCode = 401 - nuxtApp.ssrContext.res.end() - } + router.afterEach((to) => { if (to.fullPath !== nuxtApp.ssrContext.url) { nuxtApp.ssrContext.res.setHeader('Location', to.fullPath) } }) } - await router.isReady() + try { + await router.isReady() - const is404 = router.currentRoute.value.matched.length === 0 - if (process.server && is404) { - const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`) - // @ts-ignore - error.statusCode = 404 + const is404 = router.currentRoute.value.matched.length === 0 + if (process.server && is404) { + const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`) + // @ts-ignore + error.statusCode = 404 + nuxtApp.ssrContext.error = error + } + } catch (error) { + error.statusCode = error.statusCode || 500 nuxtApp.ssrContext.error = error } }) From 3deab760e0131076b453cd0378f6dbea6fd627f4 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 14 Feb 2022 11:04:20 +0000 Subject: [PATCH 4/5] refactor: use `createError` and set status code higher up --- packages/nuxt3/src/pages/runtime/router.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/nuxt3/src/pages/runtime/router.ts b/packages/nuxt3/src/pages/runtime/router.ts index 80b5d51985b..2a105dba66e 100644 --- a/packages/nuxt3/src/pages/runtime/router.ts +++ b/packages/nuxt3/src/pages/runtime/router.ts @@ -6,6 +6,7 @@ import { RouterLink, NavigationGuard } from 'vue-router' +import { createError } from 'h3' import NuxtPage from './page' import NuxtLayout from './layout' import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app' @@ -95,7 +96,11 @@ export default defineNuxtPlugin((nuxtApp) => { const result = await callWithNuxt(nuxtApp, middleware, [to, from]) if (process.server) { if (result === false || result instanceof Error) { - throw result || new Error(`Route navigation aborted: ${nuxtApp.ssrContext.url}`) + const error = result || createError({ + statusMessage: `Route navigation aborted: ${nuxtApp.ssrContext.url}` + }) + nuxtApp.ssrContext.error = error + throw error } } if (result || result === false) { return result } @@ -128,7 +133,6 @@ export default defineNuxtPlugin((nuxtApp) => { nuxtApp.ssrContext.error = error } } catch (error) { - error.statusCode = error.statusCode || 500 nuxtApp.ssrContext.error = error } }) From 802419db9eb0cf34b44412a8540b5b49e2482901 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 14 Feb 2022 11:06:14 +0000 Subject: [PATCH 5/5] refactor: use createError for 404 as well --- packages/nuxt3/src/pages/runtime/router.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/nuxt3/src/pages/runtime/router.ts b/packages/nuxt3/src/pages/runtime/router.ts index 2a105dba66e..c1ccec61c4b 100644 --- a/packages/nuxt3/src/pages/runtime/router.ts +++ b/packages/nuxt3/src/pages/runtime/router.ts @@ -127,10 +127,10 @@ export default defineNuxtPlugin((nuxtApp) => { const is404 = router.currentRoute.value.matched.length === 0 if (process.server && is404) { - const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`) - // @ts-ignore - error.statusCode = 404 - nuxtApp.ssrContext.error = error + throw createError({ + statusCode: 404, + statusMessage: `Page not found: ${nuxtApp.ssrContext.url}` + }) } } catch (error) { nuxtApp.ssrContext.error = error