Inconsistent behaviour of `request.nextUrl.href`
See original GitHub issueWhat version of Next.js are you using?
12.0.4
What version of Node.js are you using?
16.13.0
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
I’m attempting to use Middleware to force a default locale in my application URLs, using example code from the documentation.
This worked when accessing the application through localhost
, but when accessing using any other hostname, request.nextUrl.href
contains the full URL. I’m not sure whether this is expected behaviour, but it seems like it could be a bug as this breaks the example code provided in the documentation.
Expected Behavior
For request.nextUrl.href
to behave consistently. Either return the full URL, or the application path. Ideally the latter, as the example indicates this would be expected behaviour.
To Reproduce
Take this simple middleware:
import type { NextFetchEvent, NextRequest } from 'next/server'
export function middleware(request: NextRequest, event: NextFetchEvent) {
return new Response(request.nextUrl.href)
}
curl
the app using localhost, see the following:
$ curl http://localhost:3000/hello
/hello
But when using any other IP or hostname:
$ curl http://127.0.0.1:3000/hello
https://127.0.0.1:3000/hello
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5
Top GitHub Comments
This doesn’t seem to be fixed @Kikobeats? I am using next@12.0.7 and on Vercel
req.nextUrl.href
seems to be an absolute URL while it is a relative URL on LocalhostI am using: https://nextjs.org/docs/advanced-features/i18n-routing#prefixing-the-default-locale
Going to https://pycon-git-web-167-use-nextjs-intl-system-python-italia.vercel.app/ redirects to
https://pycon-git-web-167-use-nextjs-intl-system-python-italia.vercel.app/enhttps:/pycon-git-web-167-use-nextjs-intl-system-python-italia.vercel.app/default
This is the code:
More:
====
Logs from Vercel
req.nextUrl:
In case it helps anyone:
this is what I did to fix the middleware code:
req.nextUrl.pathname.replace("/default", "")
I agree that this issue is not closed out and was wrongly marked as such. I am still having inconsistent behavior with the request nextUrl object and even the request.url string. @Kikobeats