Skip to content

fix(pages-router): Add patch for trustHostHeader using res.revalidate#1109

Draft
sommeeeer wants to merge 8 commits intoopennextjs:mainfrom
sommeeeer:fix-pages-router-revalidate
Draft

fix(pages-router): Add patch for trustHostHeader using res.revalidate#1109
sommeeeer wants to merge 8 commits intoopennextjs:mainfrom
sommeeeer:fix-pages-router-revalidate

Conversation

@sommeeeer
Copy link
Copy Markdown
Collaborator

Closes #1102

res.revalidate("/path") is broken on Next 15 and 16 for pages router. The reason is that this part of the revalidate code in Next will be undefined in our case.

This code ends up bundled in /next/dist/compiled/next-server/pages-api.runtime.prod.js. I tried many other ways to solve this that failed:

  • Setting this env variable during build.
  • I tried setting experimental.trustHostHeader in Next config.
  • Also as a last try I tried passing trustHostHeader many places in new NextServer.default({});. Didn't seem to have any effect at all.

Not sure why none of these workarounds did the trick, but atleast patching the bundled code in the runtime module seems to do the trick.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 6, 2026

🦋 Changeset detected

Latest commit: 3492764

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 6, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/aws@1109

commit: 3492764

await this.updateTagsOnSet(key, data, ctx);
debug("Finished setting cache");
} catch (e) {
console.log("ARE WE HERE");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev logs?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, nice catch :) thanks

Copy link
Copy Markdown
Collaborator Author

@sommeeeer sommeeeer Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that while making this I found another bug. You can trigger it like this:

cd examples/pages-router
pnpm openbuild:local && pnpm openbuild:local:start

Then in your browser open http://localhost:3002/revalidate/123 and see the error in the terminal.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Copy Markdown
Contributor

@khuezy khuezy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I'll defer to Conico/Victor since I haven't dealt w/ page router in years.

@sommeeeer
Copy link
Copy Markdown
Collaborator Author

Thanks for the review @khuezy

Comment thread examples/pages-router/src/pages/revalidate/[key].tsx Outdated
Comment thread examples/pages-router/src/pages/revalidate/[key].tsx
Comment thread packages/tests-e2e/tests/pagesRouter/revalidate.test.ts Outdated
Comment thread examples/pages-router/src/pages/revalidate/[key].tsx
Comment thread packages/tests-e2e/tests/pagesRouter/revalidate.test.ts
@sommeeeer sommeeeer requested a review from conico974 February 7, 2026 19:41
@sommeeeer
Copy link
Copy Markdown
Collaborator Author

Thanks for the review Nico! I have addressed your comments, and this PR should be ready to ship now.

When it comes to this error its coming from here. It won't find that path since it doesn't really exist. Thus resulting in the error message from the image. It can probably be ignored? This is whats calling it. data is null here if you try to access a path that was not generated at build time.

--- pages-api.runtime.prod.js
+++ pages-api.runtime.prod.js
@@ -1,5 +1,4 @@
-
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the diff here?

Copy link
Copy Markdown
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it used to work, trying to understand why not any more

it is no more experimental?

Oh maybe I was only considering cloudflare and we have this and this

Maybe we should dig a little and check if there is a way to factor code for both aws and cloudflare?

WDYT Magnus, you have look at it more deeply

export const trustHostHeaderRule = `
rule:
kind: member_expression
pattern: $CONTEXT.trustHostHeader
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there are 3 occurences of context.tustHostHeader in this function, what is the rationale for only updating one?

inside:
kind: parenthesized_expression
inside:
kind: if_statement
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look at making this simpler.
If not possible, it would be nice to add what code we are looking for in the JSDoc

`;

// Use correct protocol when doing HEAD fetch for revalidation
export const headFetchProtocolRule = `
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here, could you please add what snippet we are tryin to match. We might be able to come with a more readable rule too.

export const patchPagesApiRuntimeProd: CodePatcher = {
name: "patch-pages-api-runtime-prod",
patches: [
// Trust the host header when invoking `res.revalidate("") from pages router
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to only comment on trustHostHeaderRule ?

Suggested change
// Trust the host header when invoking `res.revalidate("") from pages router

patchCode: createPatchCode(trustHostHeaderRule),
versions: ">=15.0.0",
},
// Use correct protocol when doing HEAD fetch for revalidation
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
// Use correct protocol when doing HEAD fetch for revalidation

@sommeeeer sommeeeer marked this pull request as draft February 10, 2026 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Failed to revalidate /path: Invariant: missing internal router-server-methods this is an internal bug

4 participants