fix(embeds): address Phase 38 review findings in hydration runtime, plugin sync, and docs

This commit is contained in:
Jakub Zych
2026-11-28 13:13:09 +01:00
parent 47de196516
commit 14558e4677
4 changed files with 46 additions and 10 deletions

View File

@@ -11,6 +11,7 @@
// inline `<script>` injection.
import {
createVNode,
getCurrentInstance,
onBeforeUnmount,
onMounted,
ref,
@@ -46,13 +47,21 @@ export interface JournalEmbedHydrationOptions {
rootMargin?: string
}
function replaceChildrenSafe(parent: HTMLElement, child: HTMLElement): void {
function replaceChildrenSafe(parent: HTMLElement, ...children: Node[]): void {
if (typeof parent.replaceChildren === 'function') {
parent.replaceChildren(child)
parent.replaceChildren(...children)
return
}
while (parent.firstChild) parent.removeChild(parent.firstChild)
parent.appendChild(child)
for (const child of children) parent.appendChild(child)
}
function restoreFallback(figure: HTMLElement, savedChildren: Node[], fallbackHtml: string): void {
if (savedChildren.length > 0) {
replaceChildrenSafe(figure, ...savedChildren)
} else {
figure.innerHTML = fallbackHtml
}
}
function warnDev(providerName: string, id: string, err: unknown): void {
@@ -90,6 +99,8 @@ export async function hydrateEmbedFigure(
const doc = figure.ownerDocument
const host = doc.createElement('div')
host.className = 'journal-embed__stage'
const savedChildren = Array.from(figure.childNodes)
let attached = false
try {
if (provider.component) {
@@ -108,13 +119,16 @@ export async function hydrateEmbedFigure(
})
if (options.appContext) vnode.appContext = options.appContext
// Attach host before render so onMounted sees real layout dimensions (WR-02).
replaceChildrenSafe(figure, host)
attached = true
render(vnode, host)
if (options.signal?.aborted) {
render(null, host)
restoreFallback(figure, savedChildren, fallbackHtml)
return null
}
replaceChildrenSafe(figure, host)
figure.classList.add('journal-embed--hydrated')
return () => {
@@ -134,13 +148,16 @@ export async function hydrateEmbedFigure(
reducedMotion: options.reducedMotion,
isReducedMotion: options.reducedMotion.value,
}
// Attach host before mount so layout-measuring widgets see real dimensions (WR-02).
replaceChildrenSafe(figure, host)
attached = true
const unmount = await provider.mount(host, ctx)
if (options.signal?.aborted) {
if (typeof unmount === 'function') unmount()
restoreFallback(figure, savedChildren, fallbackHtml)
return null
}
replaceChildrenSafe(figure, host)
figure.classList.add('journal-embed--hydrated')
return () => {
@@ -149,6 +166,14 @@ export async function hydrateEmbedFigure(
}
}
} catch (err) {
if (attached) {
try {
render(null, host)
} catch {
// Ignore cleanup errors on failed mount.
}
restoreFallback(figure, savedChildren, fallbackHtml)
}
if (options.onError) options.onError(err, figure)
else warnDev(options.providerName, options.id, err)
return null
@@ -169,6 +194,12 @@ export function useJournalEmbedHydration(
const isServer = (typeof import.meta !== 'undefined' && (import.meta as { server?: boolean }).server === true) || typeof window === 'undefined'
if (isServer) return
// Capture appContext synchronously in setup() rather than inside onMounted/watch (WR-01).
const instance = getCurrentInstance()
const appContext: AppContext | null =
instance?.appContext ??
(typeof useNuxtApp === 'function' ? ((useNuxtApp()?.vueApp?._context as AppContext | undefined) ?? null) : null)
const reducedMotion = ref(false)
let mediaQuery: MediaQueryList | null = null
const onMotionChange = (event: MediaQueryListEvent) => {
@@ -202,8 +233,6 @@ export function useJournalEmbedHydration(
if (!root) return
const registry = options.registry ?? getJournalEmbedRegistry()
const nuxtApp = typeof useNuxtApp === 'function' ? useNuxtApp() : undefined
const appContext = (nuxtApp?.vueApp?._context as AppContext | undefined) ?? null
const figures = root.querySelectorAll<HTMLElement>('figure[data-embed][data-id]')
for (const figure of figures) {

View File

@@ -4,13 +4,17 @@
// agree on the exact same provider set.
import { defineJournalEmbedProviders, type JournalEmbedProvider } from '../utils/embedRegistry.ts'
const syncedEmbedMaps = new WeakSet<object>()
export default defineNuxtPlugin({
name: 'journal-embeds',
enforce: 'pre',
setup() {
const appConfig = useAppConfig() as { journal?: { embeds?: Record<string, JournalEmbedProvider> } }
if (appConfig?.journal?.embeds) {
defineJournalEmbedProviders(appConfig.journal.embeds)
const embeds = appConfig?.journal?.embeds
if (embeds && typeof embeds === 'object' && !syncedEmbedMaps.has(embeds)) {
defineJournalEmbedProviders(embeds)
syncedEmbedMaps.add(embeds)
}
},
})