Generate the unique hash lazily so renders stay deterministic - #254
Open
pedrosousa13 wants to merge 1 commit into
Open
Generate the unique hash lazily so renders stay deterministic#254pedrosousa13 wants to merge 1 commit into
pedrosousa13 wants to merge 1 commit into
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
pedrosousa13
force-pushed
the
fix-nondeterministic-hash-on-render
branch
from
September 4, 2026 09:11
a4af8b0 to
27894a7
Compare
`randomString(8)` was passed as the `useRef` initial value, so `Math.random()` ran on every render whenever `uniqueHash` was not supplied — `??` evaluates its right operand each time regardless of whether the ref keeps the result. The hash is only read when `uniquifyIDs` is set, and only after the SVG content has loaded, which requires the DOM. It is therefore never needed during a server render. Generate it on first read instead. This unblocks static prerendering in frameworks that reject non-deterministic values read during render. Next.js with `cacheComponents` enabled patches `Math.random` and aborts the enclosing Suspense boundary when it is called, which silently drops the component — and every later boundary — from the prerendered HTML. No behaviour change: a supplied `uniqueHash` is still used verbatim, and an omitted one still resolves to a stable random string for the lifetime of the component.
pedrosousa13
force-pushed
the
fix-nondeterministic-hash-on-render
branch
from
September 4, 2026 09:13
27894a7 to
5b668af
Compare
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.
Fixes #253.
The problem
src/modules/useInlineSVG.ts:27useRef's argument is evaluated eagerly on every render, and??evaluates its right operand wheneveruniqueHashis undefined — sorandomString(8)runs on each render even though only the first result is kept.That makes the component impossible to statically prerender under Next.js 16 with
cacheComponents, which patchesMath.randomat build time and aborts the enclosing Suspense boundary when it is called. The abort is silent, and it freezes every later boundary as client-rendered in the stored artifact. Full mechanism and repro in #253.The change
Hold
uniqueHashin the ref and fill in a random string on first read:The two read sites — the
useReducerinitializer andgetElement— now callgetHash().This works because the hash is only read when
uniquifyIDsis set (getNodereturns early otherwise atutils.ts:59,updateSVGAttributesatutils.ts:115), and only oncecontentexists. Content loading happens in the mount effect behindcanUseDOM(), so on a server render the hash is never reached — and now never generated.getHashis memoized deliberately: an unstable identity would flow intogetElement's dependency array and re-trigger theLOADED -> READYeffect on every render.I kept the syntax at ES2020 to match
tsconfig.json'starget.??=would read better here, but it is ES2021 andtsdownemits it unchanged, so it would have been the first logical-assignment operator in the shipped bundle — a syntax-floor bump for a package that supportsreact: 16.8 - 19.Behaviour
Unchanged in every case:
uniqueHashis used verbatim, exactly as before;uniqueHashis still preserved (??does not treat''as nullish, same as before);uniqueHashthat arrives after mount is still ignored, as it was before.I deliberately avoided
useId(): it would be the semantically ideal source for this, but it needs React 18+ and the package supports16.8 - 19. Worth revisiting if that floor ever moves.Tests
New
test/prerender.spec.tsx:Math.randomis never called duringrenderToStaticMarkup, across a remote URL, a data URI,uniquifyIDs, and a customuniqueHash;StrictModeyields a single hash for every id. That path — the cached branch of the reducer initializer — is the only place the hash is read during a render, so it is the one that most needed covering.Verified these are real regression tests, not tautologies: with
src/modules/useInlineSVG.tsreverted tomainand the tests left in place, 3 of the 6 fail. (The other 3 should pass either way — a customuniqueHashshort-circuits the old expression, and the two client-side tests assert behaviour the old code also had.)pnpm run validateis green — lint, typecheck, 100 tests (94 before, 6 new), build, size limit (7.13 kB ESM against the 10 kB budget), andattw.