Repository navigation
chore: dedupe async-hook warning per instance - #837
Merged
harlan-zw merged 1 commit intoJul 14, 2026
Merged
Conversation
The warn-once flag for async listeners on the sync hook pipeline was module state, violating the CONTRIBUTING module-state policy (input originates from user hook behavior, not module constants). Move it to the head instance so each head (e.g. each SSR request's) warns independently and test runs can't bleed state into each other.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
harlan-zw
added a commit
that referenced
this pull request
Jul 15, 2026
This reverts commit 93725e8.
harlan-zw
added a commit
that referenced
this pull request
Jul 15, 2026
* perf(unhead): precompute SSR default init tags once per process Nuxt-style servers call createHead() per request, and the default init entry (htmlAttrs lang, charset, viewport) was re-normalized through normalizeEntryToTags + weight/dedupe assignment on every request since the entry tag cache is per instance. Hoist the default init object to module scope and lazily normalize it once, mirroring the resolveTags first-resolve path exactly. The shared array is attached as entry._precomputedTags and only used when nothing can observe or alter normalization: - default capoTagWeight (no tagWeight option, no per-resolve override) - no custom propResolvers (the entry has no on* handlers so the built-in server resolver is a no-op) - no entries:normalize or entries:resolve hooks at resolve time - no entry options - disableDefaults and user init entries keep the normalize path The array is never cached on entry._tags so entries:resolve listeners can never reach the shared array, and tag-mutating hooks are already covered by the existing needsClone defensive copy. * perf(vue): enable SSR default init fast path via static resolver marker Add an internal _static marker to PropResolver for resolvers that are the identity function for plain non-reactive JSON values. VueResolver (pure isRef/toValue unwrap) and the built-in server on* resolver are marked, and the attach-time guard now only requires every custom propResolver to be static instead of requiring none, so @unhead/vue (Nuxt-style per-request createHead) gets the precomputed default init entry too. Resolve-time guards are unchanged. * fix: pure-annotate serverPropResolver marker for import-inert * perf(unhead): resolver allocation fast paths (#833) * perf(unhead): reduce per-resolve allocations in resolveTags Per-resolve fast paths in the tag resolver, gated on registered hooks: - only snapshot `head.entries` into an array when an `entries:resolve` listener will see it; the `_pending` sweep and entry loop iterate the map directly otherwise - allocate hook context objects (`entries:resolve`, `entries:normalize`) lazily behind listener checks instead of before every callHook - drop the intermediate `entryTags: HeadTag[][]`; entry tags are pushed straight into the resolve context and defensively cloned in place only when a tag-mutating hook is registered (scan still runs after the entry loop so mid-resolve registrations are honored) - remove the dead `_nonMutating` check (read but never set anywhere) - reuse the pre-dedupe tags array in valuesToTags and compact in place in sanitizeTags instead of allocating fresh output arrays A listener returning a promise on the synchronous pipeline is now surfaced with a one-time console.warn naming the hook (previously silently dropped, with later listeners deferred past the render): - PromisesPlugin no longer returns its (never-awaited) promise, so it can't defer later-registered `entries:resolve` listeners - schema-org's `tags:resolve` listener was needlessly async (no awaits); making it sync also removes per-render promise machinery (-22% CPU on the cached schema.org render bench) * fix(unhead): keep sanitizeTags contract, snapshot entries when loop hooks exist Review follow-ups for the resolver allocation work: - `sanitizeTags` is public API (`unhead/utils`): restore its returns-a-new-array contract. The in-place compaction moves to a module-private `sanitizeTagsInPlace` used by `resolveTags`; the export delegates via a copy. Export snapshots unchanged. - iterating the live entries Map let an `entries:normalize` listener that calls `head.push()` feed the new entry back into the same resolve (a per-entry push would loop forever). Snapshot the entries whenever `entries:resolve` OR `entries:normalize` listeners exist; the live Map is only iterated when no user hook code can run inside the loop. propResolvers / tagWeight do not force the snapshot: they are pure value transforms and calling head.push from them is out of contract. - regression test: an entry pushed by an entries:normalize listener must defer to the next resolve (passes on base, fails on the previous commit, passes now) * chore: dedupe async-hook warning per instance (#837) The warn-once flag for async listeners on the sync hook pipeline was module state, violating the CONTRIBUTING module-state policy (input originates from user hook behavior, not module constants). Move it to the head instance so each head (e.g. each SSR request's) warns independently and test runs can't bleed state into each other. * Revert "chore: dedupe async-hook warning per instance (#837)" This reverts commit 93725e8. * Revert "perf(unhead): resolver allocation fast paths (#833)" This reverts commit 9a59b6f. * fix(unhead): clear stale precomputed tags
harlan-zw
added a commit
that referenced
this pull request
Jul 19, 2026
* perf(unhead): precompute SSR default init tags once per process Nuxt-style servers call createHead() per request, and the default init entry (htmlAttrs lang, charset, viewport) was re-normalized through normalizeEntryToTags + weight/dedupe assignment on every request since the entry tag cache is per instance. Hoist the default init object to module scope and lazily normalize it once, mirroring the resolveTags first-resolve path exactly. The shared array is attached as entry._precomputedTags and only used when nothing can observe or alter normalization: - default capoTagWeight (no tagWeight option, no per-resolve override) - no custom propResolvers (the entry has no on* handlers so the built-in server resolver is a no-op) - no entries:normalize or entries:resolve hooks at resolve time - no entry options - disableDefaults and user init entries keep the normalize path The array is never cached on entry._tags so entries:resolve listeners can never reach the shared array, and tag-mutating hooks are already covered by the existing needsClone defensive copy. * perf(vue): enable SSR default init fast path via static resolver marker Add an internal _static marker to PropResolver for resolvers that are the identity function for plain non-reactive JSON values. VueResolver (pure isRef/toValue unwrap) and the built-in server on* resolver are marked, and the attach-time guard now only requires every custom propResolver to be static instead of requiring none, so @unhead/vue (Nuxt-style per-request createHead) gets the precomputed default init entry too. Resolve-time guards are unchanged. * fix: pure-annotate serverPropResolver marker for import-inert * perf(unhead): resolver allocation fast paths (#833) * perf(unhead): reduce per-resolve allocations in resolveTags Per-resolve fast paths in the tag resolver, gated on registered hooks: - only snapshot `head.entries` into an array when an `entries:resolve` listener will see it; the `_pending` sweep and entry loop iterate the map directly otherwise - allocate hook context objects (`entries:resolve`, `entries:normalize`) lazily behind listener checks instead of before every callHook - drop the intermediate `entryTags: HeadTag[][]`; entry tags are pushed straight into the resolve context and defensively cloned in place only when a tag-mutating hook is registered (scan still runs after the entry loop so mid-resolve registrations are honored) - remove the dead `_nonMutating` check (read but never set anywhere) - reuse the pre-dedupe tags array in valuesToTags and compact in place in sanitizeTags instead of allocating fresh output arrays A listener returning a promise on the synchronous pipeline is now surfaced with a one-time console.warn naming the hook (previously silently dropped, with later listeners deferred past the render): - PromisesPlugin no longer returns its (never-awaited) promise, so it can't defer later-registered `entries:resolve` listeners - schema-org's `tags:resolve` listener was needlessly async (no awaits); making it sync also removes per-render promise machinery (-22% CPU on the cached schema.org render bench) * fix(unhead): keep sanitizeTags contract, snapshot entries when loop hooks exist Review follow-ups for the resolver allocation work: - `sanitizeTags` is public API (`unhead/utils`): restore its returns-a-new-array contract. The in-place compaction moves to a module-private `sanitizeTagsInPlace` used by `resolveTags`; the export delegates via a copy. Export snapshots unchanged. - iterating the live entries Map let an `entries:normalize` listener that calls `head.push()` feed the new entry back into the same resolve (a per-entry push would loop forever). Snapshot the entries whenever `entries:resolve` OR `entries:normalize` listeners exist; the live Map is only iterated when no user hook code can run inside the loop. propResolvers / tagWeight do not force the snapshot: they are pure value transforms and calling head.push from them is out of contract. - regression test: an entry pushed by an entries:normalize listener must defer to the next resolve (passes on base, fails on the previous commit, passes now) * chore: dedupe async-hook warning per instance (#837) The warn-once flag for async listeners on the sync hook pipeline was module state, violating the CONTRIBUTING module-state policy (input originates from user hook behavior, not module constants). Move it to the head instance so each head (e.g. each SSR request's) warns independently and test runs can't bleed state into each other. * Revert "chore: dedupe async-hook warning per instance (#837)" This reverts commit 93725e8. * Revert "perf(unhead): resolver allocation fast paths (#833)" This reverts commit 9a59b6f. * perf(unhead): reduce resolver allocations * fix(unhead): clear stale precomputed tags * fix(unhead): guard large resolver tag arrays Keep the spread fast path for normal entry tag arrays and use indexed copying above a safe argument-count threshold. This avoids a RangeError for large cached entries without slowing the common path.\n\nUpdate the resolver benchmark to use head.render() and cover cached renders. * perf(unhead): skip unused entries resolve payload Avoid constructing the entries:resolve context when only entries:normalize listeners require the entry snapshot. Add FlatMetaPlugin first and cached render cases to the resolver benchmark. * perf(unhead): tighten resolver size tradeoff Drop the unrealistic 32k-tag spread fallback, reducing the bundle cost while retaining the allocation fast path. Mark async stream heap deltas informational because repeated unchanged runs are bimodal due to ReadableStream cleanup. * fix(unhead): align adapter and hook cache lifecycle
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.
🔗 Linked issue
N/A
❓ Type of change
📚 Description
#833 introduced a module-scope
let warnedAsyncHook = falseto dedupe the "promise ignored" warning for async listeners on the sync hook pipeline. That flag is import-inert (CI rule 1 passes) but violates the CONTRIBUTING module-state policy: its input originates from user hook behavior at request time, not module constants, and the policy's review test for that case says the state belongs on the head instance.The flag now lives on the instance as
_warnedAsyncHook(internal, optional). Practical effects: a fresh head (e.g. the next SSR request on a warm isolate) warns again instead of the whole process going silent after the first offender, and test files can no longer bleed the flag into each other. Regression test added covering the per-instance behavior;test:import-inertstill passes all 48 entries.