Skip to content
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

fix: host not included in route handler urls #234

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/short-cats-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

fix: host not included in route handler urls

Next.js was unable to re-construct the correct URLs for the request in a route handler due to being unable to retrieve the hostname. This was due to the internal Next.js option `trustHostHeader` being disabled in OpenNext when there is external middleware - this option is needed for the Next.js server in our environment.
5 changes: 5 additions & 0 deletions examples/api/app/api/request/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextRequest } from "next/server";

export const GET = (request: NextRequest) => {
return new Response(JSON.stringify({ nextUrl: request.nextUrl.href, url: request.url }));
};
7 changes: 7 additions & 0 deletions examples/api/e2e/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ test("sets environment variables from the Next.js env file", async ({ page }) =>
const res = await page.request.get("/api/env");
await expect(res.json()).resolves.toEqual(expect.objectContaining({ TEST_ENV_VAR: "TEST_VALUE" }));
});

test("returns correct information about the request from a route handler", async ({ page }) => {
const res = await page.request.get("/api/request");
// Next.js can fall back to `localhost:3000` or `n` if it doesn't get the host - neither of these are expected.
const expectedURL = expect.stringMatching(/https?:\/\/localhost:(?!3000)\d+\/api\/request/);
await expect(res.json()).resolves.toEqual({ nextUrl: expectedURL, url: expectedURL });
});
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ async function generateBundle(
target: /core(\/|\\)util\.js/g,
deletes: [
...(disableNextPrebundledReact ? ["requireHooks"] : []),
...(disableRouting ? ["trustHostHeader"] : []),
...(!isBefore13413 ? ["requestHandlerHost"] : []),
...(isBefore13413 ? ["trustHostHeader"] : ["requestHandlerHost"]),
james-elicx marked this conversation as resolved.
Show resolved Hide resolved
...(isAfter141 ? ["experimentalIncrementalCacheHandler"] : ["stableIncrementalCache"]),
],
}),
Expand Down
Loading