Repository navigation
fix(unhead): align Promise handling with sync rendering - #839
Conversation
📝 WalkthroughWalkthroughThe synchronous hook pipeline now avoids awaiting promise-based listeners and promise entry resolution. Async hook returns emit a deduplicated per-head warning, while rejected entries remain eligible for retry. Schema.org resolution is explicitly synchronous. ChangesSynchronous pipeline behavior
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant SSRRender
participant HookPipeline
participant PromisesPlugin
participant PromiseEntry
participant Console
SSRRender->>HookPipeline: resolve tags and entries synchronously
HookPipeline->>PromisesPlugin: process entries
PromisesPlugin->>PromiseEntry: start walkPromises
HookPipeline-->>SSRRender: continue without awaiting
PromiseEntry-->>PromisesPlugin: fulfillment or rejection
PromisesPlugin->>PromisesPlugin: update input or reset processing flag
HookPipeline->>Console: warn once for thenable hook listener
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/schema-org/src/plugin.ts (1)
124-132: 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick winFix spreading a potential Promise in synchronous code.
The
metafunction is typed as returningPartial<MetaInput> | Promise<Partial<MetaInput>>(line 43). Now that this hook is synchronous, ifmeta?.()returns a Promise, spreading it with...will spread the Promise object (resulting in an empty object or unintended behavior) instead of the resolved metadata.If
metamust support returning a Promise, you'll need to either resolve it before the synchronous pipeline begins (e.g. during a separate async initialization step) or update the contract sometais strictly synchronous.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/schema-org/src/plugin.ts` around lines 124 - 132, The synchronous 'tags:resolve' hook must not spread the potentially asynchronous result of meta?.(). Ensure metadata is resolved before this hook runs, or update the meta contract and implementation to be strictly synchronous; then pass only resolved metadata into graph.resolveGraph while preserving the existing merge with config and resolvedMeta.
🧹 Nitpick comments (1)
packages/unhead/src/plugins/promises.ts (1)
36-37: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valuePrefer
for...ofwhen iterating over arrays.
ctx.entriesis an array. Iterating over an array withfor...incan iterate over unexpected enumerable properties and yields string indices, which is generally discouraged in favor offor...of.♻️ Proposed refactor
- for (const k in ctx.entries) { - const entry = ctx.entries[k] + for (const entry of ctx.entries) {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/unhead/src/plugins/promises.ts` around lines 36 - 37, Update the iteration over the array ctx.entries to use for...of, binding each element directly as entry instead of iterating string indices with for...in. Preserve the existing processing inside the loop.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/schema-org/src/plugin.ts`:
- Around line 124-132: The synchronous 'tags:resolve' hook must not spread the
potentially asynchronous result of meta?.(). Ensure metadata is resolved before
this hook runs, or update the meta contract and implementation to be strictly
synchronous; then pass only resolved metadata into graph.resolveGraph while
preserving the existing merge with config and resolvedMeta.
---
Nitpick comments:
In `@packages/unhead/src/plugins/promises.ts`:
- Around line 36-37: Update the iteration over the array ctx.entries to use
for...of, binding each element directly as entry instead of iterating string
indices with for...in. Preserve the existing processing inside the loop.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 19bde276-66d2-4987-9089-78806bb5ceee
📒 Files selected for processing (6)
packages/schema-org/src/plugin.tspackages/unhead/src/plugins/promises.tspackages/unhead/src/types/head.tspackages/unhead/src/utils/hooks.tspackages/unhead/test/unit/hooks/async-listener-warn.test.tspackages/unhead/test/unit/plugins/promises.test.ts
📦 Bundle Size✅ No notable changes All bundles (14)
⚡ Performance (directional)
All benchmarks (14)
Baseline: main @ ea14841 · 2026-07-14 · gzipped is the headline size metric · perf is directional (shared-runner, gated) |
|
The async |
|
Follow-up to the earlier schema.org thread: after checking the v3 release and sync-engine PR, this branch now treats both leftovers as part of the same missed cleanup. The |
|
Correction after reviewing the intended core/add-on boundary: |
🔗 Linked issue
Related to #828 and #619
❓ Type of change
📚 Description
The v3 renderer is synchronous, but
PromisesPluginstill used an asyncentries:resolvelistener that Unhead could not await. Schema.org metadata resolution also accepted a Promise even though the result was consumed synchronously.PromisesPluginnow runs synchronously, omits pending entries from the current pass, resolves their values in the background, and invalidates the client after they settle. A later server render can use the resolved value; inputs needed during the first SSR render should still be awaited upstream. Stale patch results are ignored and rejections can be retried.Modern entrypoints keep Promise resolution opt-in. Legacy
createHeadandcreateServerHeadcontinue registeringPromisesPluginfor v2 compatibility.ValidatePluginreports ignored Promise-returning hooks during development.The migration and hook docs now match the synchronous contract. Tests cover legacy compatibility, listener ordering, client invalidation, later SSR renders, stale resolutions, rejected thenables, and dev-only warnings.