-
Notifications
You must be signed in to change notification settings - Fork 176
fix: Normalize the Location header in redirects #941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 6092aaf The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Confirmed it works as expected using your changes @sommeeeer: https://d13iq8evz1d8fc.cloudfront.net/about?param=test%2F%2F Very promising, thank you! 💪 |
713f78e
to
79493e4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits. Also, could you add unit test for the normalizeLocationHeader
function
I turned this into draft mode again. I need to do some more thouroghly testing as it seems I overlooked the fact that Next actually does not encode the search paramaters in the Location header coming from the middleware. Update: The PR is ready now we should be matching Next behavior now. They encode query params in redirects from Next Config, but not with |
add comment in middleware check status code fix e2e review add unit test for normalizeLocationHeader refactor to match Next behavior update unit test refactor comment refactor changeset
7bdf850
to
ada2e4b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks!!
Thanks for the review Nico! |
Currently in draft mode. It will pass all our E2E tests,
but requires more manual testing. Especially the middleware part.For #940 and opennextjs/opennextjs-cloudflare#809
For redirects coming from next config we want that the
Location
header value is encoded properly according to RFC. Innext start
and deployed to Vercel the header can also be relative or absolute. We should aim for the same behavior.I am a bit unsure what to do about
NextResponse.redirect()
coming from the middleware. Should we encode it aswell? I'll keep investigating this but are open for suggestions.Update: I've done a lot of manual testing now, and made some improvements along the way. We seem to be matching the behavior of
next start
now.They do encode redirects from the middleware aswell. They also normalize it.It only accepts absolute URLs aswell. Doesn't matter if you useResponse.redirect()
,NextResponse.redirect()
or create your ownnew Response(null, ...
. The behavior seems to be the same.Interesting enough if you have this
Response
in the middleware it will crash withTypeError: Invalid URL
:With
NextResponse.redirect
andResponse.redirect
it will try to validate the URL with this function in Next.js: validateURL. You will get sent to this docs page explaining you need to use absolute URLs: https://nextjs.org/docs/messages/middleware-relative-urls