Skip to content

Commit f0f322c

Browse files
Schnizijjk
andauthored
Remove deprecation for relative URL usage in middlewares (#34461)
* Remove deprecation for relative URL usage in middlewares * fix tests Co-authored-by: JJ Kasper <[email protected]>
1 parent d4d79b2 commit f0f322c

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

errors/middleware-relative-urls.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
#### Why This Error Occurred
44

5-
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.
5+
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. Prior to Next.js 12.1, we allowed passing relative URLs. However, constructing a request with `new Request(url)` or running `fetch(url)` when `url` is a relative URL **does not** work. For this reason and to bring consistency to Next.js Middleware, this behavior has been deprecated and now removed.
66

77
#### Possible Ways to Fix It
88

9-
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:
9+
To fix this error 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:
1010

1111
```typescript
1212
import type { NextRequest } from 'next/server'

packages/next/server/web/utils.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,16 @@ export function splitCookiesString(cookiesString: string) {
149149
}
150150

151151
/**
152-
* We will be soon deprecating the usage of relative URLs in Middleware introducing
153-
* URL validation. This helper puts the future code in place and prints a warning
154-
* for cases where it will break. Meanwhile we preserve the previous behavior.
152+
* Validate the correctness of a user-provided URL.
155153
*/
156154
export function validateURL(url: string | URL): string {
157155
try {
158156
return String(new URL(String(url)))
159157
} catch (error: any) {
160-
console.log(
161-
`warn -`,
162-
'using relative URLs for Middleware will be deprecated soon - https://nextjs.org/docs/messages/middleware-relative-urls'
158+
throw new Error(
159+
`URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls`,
160+
// @ts-expect-error This will work for people who enable the error causes polyfill
161+
{ cause: error }
163162
)
164-
return String(url)
165163
}
166164
}

test/integration/middleware/core/pages/rewrites/_middleware.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export async function middleware(request) {
1212
) {
1313
const isExternal = url.searchParams.get('override') === 'external'
1414
return NextResponse.rewrite(
15-
isExternal ? 'https://vercel.com' : '/rewrites/a'
15+
isExternal ? 'https://vercel.com' : new URL('/rewrites/a', request.url)
1616
)
1717
}
1818

test/integration/middleware/core/test/index.test.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const context = {}
1919
context.appDir = join(__dirname, '../')
2020

2121
const middlewareWarning = 'using beta Middleware (not covered by semver)'
22-
const urlsWarning = 'using relative URLs for Middleware will be deprecated soon'
22+
const urlsError = 'Please use only absolute URLs'
2323

2424
describe('Middleware base tests', () => {
2525
describe('dev mode', () => {
@@ -110,7 +110,7 @@ describe('Middleware base tests', () => {
110110
})
111111
})
112112

113-
function urlTests(log, locale = '') {
113+
function urlTests(_log, locale = '') {
114114
it('rewrites by default to a target location', async () => {
115115
const res = await fetchViaHTTP(context.appPort, `${locale}/urls`)
116116
const html = await res.text()
@@ -146,18 +146,39 @@ function urlTests(log, locale = '') {
146146
})
147147

148148
it('warns when using Response.redirect with a relative URL', async () => {
149-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-redirect`)
150-
expect(log.output).toContain(urlsWarning)
149+
const response = await fetchViaHTTP(
150+
context.appPort,
151+
`${locale}/urls/relative-redirect`
152+
)
153+
expect(await response.json()).toEqual({
154+
error: {
155+
message: expect.stringContaining(urlsError),
156+
},
157+
})
151158
})
152159

153160
it('warns when using NextResponse.redirect with a relative URL', async () => {
154-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-redirect`)
155-
expect(log.output).toContain(urlsWarning)
161+
const response = await fetchViaHTTP(
162+
context.appPort,
163+
`${locale}/urls/relative-next-redirect`
164+
)
165+
expect(await response.json()).toEqual({
166+
error: {
167+
message: expect.stringContaining(urlsError),
168+
},
169+
})
156170
})
157171

158-
it('warns when using NextResponse.rewrite with a relative URL', async () => {
159-
await fetchViaHTTP(context.appPort, `${locale}/urls/relative-next-rewrite`)
160-
expect(log.output).toContain(urlsWarning)
172+
it('throws when using NextResponse.rewrite with a relative URL', async () => {
173+
const response = await fetchViaHTTP(
174+
context.appPort,
175+
`${locale}/urls/relative-next-rewrite`
176+
)
177+
expect(await response.json()).toEqual({
178+
error: {
179+
message: expect.stringContaining(urlsError),
180+
},
181+
})
161182
})
162183
}
163184

0 commit comments

Comments
 (0)