Skip to content

Commit

Permalink
fix: host not included in route handler urls
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Jan 8, 2025
1 parent 0c45b0b commit 94c515a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
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"]),
...(isAfter141 ? ["experimentalIncrementalCacheHandler"] : ["stableIncrementalCache"]),
],
}),
Expand Down

0 comments on commit 94c515a

Please sign in to comment.