Repository navigation
conneg: GET on container with index.html returns HTML to browsers (#409) - #410
Merged
Merged
Conversation
There was a problem hiding this comment.
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 containerindex.htmlbranch to only treat a request as wanting JSON-LD when theAcceptheader explicitly includesapplication/ld+jsonorapplication/json. - Add regression tests for browser-style
Acceptheaders and preserve explicit JSON-LD/Turtle extraction behavior forindex.htmlcontainers.
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 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 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. |
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.
melvincarvalho
force-pushed
the
fix/conneg-html-preference
branch
from
May 10, 2026 04:15
35137d6 to
9d361f3
Compare
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; |
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #409.
Summary
index.htmlcontaining a parseable<script type="application/ld+json">data island were gettingContent-Type: application/ld+jsoninstead of the HTML body.selectContentTypeshort-circuits on*/*and returnsJSON_LD. Every browser sendsAccept: text/html, …, */*;q=0.8, so the GET handler treated the request as wanting RDF and served the data island.explicitJsoncheck; 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 setwantsJsonLdwhen Accept explicitly namesapplication/ld+jsonorapplication/json:Turtle and explicit JSON-LD requests are unaffected — they take their respective branches as before.
Tests
Added a
#409describe block intest/conneg.test.jscovering:Accept: text/html, …, */*;q=0.8→text/html(regression)Accept: text/html→text/htmlAccept: 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:
Notes
selectContentTypeitself (it should not returnJSON_LDwhen text/html outranks*/*), but it has 5 call sites — at least one passes the result tofromJsonLdas 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.JSON.parsethrows, and the catch falls through to HTML.Test plan
node --test test/conneg.test.js— 50 passcurl -i -H 'Accept: text/html,…,*/*;q=0.8' /returnstext/html(wasapplication/ld+json)curl -i -H 'Accept: application/ld+json' /still returns JSON-LD