Skip to content

chore: dedupe async-hook warning per instance - #837

Merged
harlan-zw merged 1 commit into
perf/ssr-default-init-hoistfrom
fix/async-warn-per-instance
Jul 14, 2026
Merged

harlan-zw merged 1 commit into
perf/ssr-default-init-hoistfrom
fix/async-warn-per-instance

Conversation

@harlan-zw

Copy link
Copy Markdown
Collaborator

Stacks on #828 (perf/ssr-default-init-hoist); the flag landed there via #833.

🔗 Linked issue

N/A

❓ Type of change

  • 📖 Documentation
  • 🐞 Bug fix
  • 👌 Enhancement
  • ✨ New feature
  • 🧹 Chore
  • ⚠️ Breaking change

📚 Description

#833 introduced a module-scope let warnedAsyncHook = false to 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-inert still passes all 48 entries.

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.
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b91f9817-e81b-4ead-805c-07ab3250fee4

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/async-warn-per-instance

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@harlan-zw harlan-zw changed the title fix(unhead): dedupe async-hook warning per instance chore: dedupe async-hook warning per instance Jul 14, 2026
@harlan-zw
harlan-zw merged commit 93725e8 into perf/ssr-default-init-hoist Jul 14, 2026
harlan-zw added a commit that referenced this pull request Jul 15, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant