Skip to content

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

Closed

Description

@melvincarvalho

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:

  1. 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;
  2. Fix selectContentType directly so that text/html/application/xhtml+xml ranked above */* returns the HTML type (or null), and audit the 5 call sites — at least one (line 502) passes the result to fromJsonLd as 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 in selectContentType)
  • Node 22.21.x / 22.22.x
  • Single-user mode, container has index.html with a well-formed JSON-LD data island

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions