From a618a5c1d1cf5fec0d89dda8d9877e70cccd278f Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Mon, 20 May 2024 22:02:46 +0200 Subject: [PATCH] fix(nuxt): encode `location` header in `navigateTo` (#27280) --- packages/nuxt/src/app/composables/router.ts | 3 +-- test/basic.test.ts | 6 ++++++ test/fixtures/basic/pages/redirect-with-encode.vue | 9 +++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/basic/pages/redirect-with-encode.vue diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts index 750a1aeaf6..d6395e5832 100644 --- a/packages/nuxt/src/app/composables/router.ts +++ b/packages/nuxt/src/app/composables/router.ts @@ -169,8 +169,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na nuxtApp.ssrContext!._renderResponse = { statusCode: sanitizeStatusCode(options?.redirectCode || 302, 302), body: ``, - // do not encode as this would break some modules and some environments - headers: { location }, + headers: { location: encodeURI(location) }, } return response } diff --git a/test/basic.test.ts b/test/basic.test.ts index 7d0b11552a..9e4e0b6021 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -972,6 +972,12 @@ describe('navigate', () => { expect(status).toEqual(404) }) + + it('expect to redirect with encoding', async () => { + const { status } = await fetch('/redirect-with-encode', { redirect: 'manual' }) + + expect(status).toEqual(302) + }) }) describe('preserves current instance', () => { diff --git a/test/fixtures/basic/pages/redirect-with-encode.vue b/test/fixtures/basic/pages/redirect-with-encode.vue new file mode 100644 index 0000000000..a59a44d653 --- /dev/null +++ b/test/fixtures/basic/pages/redirect-with-encode.vue @@ -0,0 +1,9 @@ + + +