Repository navigation
Description
Summary
When --conneg is enabled and a container has an index.html containing a valid <script type="application/ld+json"> data island, GET requests from a browser receive application/ld+json instead of text/html. The HEAD handler returns the right content type because it has a workaround that the GET handler is missing.
Reproduction
Deploy jss 0.0.179 with --conneg --mashlib-module /mashlib.js and a root container whose index.html contains a parseable JSON-LD data island, then:
curl -i -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' https://example.com/Expected: Content-Type: text/html (the index.html body).
Actual: Content-Type: application/ld+json; charset=utf-8 (the extracted data island).
A curl -I (HEAD) on the same URL returns text/html — confirming GET and HEAD disagree.
Root cause
src/rdf/conneg.js — selectContentType() iterates the parsed Accept list and short-circuits to RDF_TYPES.JSON_LD on */*:
for (const { type } of accepts) {
if (type === '*/*' || type === 'application/*') {
return RDF_TYPES.JSON_LD;
}
if (SUPPORTED_OUTPUT.includes(type)) {
return type;
}
if (type === 'text/*') {
return RDF_TYPES.TURTLE;
}
}text/html and application/xhtml+xml are not recognized, so the function falls through to the */* branch and reports the client wants JSON-LD — even though text/html had higher q.
Why HEAD works but GET doesn't
src/handlers/resource.js (HEAD branch ~line 600) already guards against this:
} else if (wantsJsonLd) {
// For an index.html container, only override to JSON-LD if the
// Accept header explicitly asked for JSON; otherwise fall back
// to text/html so HEAD matches the index.html that GET serves.
const explicitJson = /\b(application\/ld\+json|application\/json)\b/i.test(acceptHeader);
contentType = (indexExists && !explicitJson) ? 'text/html' : 'application/ld+json';The GET branch (~line 164) doesn't have the same explicitJson guard, so it follows selectContentType's answer and serves the data island.
Suggested fix
Either:
-
Mirror the HEAD guard in the GET branch (minimal, matches existing intent):
const explicitJson = /\b(application\/ld\+json|application\/json)\b/i.test(acceptHeader); const wantsJsonLd = negotiated === RDF_TYPES.JSON_LD && explicitJson;
-
Fix
selectContentTypedirectly so thattext/html/application/xhtml+xmlranked above*/*returns the HTML type (or null), and audit the 5 call sites — at least one (line 502) passes the result tofromJsonLdas a target type, so a returned'text/html'would need to be handled there.
(1) is a 2-line change and obviously correct given the HEAD precedent; (2) is the proper long-term fix.
Environment
- jss
0.0.179(also reproducible at 0.0.164 — same code inselectContentType) - Node 22.21.x / 22.22.x
- Single-user mode, container has
index.htmlwith a well-formed JSON-LD data island
Activity
conneg: GET on container with index.html returns HTML to browsers (#409)
review: hoist EXPLICIT_JSON_RE + add HEAD/GET parity tests (#409)
conneg: GET on container with index.html returns HTML to browsers (#409…