Skip to content

fix: refresh injectStyle sheets when HMR keeps the hash - #957

Merged
trueadm merged 1 commit into
mainfrom
cursor/inject-style-hmr-reinject-645d
Sep 5, 2026
Merged

trueadm merged 1 commit into
mainfrom
cursor/inject-style-hmr-reinject-645d

Conversation

@jonkwheeler

@jonkwheeler jonkwheeler commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #938. injectStyle(id, css) on the client now refreshes an already-owned <style data-octane> when a later call arrives with the same id and different CSS (HMR of a later block in a multi-block scope). A server-emitted sheet seen for the first time is still adopted untouched, and the no-DOM path still records the id in _injectedStyles without creating a tag.

Base verification: feat/scoped-styles-apply already contains this contract as part of #939 (the it.fails pin was flipped there in e8ac8adb5). main still had first-wins-only dedupe and no style-hmr-reinject suite. This PR lands only the isolated runtime fix on main so #938 can close independently of the scoped-styles RFC.

Why

The compiled scope hash is derived from the first block's position and content. Editing a later block under HMR keeps the hash, so injectStyle(id, css1); injectStyle(id, css2) used to leave css1 in the document.

Changes

  • Client injectStyle: if style[data-octane=id] exists and this runtime already owns it, replace textContent when it differs. Otherwise adopt (SSR) or record the id (no document).
  • Add packages/octane/tests/runtime/style-hmr-reinject.test.ts with identical-repeat, server-adopt first-wins, HMR replace, and no-DOM first-wins.
  • Patch changeset for octane.

No content-hash-in-id change (plan D12); not required for this fix.

Validation

  • pnpm format:check (not run; used scoped format)
  • pnpm typecheck:files on the two changed source files
  • pnpm test (full suite not run)
  • targeted tests: packages/octane/tests/runtime/style-hmr-reinject.test.ts (dev + octane-prod), ssr-control.test.ts, style.test.ts

Before the runtime change, the HMR case failed (kept css1) and a first no-DOM call threw on document.createElement. After the change, 8/8 pass in both octane and octane-prod. Removing the textContent assign made only the HMR case fail again; first-wins and no-DOM stayed green. Nearby injectStyle coverage: 136 passed.

pnpm format:files on the changed paths: unchanged. pnpm sync: no generated files.

Risk / follow-ups

  • feat(compiler): sibling-scoped style blocks, $class, and apply (tsrx RFC #1) #939 already has a slightly broader client injectStyle (token-match data-href for React batched resources). Merging both will need a small reconcile in that function; this PR does not take the ~= change.
  • Content hash in the compiled id (RFC optional runtime dedupe, S8.6 / D12) is still open if HMR-without-DOM or multi-runtime cases need it.
  • injectStyle is module-eval / HMR, not a per-render hot path. Extra work is one data-octane query plus a string compare on the existing-sheet branch.

Provenance

  • An agent produced this diff (agent-authored)
Open in Web Open in Cursor 

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

Same-id re-injection with different CSS now updates the owned
<style data-octane> text instead of keeping the stale first sheet.
Server-emitted sheets and the no-DOM Set path stay first-wins.

Co-authored-by: Jon Wheeler <[email protected]>
@jonkwheeler
jonkwheeler marked this pull request as ready for review September 4, 2026 19:03
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Sep 4, 2026
@trueadm
trueadm merged commit a059b46 into main Sep 5, 2026
46 of 49 checks passed
@trueadm
trueadm deleted the cursor/inject-style-hmr-reinject-645d branch September 5, 2026 17:29
leonidaz added a commit that referenced this pull request Sep 5, 2026
Resolve against main's #957, #960, and #961, which landed on the
statement-scoped style machinery this branch replaces:

- runtime.ts: keep the branch's `injectStyle` (main's in-place refresh
  is already here; the React Float match stays a `data-href` token
  match for batched multi-hash islands).
- style-hmr-reinject.test.ts: take main's superset (adds the
  document-less case).
- compile.js / style-scopes.js: keep the sibling-scope pre-pass; drop
  main's `componentStyleRoots`/`annotateRootWithHash`/`applyCssScoping`
  and the `applyStyleRefs` import. Main's synthesized-class merge flag
  (#961) applies unchanged to the pre-pass's stamped attributes.
- `<style ref>` (#960) is not carried over: the attribute stays
  unsupported in Octane pending its removal from TSRX, so its fixture,
  tests, and changeset are dropped and the immutability fixture no
  longer uses it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

injectStyle drops a re-injection with an unchanged hash but changed CSS (HMR hazard)

3 participants