From 76132722b0123577ba59b3865b0748dff1b5c687 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 28 Nov 2026 12:51:30 +0100 Subject: [PATCH] feat(embeds): add lazy IntersectionObserver hydration runtime and embed styles --- app/assets/css/journal-base.css | 25 +- app/components/Journal/CategoryHeader.vue | 5 +- app/components/Journal/PostBody.vue | 5 +- app/composables/useJournalEmbedHydration.ts | 277 ++++++++++++++++++++ 4 files changed, 308 insertions(+), 4 deletions(-) create mode 100644 app/composables/useJournalEmbedHydration.ts diff --git a/app/assets/css/journal-base.css b/app/assets/css/journal-base.css index 45cfc98..6ccfb6d 100644 --- a/app/assets/css/journal-base.css +++ b/app/assets/css/journal-base.css @@ -180,14 +180,18 @@ text-align: left; } -/* Embeds expanded from the Blog Editor's placeholders (app/utils/embeds.ts) */ +/* Embeds expanded or hydrated from the Blog Editor's placeholders (app/utils/embeds.ts) */ .journal-embed { display: block; width: 100%; - aspect-ratio: 16 / 9; margin: var(--journal-space-4) 0; } +.journal-embed--youtube, +.journal-embed:has(> iframe) { + aspect-ratio: 16 / 9; +} + .journal-embed iframe { display: block; width: 100%; @@ -195,6 +199,23 @@ border: 0; } +.journal-embed img { + display: block; + width: 100%; + height: auto; +} + +.journal-embed figcaption { + margin-top: var(--journal-space-2); + color: var(--journal-color-muted); + font-size: 0.9em; +} + +.journal-embed__stage { + display: block; + width: 100%; +} + /* Listings */ .journal-post-list { display: grid; diff --git a/app/components/Journal/CategoryHeader.vue b/app/components/Journal/CategoryHeader.vue index 0bf4466..1b17c3f 100644 --- a/app/components/Journal/CategoryHeader.vue +++ b/app/components/Journal/CategoryHeader.vue @@ -11,9 +11,12 @@ import { expandEmbeds } from '../../utils/embeds.ts' import { sanitizeJournalHtml } from '../../utils/htmlGate.ts' const props = defineProps<{ category: JournalCategory; page?: number }>() +const contentRef = ref(null) const content = computed(() => props.page && props.page > 1 ? '' : expandEmbeds(sanitizeJournalHtml(props.category.content, { demoteH1: props.category.content_h1_to_h2 !== false })), ) + +useJournalEmbedHydration(contentRef, content) diff --git a/app/components/Journal/PostBody.vue b/app/components/Journal/PostBody.vue index 5d7cf40..9fb8ee8 100644 --- a/app/components/Journal/PostBody.vue +++ b/app/components/Journal/PostBody.vue @@ -20,6 +20,7 @@ import { expandEmbeds } from '../../utils/embeds.ts' import { renderJournalBody, type JournalBodyLinkSteps } from '../../utils/htmlGate.ts' const props = defineProps<{ html: string | null | undefined; demoteH1?: boolean }>() +const bodyRef = ref(null) const answer = useSeoAnswer() const route = useRoute() @@ -71,8 +72,10 @@ const html = computed(() => { ), ) }) + +useJournalEmbedHydration(bodyRef, html) diff --git a/app/composables/useJournalEmbedHydration.ts b/app/composables/useJournalEmbedHydration.ts new file mode 100644 index 0000000..4bf8937 --- /dev/null +++ b/app/composables/useJournalEmbedHydration.ts @@ -0,0 +1,277 @@ +// Client-side progressive-enhancement hydration runtime for Journal embeds. +// +// Observes validated `
` elements inside +// a rendered HTML container via `IntersectionObserver`, reserves layout height +// before swapping (zero CLS), tracks `prefers-reduced-motion`, supports both +// Vue async components (inheriting `nuxtApp.vueApp._context`) and plain DOM +// `mount(el, ctx)` callbacks, cleans up on unmount or HTML changes, and catches +// errors leaving the SSR static fallback DOM untouched. +// +// Strictly `script-src 'self'` CSP compliant: zero `eval`, `new Function`, or +// inline `