ReviewOS

stacks/stx

generateDocumentShell does not dedup config meta against its own hardcoded charset/viewport (injectConfigHeadTags does)

#1840
Closed glennmichael123 opened this 22 days ago · 1 comment
22 days ago

Two functions in packages/stx/src/document-shell.ts merge config head tags into a page. One dedups. The other does not.

generateDocumentShell (:396-402) prepends a hardcoded pair and then straight-concatenates whatever config supplies:

const defaultMeta = [
  { charset: 'UTF-8' },
  { name: 'viewport', content: 'width=device-width, initial-scale=1.0' },
]
const allMeta = [...defaultMeta, ...meta]

injectConfigHeadTags (same file, ~:576-598) does the opposite — it builds a dedup key from name= / property= / charset= / http-equiv= and skips anything already in the head. Its docblock even advertises this: "Idempotent: skips a <script src>, <link href>, meta name/property/charset/http-equiv…".

Result

Declaring charset or viewport in app.head.meta — the obvious place to put site-wide head tags — emits two <meta charset> and two <meta name="viewport"> on every page that goes through the shell, while pages topped up by injectConfigHeadTags correctly get one. Measured on a real app; it is why stacksjs/analyticshq's config/ui.ts carries a comment telling the next person not to declare them there.

The failure is silent and shape-dependent: the same config produces different head output depending on whether the page owns its document or not.

Fix

Apply the existing dedup in generateDocumentShell too — the key-building logic in injectConfigHeadTags already handles charset, so it can be extracted and shared rather than reimplemented.

22 days ago

Fixed in 2aea676, taking the suggested route — the dedup key is extracted from injectConfigHeadTags and shared, rather than reimplemented.

Measured on the reported case:

AT HEAD  | charset x2 | viewport x2
NOW      | charset x1 | viewport x1

One decision worth surfacing

Which wins? I made config win over the framework default, not the other way round.

That mirrors injectConfigHeadTags, where the page's own head wins over config — in both cases the more specific, author-authored source beats the generic one. Deduping to the wrong side would have been its own bug: an author writing charset: 'ISO-8859-1' asked for that value, and silently serving UTF-8 instead is a worse outcome than the duplicate was.

config charset ISO-8859-1  ->  <meta charset="ISO-8859-1">   (one, configured value)
config viewport width=500  ->  content="width=500"            (one, configured value)
config declares neither    ->  both defaults, as before

Tests

packages/stx/test/shell/config-meta-dedup.test.ts covers the counts, the value precedence, unrelated meta being undisturbed, and the key builder itself — including that charset keys by presence rather than value, which is precisely why the duplicate was harmful rather than merely untidy.

The load-bearing one asserts the actual invariant: the same config produces one of each whichever path built the page, so the two can't drift apart again.

Full suite 10772 pass in a clean worktree; the one failure is the pre-existing crosswind-arbitrary-values case.

Worth noting your closing observation — that the failure is silent and shape-dependent — is the reason this was worth fixing properly rather than just deleting the hardcoded pair: a page that owns its document still needs the defaults when config supplies nothing.

Sign in to comment on this issue.