Skip to content

fix(bundler): resolve dynamic ogImage in useSeoMeta transform - #770

Merged
harlan-zw merged 3 commits into
mainfrom
fix/seo-meta-dynamic-media-769
Jun 12, 2026
Merged

harlan-zw merged 3 commits into
mainfrom
fix/seo-meta-dynamic-media-769

Conversation

@harlan-zw

@harlan-zw harlan-zw commented Jun 11, 2026 •

Copy link
Copy Markdown
Collaborator

🔗 Linked issue

Resolves #769

❓ Type of change

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

📚 Description

The useSeoMeta -> useHead build transform only expanded ogImage/twitterImage when written as object or array literals. A dynamic value (ref, computed, a getter, or a plain identifier) was inlined as a single content field, so at runtime the resolved object serialized to [object Object] instead of expanding into og:image, og:image:width, etc.

The runtime (unpackMeta via FlatMetaPlugin) already handles these dynamic values correctly; only the build transform diverged. UseSeoMetaTransform now detects the media keys (ogImage, ogVideo, ogAudio, twitterImage) and:

  • bails the call to runtime whenever the value is not a static literal (so the original useSeoMeta(...) is preserved and resolved at runtime),
  • expands static object literals like ogImage: { url, width } into separate tags, matching runtime,
  • fixes the url sub-key, which was emitted as og:image:url instead of og:image.

Verified against runtime output: dynamic getters/refs/computed for ogImage (array and object) and the corrected static expansion now match the SSR result. Covered by new and updated tests in useSeoMetaTransform.test.ts.

Summary by CodeRabbit

  • Improvements

    • Expanded SEO/meta handling: static Open Graph/Twitter media object/array literals now generate multiple meta tags (including derived image/video/audio fields); non-static values still handled at runtime.
    • Normalized media field names (e.g., secureUrl → secure_url) and adjusted tag naming for image/twitter entries.
  • Bug Fixes

    • Preserved local import aliases when transforming meta calls.
  • Tests

    • Added coverage for static vs dynamic media, naming, and aliasing scenarios.

…form

The useSeoMeta->useHead AST transform only expanded ogImage/twitterImage
object and array literals. A dynamic value (ref, computed, getter,
identifier) was inlined as a single content field, so at runtime the
resolved object serialized to [object Object] instead of expanding into
og:image, og:image:width, etc.

Bail such calls to runtime unpackMeta, which already handles them. Also
expand static media object literals and fix the url sub-key, which was
emitted as og:image:url instead of og:image.

Resolves #769
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d9013435-89a5-4910-8936-2590a4323777

📥 Commits

Reviewing files that changed from the base of the PR and between 405c29a and 95ffffb.

📒 Files selected for processing (1)
  • packages/bundler/test/useSeoMetaTransform.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bundler/test/useSeoMetaTransform.test.ts

📝 Walkthrough

Walkthrough

Adds MEDIA_KEYS-aware transform for useSeoMeta: statically expands object/array literal media entries (ogImage, ogVideo, ogAudio, twitterImage) into multiple meta tags and falls back to leaving the call for runtime unpacking when shapes are dynamic; also preserves aliased imports when transforms bail.

Changes

Media key static expansion with dynamic fallback

Layer / File(s) Summary
Media key definition and static expansion logic
packages/bundler/src/unplugin/UseSeoMetaTransform.ts
Adds MEDIA_KEYS for ogImage, ogVideo, ogAudio, twitterImage. Implements static expansion of object/array literals into multiple og:*/twitter:* meta entries and sets output = false to fallback to runtime when values are dynamic or unsupported.
Import specifier preservation and tests
packages/bundler/src/unplugin/UseSeoMetaTransform.ts, packages/bundler/test/useSeoMetaTransform.test.ts
Preserves local aliased useSeoMeta imports when transform bails. Updates and adds tests: inlines reactive non-structural keys, ensures dynamic computed media stays as runtime useSeoMeta, validates static expansion (including secureUrl → secure_url mapping), and updates snapshots to use og:image naming.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • unjs/unhead#749: Preserves the useSeoMeta import when sibling calls are untransformable, related to alias-preservation logic updated here.

Poem

🐰 Meta tags dance in the light,
Static shapes expand just right,
When computed values whirl and sigh,
Runtime steps in, lets them fly,
No more "[object Object]" — huzzah, hooray!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: fixing dynamic ogImage handling in the useSeoMeta transform. It is concise, specific, and directly reflects the main purpose of the PR.
Description check ✅ Passed The description includes all required template sections: linked issue (#769), type of change (bug fix), and detailed explanation of the problem, solution, and verification approach.
Linked Issues check ✅ Passed The PR fully addresses issue #769 by detecting media keys, bailing to runtime for dynamic values, expanding static literals, and fixing the url sub-key naming convention, with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes in the two modified files are directly related to fixing the dynamic ogImage issue and its test coverage; no unrelated modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/seo-meta-dynamic-media-769

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 and usage tips.

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Bundle Size Gzipped
Client (Minimal) 10.8 kB 4.4 kB
Server (Minimal) 10.7 kB 4.3 kB
Vue Client (Minimal) 11.8 kB 4.9 kB
Vue Server (Minimal) 11.7 kB 4.8 kB

- preserve aliased imports (useSeoMeta as usm) when a media call bails,
  which previously left the kept call site unbound (ReferenceError)
- bail object literals with getters/setters/methods/computed keys instead
  of splicing invalid syntax into content
- bail non-primitive literal media values (e.g. regexp) that would diverge
  from runtime
- map secureUrl to :secure_url in static media objects, matching runtime

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/bundler/test/useSeoMetaTransform.test.ts (1)

222-232: ⚡ Quick win

Add the missing “method” bailout case to match the test intent.

This block claims getter/method/computed-key coverage, but currently only asserts getter and computed-key forms.

Suggested test addition
   it('bails media objects with getters/methods/computed keys', async () => {
     // These can't be reproduced as plain `key: value` tags, so the call must go to runtime.
     expect(await transform([
       'import { useSeoMeta } from \'unhead\'',
       'useSeoMeta({ ogImage: { get url() { return \'/o.png\' } } })',
     ])).toBeUndefined()
+    expect(await transform([
+      'import { useSeoMeta } from \'unhead\'',
+      'useSeoMeta({ ogImage: { url() { return \'/o.png\' } } })',
+    ])).toBeUndefined()
     expect(await transform([
       'import { useSeoMeta } from \'unhead\'',
       'useSeoMeta({ ogImage: { [key]: \'/o.png\' } })',
     ])).toBeUndefined()
   })
🤖 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/bundler/test/useSeoMetaTransform.test.ts` around lines 222 - 232,
The test currently checks getter and computed-key bailouts but misses a "method"
object form; add a third assertion inside the same it('bails media objects with
getters/methods/computed keys', ...) that calls transform with code containing
useSeoMeta({ ogImage: { method() { return '/o.png' } } }) and expect the result
toBeUndefined(), referencing the existing transform helper and the useSeoMeta
invocation so the test covers the method bailout case.
🤖 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.

Nitpick comments:
In `@packages/bundler/test/useSeoMetaTransform.test.ts`:
- Around line 222-232: The test currently checks getter and computed-key
bailouts but misses a "method" object form; add a third assertion inside the
same it('bails media objects with getters/methods/computed keys', ...) that
calls transform with code containing useSeoMeta({ ogImage: { method() { return
'/o.png' } } }) and expect the result toBeUndefined(), referencing the existing
transform helper and the useSeoMeta invocation so the test covers the method
bailout case.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2da73da3-1489-43f6-9401-cf3f74c04a90

📥 Commits

Reviewing files that changed from the base of the PR and between 93cc36b and 405c29a.

📒 Files selected for processing (2)
  • packages/bundler/src/unplugin/UseSeoMetaTransform.ts
  • packages/bundler/test/useSeoMetaTransform.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/bundler/src/unplugin/UseSeoMetaTransform.ts

@harlan-zw
harlan-zw merged commit 06a859b into main Jun 12, 2026
7 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request Jun 30, 2026
1 of 6 tasks
@coderabbitai coderabbitai Bot mentioned this pull request Jul 19, 2026
1 of 6 tasks
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.

Unable to resolve dynamic ogImage object value

1 participant