-
Notifications
You must be signed in to change notification settings - Fork 143
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
refactor: internalEvent.url is a full URL #752
Conversation
🦋 Changeset detectedLatest commit: 9ffa41a 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: |
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.
All internal redirect are failing in e2e because of the incorrect url created.
One thing I need to check is the base path, we should probably add it when constructing URLs (i.e. 404, 500)
Yeah good point, we should probably create a helper functions for these cases
return { | ||
type: "core", | ||
method: httpMethod, | ||
rawPath: path, | ||
url: path + normalizeAPIGatewayProxyEventQueryParams(event), | ||
url: `https://${headers.host ?? "on"}${path}${normalizeAPIGatewayProxyEventQueryParams(event)}`, |
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.
For all aws converters, the host will not be correct here.
The correct host is in the x-forwarded-host
header, we apply it but only after the converter has already run :
opennextjs-aws/packages/open-next/src/core/requestHandler.ts
Lines 42 to 44 in 674dce6
if (initialHeaders["x-forwarded-host"]) { | |
initialHeaders.host = initialHeaders["x-forwarded-host"]; | |
} |
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.
Ok I'll apply the change in aws converters.
Could the code in request handler (your link) be deleted then?
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.
Honestly i'm not sure how to handle this one properly. We need the headers to be updated, but only if there is a x-forwarded-host
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.
This could be done in the converter as it is aws specific, no?
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.
Not entirely, this is kind of a standard https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host.
And this will likely cause trouble when used with the node
converter and wrapper as well.
Maybe a better solution would be to modify the url in the request handler as well. If we have a x-forwarded-host
we change both the host header and the url to use this header.
We may have to move this to the routing handler though (in case of an external middleware)
url: `${rewrittenUrl}${convertToQueryString(finalQuery)}`, | ||
url: new URL( | ||
`${rewrittenUrl}${convertToQueryString(finalQuery)}`, | ||
new URL(event.url), |
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.
Here it fails in e2e because event.url
is incorrect.
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.
Do you know what test fails?
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.
test("Missing cookies", async ({ page }) => { |
test("Has cookies", async ({ page }) => { |
test("Has cookies with value", async ({ page }) => { |
And these 2
opennextjs-aws/packages/tests-e2e/tests/pagesRouter/redirect.test.ts
Lines 11 to 25 in 674dce6
test("Redirect with default locale support", async ({ page }) => { | |
await page.goto("/redirect-with-locale/"); | |
await page.waitForURL("/ssr/"); | |
const el = page.getByText("SSR"); | |
await expect(el).toBeVisible(); | |
}); | |
test("Redirect with locale support", async ({ page }) => { | |
await page.goto("/nl/redirect-with-locale/"); | |
await page.waitForURL("/nl/ssr/"); | |
const el = page.getByText("SSR"); | |
await expect(el).toBeVisible(); | |
}); |
@@ -62,7 +62,7 @@ async function convertFromAPIGatewayProxyEvent( | |||
type: "core", | |||
method: httpMethod, | |||
rawPath: path, | |||
url: `https://${headers.host ?? "on"}${path}${normalizeAPIGatewayProxyEventQueryParams(event)}`, | |||
url: `https://${headers["x-forwarded-host"] ?? "on"}${path}${normalizeAPIGatewayProxyEventQueryParams(event)}`, |
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.
This is not 100% correct either, we could create an helper function that would retrieve the host from the headers.
If this header is not set (which can be the case), we should fallback to the host
header
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.
Do you mean an helper to headers["x-forwarded-host"] ?? headers.on ?? "on"
?
For all aws converters?
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.
That was my original idea, but i think there is a better solution (#752 (comment)). In the converters we keep headers.host ?? "on"
, but in the routing handler if we have a x-forwarded-host
header, we change both the host header and the internalEvent.url
I have pushed some commits to integrate the feedback. @conico974 Could you please run the test in debug mode and report the debug("rewrittenUrl", { rewrittenUrl, finalQuery, isUsingParams }); Thanks! |
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.
I've run the e2e test and everything is working as expected.
LGTM Thanks
Thanks a lot @conico974, I will double check the basePath thing before merging. |
I tested using a |
fixes #719
edit: One thing I need to check is the base path, we should probably add it when constructing URLs (i.e. 404, 500)
@conico974 could you please review and maybe run the aws integration tests when this looks ok?
cf tests are green with this PR.
Thanks!