Skip to content

[pages-shared] fix: resolve relative link hrefs against base href in early hint Link headers#13779

Merged
dario-piotrowicz merged 5 commits into
cloudflare:mainfrom
matingathani:fix/pages-link-header-base-href
May 21, 2026
Merged

[pages-shared] fix: resolve relative link hrefs against base href in early hint Link headers#13779
dario-piotrowicz merged 5 commits into
cloudflare:mainfrom
matingathani:fix/pages-link-header-base-href

Conversation

@matingathani

@matingathani matingathani commented May 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #7181.

When serving HTML pages through Cloudflare Pages with a `` element, the automatic Link response header generation for early hints did not resolve relative link hrefs against the document's base URL.

For example, a page with `` and `` generated `Link: <module.js>; rel=modulepreload` instead of the correct `Link: </module.js>; rel=modulepreload`.

Fix: Added a `base[href]` handler to the HTMLRewriter to extract the base URL, then resolve relative hrefs against it before building the Link header values.


  • Tests
    • Tests included/updated
  • Public documentation
    • Documentation not necessary because: This is a bug fix for internal early hints Link header generation; no user-facing docs change required.

Copilot AI review requested due to automatic review settings May 1, 2026 21:02
@changeset-bot

changeset-bot Bot commented May 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0563f51

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

This PR includes changesets to release 1 package
Name Type
@cloudflare/pages-shared 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

@workers-devprod workers-devprod requested review from a team and edmundhung and removed request for a team and Copilot May 1, 2026 21:03
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/fix-pages-link-header-base-href.md: [@cloudflare/wrangler]
  • packages/pages-shared/tests/asset-server/handler.test.ts: [@cloudflare/wrangler]
  • packages/pages-shared/asset-server/handler.ts: [@cloudflare/wrangler]

…n generating early hint Link headers

Fixes cloudflare#7181

Previously, the automatic Link header generation for Pages early hints
extracted href attributes from <link> elements as-is, without resolving
relative URLs against the document's <base href>. This caused incorrect
preload/preconnect URLs when a page used a <base> element.

Add a base[href] handler to the HTMLRewriter and resolve relative
hrefs against the extracted base before building the Link header.
@matingathani matingathani force-pushed the fix/pages-link-header-base-href branch from a8c9307 to 1772aeb Compare May 6, 2026 22:00
Copilot AI review requested due to automatic review settings May 6, 2026 22:00
@workers-devprod

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/wrangler
Show detailed file reviewers
  • .changeset/fix-pages-link-header-base-href.md: [@cloudflare/wrangler]
  • packages/pages-shared/tests/asset-server/handler.test.ts: [@cloudflare/wrangler]
  • packages/pages-shared/asset-server/handler.ts: [@cloudflare/wrangler]

Copilot AI left a comment

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.

Pull request overview

Fixes Cloudflare Pages early hints automatic Link header generation to respect the document’s <base href> when resolving relative <link ... href> values.

Changes:

  • Capture <base href> via HTMLRewriter during early hints link extraction.
  • Resolve relative link hrefs against the captured base before appending Link header values.
  • Add a regression test and a changeset entry for @cloudflare/pages-shared.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/pages-shared/asset-server/handler.ts Adds <base href> capture and applies it when building early hints Link headers.
packages/pages-shared/tests/asset-server/handler.test.ts Adds a regression test asserting base-href-aware resolution in cached early hints headers.
.changeset/fix-pages-link-header-base-href.md Declares a patch release note for the behavior fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/pages-shared/asset-server/handler.ts Outdated
Comment thread packages/pages-shared/asset-server/handler.ts Outdated
Comment thread packages/pages-shared/__tests__/asset-server/handler.test.ts
@matingathani

Copy link
Copy Markdown
Contributor Author

@NuroDev @penalosa @dario-piotrowicz — all Copilot review comments have been addressed. This PR is ready for review.

@pkg-pr-new

pkg-pr-new Bot commented May 19, 2026

Copy link
Copy Markdown
create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@13779

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@13779

miniflare

npm i https://pkg.pr.new/miniflare@13779

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@13779

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@13779

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@13779

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@13779

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@13779

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@13779

wrangler

npm i https://pkg.pr.new/wrangler@13779

commit: 0563f51

@dario-piotrowicz

Copy link
Copy Markdown
Member

Hi @matingathani thanks for the PR sorry for only now noticing this now

The CI seems quite unhappy and there's also some linting failures, could you have a look at these? 🙏

…ests

Replace `earlyHintsRes!` non-null assertions (flagged by oxlint's
no-non-null-assertion rule) with explicit `if (!earlyHintsRes) throw`
guards, consistent with the pattern used in other tests in the same file.
@matingathani

Copy link
Copy Markdown
Contributor Author

Fixed the CI failures:

  • oxlint no-non-null-assertion errors: The three new tests used earlyHintsRes! non-null assertions. Replaced with explicit if (!earlyHintsRes) throw new Error(...) guards, consistent with the pattern used by existing tests in the same file.
  • PR description validation: Added the required checkboxes for tests and documentation to the PR description.

CI should be green on the linting / checks now.

@workers-devprod

workers-devprod commented May 20, 2026

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • ✅ @cloudflare/wrangler
Show detailed file reviewers

@matingathani

Copy link
Copy Markdown
Contributor Author

Fixed the linting failures — the three new test assertions were using ! non-null assertion which oxlint bans (@typescript-eslint/no-non-null-assertion). Replaced with the guard pattern used elsewhere in the same file. Also updated the PR description to include the required template checkboxes.

@dario-piotrowicz dario-piotrowicz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@workers-devprod workers-devprod left a comment

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.

Codeowners reviews satisfied

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk May 21, 2026
@dario-piotrowicz dario-piotrowicz merged commit 416857c into cloudflare:main May 21, 2026
61 of 62 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk May 21, 2026
penalosa pushed a commit that referenced this pull request May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🐛 BUG: Automatic Link header generation does not take into account base URL

4 participants