Skip to content

conneg: GET on container with index.html returns HTML to browsers (#409) - #410

Merged
melvincarvalho merged 3 commits into
gh-pagesfrom
fix/conneg-html-preference
May 10, 2026
Merged

melvincarvalho merged 3 commits into
gh-pagesfrom
fix/conneg-html-preference

Conversation

@melvincarvalho

Copy link
Copy Markdown
Contributor

Closes #409.

Summary

  • Browsers requesting a container that has an index.html containing a parseable <script type="application/ld+json"> data island were getting Content-Type: application/ld+json instead of the HTML body.
  • Root cause: selectContentType short-circuits on */* and returns JSON_LD. Every browser sends Accept: text/html, …, */*;q=0.8, so the GET handler treated the request as wanting RDF and served the data island.
  • The HEAD handler (~line 600) already guards against this with an explicitJson check; GET just hadn't been brought in line. This PR mirrors that guard.

Change

src/handlers/resource.js — in the container/index.html branch, only set wantsJsonLd when Accept explicitly names application/ld+json or application/json:

const explicitJson = /\b(application\/ld\+json|application\/json)\b/i.test(acceptHeader);
const wantsJsonLd = negotiated === RDF_TYPES.JSON_LD && explicitJson;

Turtle and explicit JSON-LD requests are unaffected — they take their respective branches as before.

Tests

Added a #409 describe block in test/conneg.test.js covering:

  • Accept: text/html, …, */*;q=0.8 → text/html (regression)
  • Accept: text/html → text/html
  • Accept: application/ld+json → JSON-LD from data island (preserved)
  • Accept: text/turtle → Turtle from data island (preserved)

Stash-and-rerun confirms the new tests fail without the fix and pass with it. Full suite is green:

ℹ tests 50
ℹ pass 50
ℹ fail 0

Notes

  • The deeper fix is in selectContentType itself (it should not return JSON_LD when text/html outranks */*), but it has 5 call sites — at least one passes the result to fromJsonLd as a target type, so changing the return contract needs a wider audit. This PR takes the same minimal, intent-matching approach already used in the HEAD handler. Happy to follow up with a broader refactor if desired.
  • Found while debugging melvincarvalho.com, which had a well-formed data island. melvin.me hides the bug only because its data island has a trailing comma — JSON.parse throws, and the catch falls through to HTML.

Test plan

  • node --test test/conneg.test.js — 50 pass
  • Manual repro: curl -i -H 'Accept: text/html,…,*/*;q=0.8' / returns text/html (was application/ld+json)
  • Manual: curl -i -H 'Accept: application/ld+json' / still returns JSON-LD

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes content negotiation for containers that have an index.html with an embedded JSON-LD data island, ensuring browser-like Accept: text/html, …, */*;q=… requests receive the HTML document instead of the extracted JSON-LD (aligning GET behavior with the existing HEAD logic).

Changes:

  • Update handleGet’s container index.html branch to only treat a request as wanting JSON-LD when the Accept header explicitly includes application/ld+json or application/json.
  • Add regression tests for browser-style Accept headers and preserve explicit JSON-LD/Turtle extraction behavior for index.html containers.

Reviewed changes

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

File Description
src/handlers/resource.js Adds an explicit-JSON guard so browser Accept headers don’t get diverted into the JSON-LD data-island branch.
test/conneg.test.js Adds regression coverage for index.html containers under browser-like Accept headers plus explicit JSON-LD/Turtle cases.

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

Comment thread src/handlers/resource.js Outdated
Comment on lines +165 to +167
// otherwise selectContentType's `*/*` fallback wrongly diverts
// browser GETs (which include `*/*;q=0.8`) into the RDF branch and
// serves the embedded data island instead of the index.html body.
Comment thread test/conneg.test.js Outdated
Comment on lines +685 to +687
// selectContentType's `*/*` fallback short-circuits before it sees
// text/html. Browsers send `Accept: text/html, ..., */*;q=0.8`, so
// the user-visible page silently flipped to JSON.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

selectContentType's `*/*` short-circuit reported JSON-LD wanted whenever
Accept contained `*/*` — which every browser sends as a low-q fallback
behind text/html — so a container with a parseable
`<script type="application/ld+json">` data island in its index.html
silently flipped to JSON for plain page loads.

Fix: only treat the request as wanting JSON-LD when Accept *explicitly*
mentions application/ld+json or application/json. Mirrors the existing
HEAD-handler guard so GET and HEAD agree (#325 expected this; the GET
side just hadn't been brought into line).

Confirmed against melvincarvalho.com which had a well-formed data island
and was returning JSON to browsers. melvin.me wasn't affected only
because its data island had a trailing comma — JSON.parse threw, the
catch fell through to HTML, and the bug stayed hidden.

Tests in conneg.test.js (#409 block):
  - browser Accept (text/html + */*;q=0.8) → text/html
  - Accept: text/html → text/html
  - Accept: application/ld+json → JSON-LD from data island (preserved)
  - Accept: text/turtle → Turtle from data island (preserved)
…pped

selectContentType walks the parsed Accept list in q-order. text/html
and application/xhtml+xml are seen first; they just aren't recognized
(they're not in SUPPORTED_OUTPUT, application/*, or text/*), so the
loop walks past them and lands on `*/*`, which returns JSON-LD.

Earlier wording said the */* fallback "short-circuits before it sees
text/html" — that's not how the parser works. Fix the wording in both
the resource.js comment and the test description.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread src/handlers/resource.js
Comment on lines +164 to +173
// Only treat as JSON-LD when Accept *explicitly* asks for JSON.
// selectContentType doesn't recognize text/html or
// application/xhtml+xml, so for a browser Accept like
// `text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8`
// it walks past those unsupported types and lands on `*/*`, which
// returns JSON-LD — diverting plain browser GETs into the RDF
// branch and serving the embedded data island instead of the
// index.html body. Mirrors the HEAD-handler logic below (#409).
const explicitJson = /\b(application\/ld\+json|application\/json)\b/i.test(acceptHeader);
const wantsJsonLd = negotiated === RDF_TYPES.JSON_LD && explicitJson;
Comment thread test/conneg.test.js
const body = await res.text();
assert.ok(body.includes('<h1>hello</h1>'), 'response should be the index.html body');
});

Per Copilot review on PR #410:

1. The explicit-JSON regex was duplicated in handleGet (container/index.html
   branch) and handleHead (the existing #325 guard). Hoist it to a
   module-level EXPLICIT_JSON_RE so the two branches can't drift apart
   silently — e.g. someone adding application/vnd.api+json support in one
   place and not the other.

2. The new #409 tests covered GET only. The original bug was specifically
   GET-vs-HEAD divergence (HEAD already had the guard, GET didn't), so add
   a small parity loop that runs the same Accept headers through both
   methods on the same index.html container and asserts equal Content-Type.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@melvincarvalho
melvincarvalho merged commit 2f4099a into gh-pages May 10, 2026
4 checks passed
@melvincarvalho
melvincarvalho deleted the fix/conneg-html-preference branch May 10, 2026 04:25
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.

conneg: GET / on container with index.html returns JSON-LD to browsers (selectContentType prefers */* over text/html)

2 participants