Skip to content

Commit 1f65018

Browse files
committed
Remove deprecation for relative URL usage in middlewares
1 parent ba78437 commit 1f65018

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

errors/middleware-relative-urls.md

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,18 +149,13 @@ 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'
163-
)
164-
return String(url)
158+
error.message = `URLs is malformed. Please use only absolute URLs - https://nextjs.org/docs/messages/middleware-relative-urls\n${error.message}`
159+
throw error
165160
}
166161
}

0 commit comments

Comments
 (0)