From c4fe8525ef6c15a52685313bb16edb3c52e0acc6 Mon Sep 17 00:00:00 2001
From: Julien Huang <63512348+huang-julien@users.noreply.github.com>
Date: Mon, 19 Sep 2022 10:54:35 +0200
Subject: [PATCH] fix(nuxt): add missing `process.client` for early redirect in
navigateTo (#7625)
---
packages/nuxt/src/app/composables/router.ts | 2 +-
test/basic.test.ts | 6 ++++++
test/fixtures/basic/pages/navigate-to-redirect.vue | 11 +++++++++++
3 files changed, 18 insertions(+), 1 deletion(-)
create mode 100644 test/fixtures/basic/pages/navigate-to-redirect.vue
diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts
index 6cfe749c9e..a16dbf7f04 100644
--- a/packages/nuxt/src/app/composables/router.ts
+++ b/packages/nuxt/src/app/composables/router.ts
@@ -79,7 +79,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
}
// Early redirect on client-side
- if (!isExternal && isProcessingMiddleware()) {
+ if (process.client && !isExternal && isProcessingMiddleware()) {
return to
}
diff --git a/test/basic.test.ts b/test/basic.test.ts
index 3d86a69d9e..554d2bb8a1 100644
--- a/test/basic.test.ts
+++ b/test/basic.test.ts
@@ -265,6 +265,12 @@ describe('middlewares', () => {
expect(html).toContain('auth: ')
expect(html).not.toContain('Injected by injectAuth middleware')
})
+
+ it('should redirect to index with http 307 with navigateTo on server side', async () => {
+ const html = await fetch('/navigate-to-redirect', { redirect: 'manual' })
+ expect(html.headers.get('location')).toEqual('/')
+ expect(html.status).toEqual(307)
+ })
})
describe('plugins', () => {
diff --git a/test/fixtures/basic/pages/navigate-to-redirect.vue b/test/fixtures/basic/pages/navigate-to-redirect.vue
new file mode 100644
index 0000000000..b0c7bea050
--- /dev/null
+++ b/test/fixtures/basic/pages/navigate-to-redirect.vue
@@ -0,0 +1,11 @@
+
+ You should not see me
+
+
+