Skip to content

Fix pages router context #607

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

Merged
merged 3 commits into from
Apr 28, 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
5 changes: 5 additions & 0 deletions .changeset/twelve-turtles-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/cloudflare": patch
---

fix page router shared context to use the correct one
13 changes: 13 additions & 0 deletions examples/playground14/e2e/head.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test, expect } from "@playwright/test";

test.describe("head properly populated", () => {
test("should properly populate the <head>", async ({ page }) => {
await page.goto("/head");
const title = await page.title();
const description = await page.locator('meta[name="description"]').getAttribute("content");
const favicon = await page.locator('link[rel="icon"]').getAttribute("href");
expect(title).toBe("SSR Head");
expect(description).toBe("SSR");
expect(favicon).toBe("/favicon.ico");
});
});
23 changes: 23 additions & 0 deletions examples/playground14/pages/head.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Head from "next/head";

export function getServerSideProps() {
return {
props: {
time: new Date().toISOString(),
},
};
}
export default function Page({ time }) {
return (
<div>
<Head>
<title>SSR Head</title>
<meta name="description" content="SSR" />
<link rel="icon" href="/favicon.ico" />
</Head>
<div className="flex" data-testid="time">
Time: {time}
</div>
</div>
);
}
13 changes: 13 additions & 0 deletions examples/playground15/e2e/head.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { test, expect } from "@playwright/test";

test.describe("head properly populated", () => {
test("should properly populate the <head>", async ({ page }) => {
await page.goto("/head");
const title = await page.title();
const description = await page.locator('meta[name="description"]').getAttribute("content");
const favicon = await page.locator('link[rel="icon"]').getAttribute("href");
expect(title).toBe("SSR Head");
expect(description).toBe("SSR");
expect(favicon).toBe("/favicon.ico");
});
});
23 changes: 23 additions & 0 deletions examples/playground15/pages/head.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Head from "next/head";

export function getServerSideProps() {
return {
props: {
time: new Date().toISOString(),
},
};
}
export default function Page({ time }) {
return (
<div>
<Head>
<title>SSR Head</title>
<meta name="description" content="SSR" />
<link rel="icon" href="/favicon.ico" />
</Head>
<div className="flex" data-testid="time">
Time: {time}
</div>
</div>
);
}
3 changes: 2 additions & 1 deletion examples/playground15/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"name": "next"
}
],
"target": "ES2017"
"target": "ES2017",
"strictNullChecks": true
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx", "worker-configuration.d.ts"],
"exclude": ["node_modules", "open-next.config.ts"]
Expand Down
2 changes: 2 additions & 0 deletions packages/cloudflare/src/cli/build/bundle-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { inlineFindDir } from "./patches/plugins/find-dir.js";
import { patchInstrumentation } from "./patches/plugins/instrumentation.js";
import { inlineLoadManifest } from "./patches/plugins/load-manifest.js";
import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js";
import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js";
import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js";
import { fixRequire } from "./patches/plugins/require.js";
import { shimRequireHook } from "./patches/plugins/require-hook.js";
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function bundleServer(buildOpts: BuildOptions): Promise<void> {
fixRequire(updater),
handleOptionalDependencies(optionalDependencies),
patchInstrumentation(updater, buildOpts),
patchPagesRouterContext(buildOpts),
inlineEvalManifest(updater, buildOpts),
inlineFindDir(updater, buildOpts),
inlineLoadManifest(updater, buildOpts),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* ESBuild plugin to handle pages router context.
*
* We need to change the import path for the pages router context to use the one provided in `pages-runtime.prod.js`
*/

import { BuildOptions, compareSemver } from "@opennextjs/aws/build/helper.js";
import type { OnResolveResult, PluginBuild } from "esbuild";

export function patchPagesRouterContext(buildOpts: BuildOptions) {
const pathRegex = /^.*\/(?<CONTEXT>.*)\.shared-runtime$/;
const isAfter15 = compareSemver(buildOpts.nextVersion, ">=", "15.0.0");
const isAfter153 = compareSemver(buildOpts.nextVersion, ">=", "15.3.0");
const basePath = `next/dist/server/${isAfter15 ? "" : "future/"}route-modules/pages/vendored/contexts/`;
return {
name: "pages-router-context",
setup: (build: PluginBuild) => {
// If we are after 15.3, we don't need to patch the context anymore
if (isAfter153) {
return;
}
// We need to modify some imports (i.e. https://github.com/vercel/next.js/blob/48540b836642525b38a2cba40a92b4532c553a52/packages/next/src/server/require-hook.ts#L59-L68)
build.onResolve(
{ filter: /.*shared-runtime/ },
async ({ path, resolveDir, ...options }): Promise<OnResolveResult | undefined> => {
const match = path.match(pathRegex);
if (match && match.groups?.CONTEXT) {
const newPath = `${basePath}${match.groups.CONTEXT}.js`;
return await build.resolve(newPath, {
resolveDir,
...options,
});
}
}
);
},
};
}