[pages-shared] fix: resolve relative link hrefs against base href in early hint Link headers#13779
Conversation
🦋 Changeset detectedLatest commit: 0563f51 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
…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.
a8c9307 to
1772aeb
Compare
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
There was a problem hiding this comment.
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>viaHTMLRewriterduring early hints link extraction. - Resolve relative link
hrefs against the captured base before appendingLinkheader 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.
… first <base> element
|
@NuroDev @penalosa @dario-piotrowicz — all Copilot review comments have been addressed. This PR is ready for review. |
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
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.
|
Fixed the CI failures:
CI should be green on the linting / checks now. |
|
Codeowners approval required for this PR:
Show detailed file reviewers |
|
Fixed the linting failures — the three new test assertions were using |
workers-devprod
left a comment
There was a problem hiding this comment.
Codeowners reviews satisfied
…early hint Link headers (#13779)
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.