diff --git a/quartz/components/renderPage.tsx b/quartz/components/renderPage.tsx index aad75a3..83beff5 100644 --- a/quartz/components/renderPage.tsx +++ b/quartz/components/renderPage.tsx @@ -1,7 +1,12 @@ import { render } from "preact-render-to-string" import { QuartzComponent, QuartzComponentProps } from "./types" import BodyConstructor from "./Body" -import { CSSResource, JSResourceToScriptElement, StaticResources } from "../util/resources" +import { + CSSResource, + JSResource, + JSResourceToScriptElement, + StaticResources, +} from "../util/resources" import { FullSlug, RelativeURL, joinSegments, normalizeHastElement } from "../util/path" import { clone } from "../util/clone" import { visit } from "unist-util-visit" @@ -46,6 +51,27 @@ export function pageResources( } } + const extracted = ctx?.extractedInlineResources + const resolvedCss: CSSResource[] = staticResources.css.map((resource) => { + if (!(resource.inline ?? false) || !extracted) return resource + const filename = extracted.get(resource.content) + if (!filename) return resource + return { content: joinSegments(baseDir, filename) } + }) + + const resolvedJs: JSResource[] = staticResources.js.map((resource) => { + if (resource.contentType !== "inline" || !extracted) return resource + const filename = extracted.get(resource.script) + if (!filename) return resource + return { + src: joinSegments(baseDir, filename), + loadTime: resource.loadTime, + contentType: "external" as const, + moduleType: resource.moduleType, + spaPreserve: resource.spaPreserve, + } + }) + const contentIndexPath = joinSegments(baseDir, "static/contentIndex.json") const contentIndexScript = `const fetchData = fetch("${contentIndexPath}").then(data => data.json())` @@ -55,7 +81,7 @@ export function pageResources( content: joinSegments(baseDir, cssFile), }, ...componentCssResources, - ...staticResources.css, + ...resolvedCss, ], js: [ { @@ -69,7 +95,7 @@ export function pageResources( spaPreserve: true, script: contentIndexScript, }, - ...staticResources.js, + ...resolvedJs, ], additionalHead: staticResources.additionalHead, } diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index 9a44496..4bd6a37 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -275,7 +275,7 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso export const ComponentResources: QuartzEmitterPlugin = () => { return { name: "ComponentResources", - async *emit(ctx, _content, _resources) { + async *emit(ctx, _content, resources) { const cfg = ctx.cfg.configuration // component specific scripts and styles const componentResources = getComponentResources(ctx) @@ -395,6 +395,59 @@ export const ComponentResources: QuartzEmitterPlugin = () => { ctx.componentCssMap = cssStringToFilename + // Extract inline CSS/JS from plugin externalResources() into external files. + // This prevents large inline payloads (e.g. theme CSS) from being duplicated + // into every HTML page's
. + const extractedInlineResources = new Map