|
| 1 | +import { createRenderEffect, createResource, onCleanup, sharedConfig } from "solid-js"; |
| 2 | +import { getRequestEvent, isServer, useAssets as useAssets_ } from "solid-js/web"; |
| 3 | +import { renderAsset, type Asset } from "../server/renderAsset.tsx"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Keep's Solid suspended while loading CSS |
| 7 | + * Prevents FOUC's |
| 8 | + */ |
| 9 | +const EXPERIMENTAL_CSS_SUSPENSE = true; |
| 10 | + |
| 11 | +/** |
| 12 | + * Should also prevent FOUC's (Spoiler alert: it doesn't) |
| 13 | + * https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/blocking |
| 14 | + * https://developer.mozilla.org/en-US/docs/Web/API/HTMLStyleElement/blocking |
| 15 | + */ |
| 16 | +const EXPERIMENTAL_BLOCKING = true; |
| 17 | + |
| 18 | +const CANCEL_EVENT = "cancel"; |
| 19 | +const EVENT_REGISTRY = Symbol("assetRegistry"); |
| 20 | +const NOOP = () => ""; |
| 21 | + |
| 22 | +type AssetEntity = { key: string; consumers: number; el?: HTMLElement; ssrIdx?: number }; |
| 23 | +type Registry = Record<string, AssetEntity>; |
| 24 | +type AttrKeys = keyof Asset["attrs"]; |
| 25 | + |
| 26 | +const globalRegistry: Registry = {}; |
| 27 | + |
| 28 | +const keyAttrs = ["href", "rel", "data-vite-dev-id"] as const; |
| 29 | + |
| 30 | +const getEntity = (registry: Registry, asset: Asset) => { |
| 31 | + let key = asset.tag; |
| 32 | + for (const k of keyAttrs) { |
| 33 | + if (!(k in asset.attrs)) continue; |
| 34 | + key += `[${k}='${asset.attrs[k as keyof Asset["attrs"]]}']`; |
| 35 | + } |
| 36 | + |
| 37 | + const entity = (registry[key] ??= { |
| 38 | + key, |
| 39 | + consumers: 0, |
| 40 | + el: isServer ? undefined : (document.querySelector("head " + key) as HTMLElement) |
| 41 | + }); |
| 42 | + |
| 43 | + return entity; |
| 44 | +}; |
| 45 | + |
| 46 | +export const useAssets = (assets: Asset[], nonce?: string) => { |
| 47 | + if (!assets.length) return; |
| 48 | + |
| 49 | + const registry: Registry = isServer |
| 50 | + ? (getRequestEvent()!.locals[EVENT_REGISTRY] ??= {}) |
| 51 | + : globalRegistry; |
| 52 | + const ssrRequestAssets: Function[] = (sharedConfig.context as any)?.assets; |
| 53 | + const cssKeys: string[] = []; |
| 54 | + |
| 55 | + const cssSuspense = EXPERIMENTAL_CSS_SUSPENSE && !sharedConfig.context; |
| 56 | + const cssPromises: Promise<any>[] = []; |
| 57 | + |
| 58 | + for (const asset of assets) { |
| 59 | + const entity = getEntity(registry, asset); |
| 60 | + const isCSSLink = asset.tag === "link" && asset.attrs.rel === "stylesheet"; |
| 61 | + const isCSS = isCSSLink || asset.tag === "style"; |
| 62 | + if (isCSS) { |
| 63 | + cssKeys.push(entity.key); |
| 64 | + } |
| 65 | + |
| 66 | + entity.consumers++; |
| 67 | + if (entity.consumers > 1 || entity.el) continue; |
| 68 | + |
| 69 | + // Mounting logic |
| 70 | + if (isServer) { |
| 71 | + useAssets_(() => renderAsset(asset, nonce)); |
| 72 | + entity.ssrIdx = ssrRequestAssets.length - 1; |
| 73 | + } else { |
| 74 | + const el = (entity.el = document.createElement(asset.tag)); |
| 75 | + |
| 76 | + if (cssSuspense && isCSSLink) { |
| 77 | + cssPromises.push( |
| 78 | + new Promise(res => { |
| 79 | + el.addEventListener("load", res, { once: true }); |
| 80 | + el.addEventListener(CANCEL_EVENT, res, { once: true }); |
| 81 | + el.addEventListener("error", res, { once: true }); |
| 82 | + }) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if (EXPERIMENTAL_BLOCKING && isCSS) { |
| 87 | + el.setAttribute("blocking", "render"); |
| 88 | + } |
| 89 | + |
| 90 | + for (const k of Object.keys(asset.attrs)) { |
| 91 | + if (k === "key") continue; |
| 92 | + el.setAttribute(k, asset.attrs[k as AttrKeys]); |
| 93 | + } |
| 94 | + |
| 95 | + if ("children" in asset) { |
| 96 | + el.innerHTML = asset.children as string; |
| 97 | + } |
| 98 | + |
| 99 | + document.head.appendChild(el); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (cssSuspense && cssPromises.length) { |
| 104 | + const [r] = createResource(() => Promise.all(cssPromises)); |
| 105 | + createRenderEffect(r); |
| 106 | + } |
| 107 | + |
| 108 | + // Unmounting logic |
| 109 | + onCleanup(() => { |
| 110 | + for (const key of cssKeys) { |
| 111 | + const entity = registry[key]!; |
| 112 | + entity.consumers--; |
| 113 | + if (entity.consumers != 0) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + if (isServer) { |
| 118 | + // Ideally this logic should be implemented directly in dom-expressions |
| 119 | + ssrRequestAssets.splice(entity.ssrIdx!, 1, NOOP); |
| 120 | + } else { |
| 121 | + if (EXPERIMENTAL_CSS_SUSPENSE) entity.el!.dispatchEvent(new CustomEvent(CANCEL_EVENT)); |
| 122 | + entity.el!.remove(); |
| 123 | + delete registry[key]; |
| 124 | + } |
| 125 | + } |
| 126 | + }); |
| 127 | +}; |
0 commit comments