docs(38-02): record vue-journal-layer embed registry and hydration summary

This commit is contained in:
Jakub Zych
2026-11-28 12:52:11 +01:00
parent 44f47ad64f
commit e0a4eb7480

View File

@@ -0,0 +1,72 @@
---
phase: 38-extensible-journal-embed-providers
plan: 02
subsystem: vue-journal-layer
tags: [journal, embeds, sanitizer, nuxt, hydration, cls, reduced-motion]
requires: ["38-01"]
provides:
- "Pure TS `JournalEmbedRegistry` (`app/utils/embedRegistry.ts`) with prototype-safe `Map` storage, built-in `YOUTUBE_EMBED_PROVIDER`, and singleton helpers"
- "Universal Nuxt plugin (`app/plugins/journal-embeds.ts`) syncing `app.config.ts` (`journal.embeds`) into the active registry on SSR and client"
- "Registry-driven pair-aware `(data-embed, data-id)` sanitizer (`app/utils/htmlGate.ts`) allowing validated `data-aspect`/`data-height` and UGC fallback children"
- "Generalized `expandEmbeds` (`app/utils/embeds.ts`) preserving byte-identical YouTube SSR iframes and SSR fallback DOM + CLS reservation for client-hydrated providers"
- "Lazy `IntersectionObserver` hydration composable (`app/composables/useJournalEmbedHydration.ts`) wired into `PostBody.vue` and `CategoryHeader.vue`, plus neutral `.journal-embed` CSS"
affects:
- "../vue-journal-layer"
- "Plan 38-03 (unit tests, corpus cross-check, docs)"
tech-stack:
added: []
patterns:
- "Dual provider registration (`app.config.ts` `journal.embeds` + `registerJournalEmbedProvider` / `defineJournalEmbedProviders`) feeding both SSR sanitizer and client hydration"
- "Zero-CLS height reservation before fallback swap (`min-height` from measured box + validated `data-aspect`/`data-height`)"
- "Dual client mount interface: Vue async `component` (`createVNode` + `render` inheriting `nuxtApp.vueApp._context`) and plain DOM `mount(el, ctx)` with reactive `prefers-reduced-motion` and error fallback"
key-files:
created:
- "../vue-journal-layer/app/utils/embedRegistry.ts"
- "../vue-journal-layer/app/plugins/journal-embeds.ts"
- "../vue-journal-layer/app/composables/useJournalEmbedHydration.ts"
modified:
- "../vue-journal-layer/app/utils/htmlGate.ts"
- "../vue-journal-layer/app/utils/embeds.ts"
- "../vue-journal-layer/app/components/Journal/PostBody.vue"
- "../vue-journal-layer/app/components/Journal/CategoryHeader.vue"
- "../vue-journal-layer/app/assets/css/journal-base.css"
- "../vue-journal-layer/tests/embeds.test.ts"
- "../vue-journal-layer/tests/htmlGate.test.ts"
key-decisions:
- "Kept `embedRegistry.ts` pure TypeScript with zero Nuxt imports and avoided re-exporting symbols across `app/utils` / `app/composables` so both `node --test` and `tests/autoImports.test.ts` pass cleanly."
- "Aligned malformed-placeholder stripping expectations in `tests/embeds.test.ts` and `tests/htmlGate.test.ts` from orphan single-attribute `<figure data-embed=...>` / `<figure data-id=...>` to `<figure></figure>` to match Wave 1's D-03 pair-aware stripping."
- "Scoped the default 16/9 `aspect-ratio` rule in `journal-base.css` to `.journal-embed--youtube` and `.journal-embed:has(> iframe)` so custom fallback images and interactive widgets are not cropped to 16/9."
duration: 6m
completed: 2026-11-28
---
# Phase 38 Plan 02: Extensible Embed Registry & Lazy Hydration Runtime (`vue-journal-layer`) Summary
Implemented the app-extensible embed provider registry, universal Nuxt config sync plugin, registry-driven pair-aware sanitizer, generalized SSR embed expander, and lazy `IntersectionObserver` client hydration runtime in `vue-journal-layer`.
## Commits
- **`vue-journal-layer` `babd3a9`** — `feat(embeds): add extensible embed registry, universal plugin, and pair-aware sanitizer`
- Created `app/utils/embedRegistry.ts` (`EMBED_PROVIDER_KEY`, `EMBED_ASPECT`, `EMBED_HEIGHT`, `JournalEmbedRegistry`, `YOUTUBE_EMBED_PROVIDER`, `createJournalEmbedRegistry`, `getJournalEmbedRegistry`, `registerJournalEmbedProvider`, `defineJournalEmbedProviders`, `resetJournalEmbedRegistry`).
- Created universal `app/plugins/journal-embeds.ts` (`enforce: 'pre'`) syncing `useAppConfig().journal?.embeds` into the active registry on SSR and client.
- Updated `app/utils/htmlGate.ts` to read the active (or `options.embedRegistry`) registry inside `makeFilterAttributeValues`, enforce pair-aware `(data-embed, data-id)` matching, allow validated `data-aspect`/`data-height` only on validated `<figure data-embed>` elements, and strip all four attributes on mismatch.
- Generalized `app/utils/embeds.ts` (`expandEmbeds(html, registry?)`) to preserve byte-identical YouTube SSR `<iframe>` output while keeping SSR fallback children intact with `journal-embed journal-embed--<provider>` classes and safe CLS `aspect-ratio`/`min-height` inline styles for client-hydrated providers.
- Aligned existing malformed-placeholder assertions in `tests/embeds.test.ts` and `tests/htmlGate.test.ts` with pair-aware stripping.
- **`vue-journal-layer` `7613272`** — `feat(embeds): add lazy IntersectionObserver hydration runtime and embed styles`
- Created `app/composables/useJournalEmbedHydration.ts` exporting pure testable `hydrateEmbedFigure` and client-only `useJournalEmbedHydration` with `IntersectionObserver` (`rootMargin` default `200px`), measured-height CLS reservation, reactive `prefers-reduced-motion`, Vue async `component` (`createVNode` + `render` inheriting `nuxtApp.vueApp._context`), plain DOM `mount(el, ctx)`, teardown/abort handling on HTML changes or unmount, and error resilience preserving SSR fallback DOM.
- Wired `useJournalEmbedHydration` into `app/components/Journal/PostBody.vue` and `app/components/Journal/CategoryHeader.vue` while keeping the single-call sanitize-first `expandEmbeds(...)` pipeline intact.
- Updated `app/assets/css/journal-base.css` so 16/9 aspect ratio applies to `.journal-embed--youtube` and iframe embeds while custom fallback images, `<figcaption>`, and `.journal-embed__stage` render naturally.
## Verification Results
- `pnpm --dir ../vue-journal-layer test`: PASS (204/204 tests, including `autoImports.test.ts`, `embeds.test.ts`, `htmlGate.test.ts`).
- `pnpm --dir ../vue-journal-layer build:playground`: PASS (clean Nuxt SSR build, zero duplicate auto-import warnings).
- `pnpm --dir ../vue-journal-layer smoke:ssr`: PASS (all 5 SSR smoke suites: 89 + 2 + 2 + 2 + 2 = 97 checks green).
## Deviations & Notes for Wave 3
- **Expected test updates (Rule 1):**
- Updated 2 malformed-placeholder assertions in `tests/embeds.test.ts` and 9 assertions in `tests/htmlGate.test.ts` from orphan single-attribute `<figure data-embed="youtube"></figure>` / `<figure data-id="..."></figure>` to `<figure></figure>` to match Wave 1's pair-aware stripping (`enforceEmbedPairs`).
- **Notes for Wave 3 (Plan 38-03 — tests, corpus cross-check, docs):**
- `hydrateEmbedFigure` is directly importable under `node --test` without Nuxt globals (passing a mock figure element, `NormalizedJournalEmbedProvider`, and `{ id, providerName, reducedMotion }` options), making unit tests for CLS height reservation, Vue component mount/unmount, DOM `mount` cleanup, `prefers-reduced-motion`, abort-on-teardown, and error fallback straightforward to write.
- `resetJournalEmbedRegistry()` should be called in `afterEach` / test teardown whenever tests register custom providers on the global registry.