From 6d29b59a708b7ff4745049370c2b09c4a4c656eb Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Mon, 17 Jan 2022 19:19:50 +0100 Subject: [PATCH 1/9] Add URL behaviour tests --- .../middleware/core/pages/urls/_middleware.js | 35 ++++++++++ .../middleware/core/pages/urls/index.js | 3 + .../middleware/core/pages/urls/urls-b.js | 3 + .../middleware/core/test/index.test.js | 67 +++++++++++++++++++ .../middleware/hmr/pages/about/_middleware.js | 4 +- 5 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 test/integration/middleware/core/pages/urls/_middleware.js create mode 100644 test/integration/middleware/core/pages/urls/index.js create mode 100644 test/integration/middleware/core/pages/urls/urls-b.js diff --git a/test/integration/middleware/core/pages/urls/_middleware.js b/test/integration/middleware/core/pages/urls/_middleware.js new file mode 100644 index 0000000000000..43efc371d2e6d --- /dev/null +++ b/test/integration/middleware/core/pages/urls/_middleware.js @@ -0,0 +1,35 @@ +import { NextResponse, NextRequest } from 'next/server' + +export function middleware(request) { + try { + if (request.nextUrl.pathname === '/urls/relative-url') { + return NextResponse.json({ message: String(new URL('/relative')) }) + } + + if (request.nextUrl.pathname === '/urls/relative-request') { + return fetch(new Request('/urls/urls-b')) + } + + if (request.nextUrl.pathname === '/urls/relative-redirect') { + return Response.redirect('/urls/urls-b') + } + + if (request.nextUrl.pathname === '/urls/relative-next-redirect') { + return NextResponse.redirect('/urls/urls-b') + } + + if (request.nextUrl.pathname === '/urls/relative-next-rewrite') { + return NextResponse.rewrite('/urls/urls-b') + } + + if (request.nextUrl.pathname === '/urls/relative-next-request') { + return fetch(new NextRequest('/urls/urls-b')) + } + } catch (error) { + return NextResponse.json({ + error: { + message: error.message, + }, + }) + } +} diff --git a/test/integration/middleware/core/pages/urls/index.js b/test/integration/middleware/core/pages/urls/index.js new file mode 100644 index 0000000000000..a1a42f2f39375 --- /dev/null +++ b/test/integration/middleware/core/pages/urls/index.js @@ -0,0 +1,3 @@ +export default function URLsA() { + return

URLs A

+} diff --git a/test/integration/middleware/core/pages/urls/urls-b.js b/test/integration/middleware/core/pages/urls/urls-b.js new file mode 100644 index 0000000000000..19326a7b8d8ac --- /dev/null +++ b/test/integration/middleware/core/pages/urls/urls-b.js @@ -0,0 +1,3 @@ +export default function URLsB() { + return

URLs B

+} diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index 25f4e01634ab3..e90aa64045807 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -43,6 +43,8 @@ describe('Middleware base tests', () => { responseTests('/fr') interfaceTests() interfaceTests('/fr') + urlTests() + urlTests('/fr') it('should have showed warning for middleware usage', () => { expect(output).toContain(middlewareWarning) @@ -78,6 +80,8 @@ describe('Middleware base tests', () => { responseTests('/fr') interfaceTests() interfaceTests('/fr') + urlTests() + urlTests('/fr') it('should have middleware warning during build', () => { expect(buildOutput).toContain(middlewareWarning) @@ -104,6 +108,69 @@ describe('Middleware base tests', () => { }) }) +function urlTests(locale = '') { + it('rewrites by default to a target location', async () => { + const res = await fetchViaHTTP(context.appPort, `${locale}/urls`) + const html = await res.text() + const $ = cheerio.load(html) + expect($('.title').text()).toBe('URLs A') + }) + + it('throws when using URL with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-url` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) + + it('throws when using Request with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-request` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) + + it('throws when using Response.redirect with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-redirect` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) + + it('throws when using NextRequest with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-next-request` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) + + it('throws when using NextResponse.redirect with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-next-redirect` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) + + it('throws when using NextResponse.rewrite with a relative URL', async () => { + const res = await fetchViaHTTP( + context.appPort, + `${locale}/urls/relative-next-rewrite` + ) + const json = await res.json() + expect(json.error.message).toEqual('Invalid URL') + }) +} + function rewriteTests(locale = '') { it('should rewrite to fallback: true page successfully', async () => { const randomSlug = `another-${Date.now()}` diff --git a/test/integration/middleware/hmr/pages/about/_middleware.js b/test/integration/middleware/hmr/pages/about/_middleware.js index 64dba899b58b9..66902bd191d7f 100644 --- a/test/integration/middleware/hmr/pages/about/_middleware.js +++ b/test/integration/middleware/hmr/pages/about/_middleware.js @@ -1,5 +1,5 @@ import { NextResponse } from 'next/server' -export function middleware() { - return NextResponse.rewrite('/about/a') +export function middleware(request) { + return NextResponse.rewrite(new URL('/about/a', request.url)) } From 67350857acf3f5a7d2e969af0ca80b6657bf28e9 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Mon, 17 Jan 2022 19:20:11 +0100 Subject: [PATCH 2/9] Make the runtime work only with absolute URLs --- packages/next/server/web/spec-compliant/response.ts | 2 +- packages/next/server/web/spec-extension/response.ts | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/next/server/web/spec-compliant/response.ts b/packages/next/server/web/spec-compliant/response.ts index c7d5c746e9316..3991c8ecef80c 100644 --- a/packages/next/server/web/spec-compliant/response.ts +++ b/packages/next/server/web/spec-compliant/response.ts @@ -45,7 +45,7 @@ class BaseResponse extends Body implements Response { ) } - return new Response(url, { + return new Response(String(new URL(url)), { headers: { Location: url }, status, }) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index 76b40a8490407..027bfaf7ac530 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -79,7 +79,8 @@ export class NextResponse extends Response { 'Failed to execute "redirect" on "response": Invalid status code' ) } - const destination = typeof url === 'string' ? url : url.toString() + + const destination = String(new URL(String(url))) return new NextResponse(destination, { headers: { Location: destination }, status, @@ -89,10 +90,7 @@ export class NextResponse extends Response { static rewrite(destination: string | NextURL) { return new NextResponse(null, { headers: { - 'x-middleware-rewrite': - typeof destination === 'string' - ? destination - : destination.toString(), + 'x-middleware-rewrite': String(new URL(String(destination))), }, }) } From b80aa39c1cbdf66d191bdb1a1eb9ff3bd6ecba4c Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Mon, 17 Jan 2022 19:20:20 +0100 Subject: [PATCH 3/9] Fix remaining tests --- .../middleware/core/pages/interface/_middleware.js | 3 ++- .../middleware/core/pages/redirects/_middleware.js | 2 +- .../middleware/core/pages/rewrites/_middleware.js | 13 ++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/test/integration/middleware/core/pages/interface/_middleware.js b/test/integration/middleware/core/pages/interface/_middleware.js index 81b57cba1fb4d..1aee8917ed5c9 100644 --- a/test/integration/middleware/core/pages/interface/_middleware.js +++ b/test/integration/middleware/core/pages/interface/_middleware.js @@ -81,7 +81,8 @@ export async function middleware(request) { } if (url.pathname.endsWith('/dynamic-replace')) { - return NextResponse.rewrite('/_interface/dynamic-path') + url.pathname = '/_interface/dynamic-path' + return NextResponse.rewrite(url) } return new Response(null, { diff --git a/test/integration/middleware/core/pages/redirects/_middleware.js b/test/integration/middleware/core/pages/redirects/_middleware.js index e97cb99011b04..0a8dafc5c8b26 100644 --- a/test/integration/middleware/core/pages/redirects/_middleware.js +++ b/test/integration/middleware/core/pages/redirects/_middleware.js @@ -56,6 +56,6 @@ export async function middleware(request) { if (url.pathname === '/redirects/infinite-loop-1') { url.pathname = '/redirects/infinite-loop' - return Response.redirect(url.pathname) + return Response.redirect(url) } } diff --git a/test/integration/middleware/core/pages/rewrites/_middleware.js b/test/integration/middleware/core/pages/rewrites/_middleware.js index 13d4453938251..20113e2978d16 100644 --- a/test/integration/middleware/core/pages/rewrites/_middleware.js +++ b/test/integration/middleware/core/pages/rewrites/_middleware.js @@ -5,24 +5,27 @@ export async function middleware(request) { if (url.pathname.startsWith('/rewrites/to-blog')) { const slug = url.pathname.split('/').pop() - console.log('rewriting to slug', slug) - return NextResponse.rewrite(`/rewrites/fallback-true-blog/${slug}`) + url.pathname = `/rewrites/fallback-true-blog/${slug}` + return NextResponse.rewrite(url) } if (url.pathname === '/rewrites/rewrite-to-ab-test') { let bucket = request.cookies.bucket if (!bucket) { bucket = Math.random() >= 0.5 ? 'a' : 'b' - const response = NextResponse.rewrite(`/rewrites/${bucket}`) + url.pathname = `/rewrites/${bucket}` + const response = NextResponse.rewrite(url) response.cookie('bucket', bucket, { maxAge: 10000 }) return response } - return NextResponse.rewrite(`/rewrites/${bucket}`) + url.pathname = `/rewrites/${bucket}` + return NextResponse.rewrite(url) } if (url.pathname === '/rewrites/rewrite-me-to-about') { - return NextResponse.rewrite('/rewrites/about') + url.pathname = '/rewrites/about' + return NextResponse.rewrite(url) } if (url.pathname === '/rewrites/rewrite-me-to-vercel') { From 6dea23980fd93f28d865c35374de712406399934 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 11:42:07 +0100 Subject: [PATCH 4/9] Make tests pass for all node versions --- test/integration/middleware/core/test/index.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index e90aa64045807..ca6a8f6e277f6 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -122,7 +122,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-url` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) it('throws when using Request with a relative URL', async () => { @@ -131,7 +131,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-request` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) it('throws when using Response.redirect with a relative URL', async () => { @@ -140,7 +140,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-redirect` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) it('throws when using NextRequest with a relative URL', async () => { @@ -149,7 +149,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-next-request` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) it('throws when using NextResponse.redirect with a relative URL', async () => { @@ -158,7 +158,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-next-redirect` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) it('throws when using NextResponse.rewrite with a relative URL', async () => { @@ -167,7 +167,7 @@ function urlTests(locale = '') { `${locale}/urls/relative-next-rewrite` ) const json = await res.json() - expect(json.error.message).toEqual('Invalid URL') + expect(json.error.message).toContain('Invalid URL') }) } From e7066d95b05988c0f792a8e3111a31ea087d2fce Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 13:14:22 +0100 Subject: [PATCH 5/9] Update URL tests to show a warning --- .../middleware/core/test/index.test.js | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/test/integration/middleware/core/test/index.test.js b/test/integration/middleware/core/test/index.test.js index ca6a8f6e277f6..32e2b99c2f4a2 100644 --- a/test/integration/middleware/core/test/index.test.js +++ b/test/integration/middleware/core/test/index.test.js @@ -19,18 +19,20 @@ const context = {} context.appDir = join(__dirname, '../') const middlewareWarning = 'using beta Middleware (not covered by semver)' +const urlsWarning = 'using relative URLs for Middleware will be deprecated soon' describe('Middleware base tests', () => { describe('dev mode', () => { - let output = '' + const log = { output: '' } + beforeAll(async () => { context.appPort = await findPort() context.app = await launchApp(context.appDir, context.appPort, { onStdout(msg) { - output += msg + log.output += msg }, onStderr(msg) { - output += msg + log.output += msg }, }) }) @@ -43,16 +45,16 @@ describe('Middleware base tests', () => { responseTests('/fr') interfaceTests() interfaceTests('/fr') - urlTests() - urlTests('/fr') + urlTests(log) + urlTests(log, '/fr') it('should have showed warning for middleware usage', () => { - expect(output).toContain(middlewareWarning) + expect(log.output).toContain(middlewareWarning) }) }) describe('production mode', () => { + let serverOutput = { output: '' } let buildOutput - let serverOutput beforeAll(async () => { const res = await nextBuild(context.appDir, undefined, { @@ -64,10 +66,10 @@ describe('Middleware base tests', () => { context.appPort = await findPort() context.app = await nextStart(context.appDir, context.appPort, { onStdout(msg) { - serverOutput += msg + serverOutput.output += msg }, onStderr(msg) { - serverOutput += msg + serverOutput.output += msg }, }) }) @@ -80,15 +82,15 @@ describe('Middleware base tests', () => { responseTests('/fr') interfaceTests() interfaceTests('/fr') - urlTests() - urlTests('/fr') + urlTests(serverOutput) + urlTests(serverOutput, '/fr') it('should have middleware warning during build', () => { expect(buildOutput).toContain(middlewareWarning) }) it('should have middleware warning during start', () => { - expect(serverOutput).toContain(middlewareWarning) + expect(serverOutput.output).toContain(middlewareWarning) }) it('should have correct files in manifest', async () => { @@ -108,7 +110,7 @@ describe('Middleware base tests', () => { }) }) -function urlTests(locale = '') { +function urlTests(log, locale = '') { it('rewrites by default to a target location', async () => { const res = await fetchViaHTTP(context.appPort, `${locale}/urls`) const html = await res.text() @@ -134,15 +136,6 @@ function urlTests(locale = '') { expect(json.error.message).toContain('Invalid URL') }) - it('throws when using Response.redirect with a relative URL', async () => { - const res = await fetchViaHTTP( - context.appPort, - `${locale}/urls/relative-redirect` - ) - const json = await res.json() - expect(json.error.message).toContain('Invalid URL') - }) - it('throws when using NextRequest with a relative URL', async () => { const res = await fetchViaHTTP( context.appPort, @@ -152,22 +145,19 @@ function urlTests(locale = '') { expect(json.error.message).toContain('Invalid URL') }) - it('throws when using NextResponse.redirect with a relative URL', async () => { - const res = await fetchViaHTTP( - context.appPort, - `${locale}/urls/relative-next-redirect` - ) - const json = await res.json() - expect(json.error.message).toContain('Invalid URL') + it('warns when using Response.redirect with a relative URL', async () => { + await fetchViaHTTP(context.appPort, `${locale}/urls/relative-redirect`) + expect(log.output).toContain(urlsWarning) }) - it('throws when using NextResponse.rewrite with a relative URL', async () => { - const res = await fetchViaHTTP( - context.appPort, - `${locale}/urls/relative-next-rewrite` - ) - const json = await res.json() - expect(json.error.message).toContain('Invalid URL') + it('warns when using NextResponse.redirect with a relative URL', async () => { + await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-redirect`) + expect(log.output).toContain(urlsWarning) + }) + + it('warns when using NextResponse.rewrite with a relative URL', async () => { + await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-rewrite`) + expect(log.output).toContain(urlsWarning) }) } From 45d50e773af2af2acfce2ea7f7563dce8c19f50a Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 13:37:43 +0100 Subject: [PATCH 6/9] Add clone method for NextURL --- packages/next/server/web/next-url.ts | 4 ++++ test/unit/web-runtime/next-url.test.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/next/server/web/next-url.ts b/packages/next/server/web/next-url.ts index 8ee57ff66a6cb..532b93e64ab38 100644 --- a/packages/next/server/web/next-url.ts +++ b/packages/next/server/web/next-url.ts @@ -244,6 +244,10 @@ export class NextURL { toJSON() { return this.href } + + clone() { + return new NextURL(String(this), this[Internal].options) + } } const REGEX_LOCALHOST_HOSTNAME = diff --git a/test/unit/web-runtime/next-url.test.ts b/test/unit/web-runtime/next-url.test.ts index e3914a938e920..ed13e63eb217f 100644 --- a/test/unit/web-runtime/next-url.test.ts +++ b/test/unit/web-runtime/next-url.test.ts @@ -170,3 +170,21 @@ it('allows to change the port', () => { url.port = '' expect(url.href).toEqual('https://localhost/foo') }) + +it('allows to clone a new copy', () => { + const url = new NextURL('/root/es/bar', { + base: 'http://127.0.0.1', + basePath: '/root', + i18n: { + defaultLocale: 'en', + locales: ['en', 'es', 'fr'], + }, + }) + + const clone = url.clone() + clone.pathname = '/test' + clone.basePath = '/root-test' + + expect(url.toString()).toEqual('http://localhost/root/es/bar') + expect(clone.toString()).toEqual('http://localhost/root-test/es/test') +}) From 3ef00507abd63993de9f32b829d693c4502bea34 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 13:45:18 +0100 Subject: [PATCH 7/9] Add a util to warn on relative URLs --- errors/manifest.json | 4 ++++ errors/middleware-relative-urls.md | 38 ++++++++++++++++++++++++++++++ packages/next/server/web/utils.ts | 17 +++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 errors/middleware-relative-urls.md diff --git a/errors/manifest.json b/errors/manifest.json index 4aaeead555a5a..69d0763077574 100644 --- a/errors/manifest.json +++ b/errors/manifest.json @@ -527,6 +527,10 @@ { "title": "invalid-styled-jsx-children", "path": "/errors/invalid-styled-jsx-children.md" + }, + { + "title": "middleware-relative-urls", + "path": "/errors/middleware-relative-urls.md" } ] } diff --git a/errors/middleware-relative-urls.md b/errors/middleware-relative-urls.md new file mode 100644 index 0000000000000..db832e3faea98 --- /dev/null +++ b/errors/middleware-relative-urls.md @@ -0,0 +1,38 @@ +# Middleware Relative URLs + +#### Why This Error Occurred + +You are using a Middleware function that uses `Response.redirect(url)`, `NextResponse.redirect(url)` or `NextResponse.rewrite(url)` where `url` is a relative or an invalid URL. Currently this will work, but building a request with `new Request(url)` or running `fetch(url)` when `url` is a relative URL will **not** work. For this reason and to bring consistency to Next.js Middleware, this behavior will be deprecated soon in favor of always using absolute URLs. + +#### Possible Ways to Fix It + +To fix this warning you must always pass absolute URL for redirecting and rewriting. There are several ways to get the absolute URL but the recommended way is to clone `NextURL` and mutate it: + +```typescript +import type { NextRequest } from 'next/server' +import { NextResponse } from 'next/server' + +export function middleware(request: NextRequest) { + const url = request.nextUrl.clone() + url.pathname = '/dest' + return NextResponse.rewrite(url) +} +``` + +Another way to fix this error could be to use the original URL as the base but this will not consider configuration like `basePath` or `locale`: + +```typescript +import type { NextRequest } from 'next/server' +import { NextResponse } from 'next/server' + +export function middleware(request: NextRequest) { + return NextResponse.rewrite(new URL('/dest', request.url)) +} +``` + +Or you can simply pass a string containing a valid absolute URL. + +### Useful Links + +- [URL Documentation](https://developer.mozilla.org/en-US/docs/Web/API/URL) +- [Response Documentation](https://developer.mozilla.org/en-US/docs/Web/API/Response) diff --git a/packages/next/server/web/utils.ts b/packages/next/server/web/utils.ts index 2c5dae0e83dcd..06e257457f09d 100644 --- a/packages/next/server/web/utils.ts +++ b/packages/next/server/web/utils.ts @@ -147,3 +147,20 @@ export function splitCookiesString(cookiesString: string) { return cookiesStrings } + +/** + * We will be soon deprecating the usage of relative URLs in Middleware introducing + * URL validation. This helper puts the future code in place and prints a warning + * for cases where it will break. Meanwhile we preserve the previous behavior. + */ +export function validateURL(url: string | URL): string { + try { + return String(new URL(String(url))) + } catch (error: any) { + console.log( + `warn -`, + 'using relative URLs for Middleware will be deprecated soon - https://nextjs.org/docs/messages/middleware-relative-urls' + ) + return String(url) + } +} From 743f3241d09c6855b2a0458b4969223448ae7430 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 13:45:36 +0100 Subject: [PATCH 8/9] Roll back to warn instead of failing --- packages/next/server/web/spec-compliant/response.ts | 3 ++- packages/next/server/web/spec-extension/response.ts | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/next/server/web/spec-compliant/response.ts b/packages/next/server/web/spec-compliant/response.ts index 3991c8ecef80c..3ea1a55650f6b 100644 --- a/packages/next/server/web/spec-compliant/response.ts +++ b/packages/next/server/web/spec-compliant/response.ts @@ -1,5 +1,6 @@ import { Body, BodyInit, cloneBody, extractContentType } from './body' import { NextURL } from '../next-url' +import { validateURL } from '../utils' const INTERNALS = Symbol('internal response') const REDIRECTS = new Set([301, 302, 303, 307, 308]) @@ -45,7 +46,7 @@ class BaseResponse extends Body implements Response { ) } - return new Response(String(new URL(url)), { + return new Response(validateURL(url), { headers: { Location: url }, status, }) diff --git a/packages/next/server/web/spec-extension/response.ts b/packages/next/server/web/spec-extension/response.ts index 027bfaf7ac530..240096c3bd30d 100644 --- a/packages/next/server/web/spec-extension/response.ts +++ b/packages/next/server/web/spec-extension/response.ts @@ -1,6 +1,6 @@ import type { I18NConfig } from '../../config-shared' import { NextURL } from '../next-url' -import { toNodeHeaders } from '../utils' +import { toNodeHeaders, validateURL } from '../utils' import cookie from 'next/dist/compiled/cookie' import { CookieSerializeOptions } from '../types' @@ -80,7 +80,7 @@ export class NextResponse extends Response { ) } - const destination = String(new URL(String(url))) + const destination = validateURL(url) return new NextResponse(destination, { headers: { Location: destination }, status, @@ -90,7 +90,7 @@ export class NextResponse extends Response { static rewrite(destination: string | NextURL) { return new NextResponse(null, { headers: { - 'x-middleware-rewrite': String(new URL(String(destination))), + 'x-middleware-rewrite': validateURL(destination), }, }) } From 1b86acab00549284667a42e359637e36b67178b1 Mon Sep 17 00:00:00 2001 From: Javi Velasco Date: Wed, 19 Jan 2022 13:57:23 +0100 Subject: [PATCH 9/9] Fix lint warning --- errors/middleware-relative-urls.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errors/middleware-relative-urls.md b/errors/middleware-relative-urls.md index db832e3faea98..dabdf33060855 100644 --- a/errors/middleware-relative-urls.md +++ b/errors/middleware-relative-urls.md @@ -30,7 +30,7 @@ export function middleware(request: NextRequest) { } ``` -Or you can simply pass a string containing a valid absolute URL. +You can also pass directly a string containing a valid absolute URL. ### Useful Links