|
| 1 | +import { createRenderEffect, createResource, onCleanup, sharedConfig } from "solid-js"; |
| 2 | +import { getRequestEvent, isServer, useAssets } from "solid-js/web"; |
| 3 | +import { renderAsset, type Asset } from "../server/renderAsset.tsx"; |
| 4 | + |
| 5 | +const CANCEL_EVENT = "cancel"; |
| 6 | +const EVENT_REGISTRY = Symbol("assetRegistry"); |
| 7 | +const NOOP = () => ""; |
| 8 | + |
| 9 | +type AssetEntity = { key: string; consumers: number; el?: HTMLElement; ssrIdx?: number }; |
| 10 | +type Registry = Record<string, AssetEntity>; |
| 11 | +type AttrKeys = keyof Asset["attrs"]; |
| 12 | + |
| 13 | +const globalRegistry: Registry = {}; |
| 14 | + |
| 15 | +const getEntity = (registry: Registry, asset: Asset) => { |
| 16 | + let key = asset.tag; |
| 17 | + for (const k of Object.keys(asset.attrs)) { |
| 18 | + if (k === "key") continue; |
| 19 | + key += `[${k}='${asset.attrs[k as keyof Asset["attrs"]]}']`; |
| 20 | + } |
| 21 | + |
| 22 | + const entity = (registry[key] ??= { |
| 23 | + key, |
| 24 | + consumers: 0, |
| 25 | + el: isServer ? undefined : (document.querySelector("head " + key) as HTMLElement) |
| 26 | + }); |
| 27 | + |
| 28 | + return entity; |
| 29 | +}; |
| 30 | + |
| 31 | +export const mountAssets = ( |
| 32 | + assets: Asset[], |
| 33 | + { unmount = true, nonce }: { unmount?: boolean; nonce?: string } = {} |
| 34 | +) => { |
| 35 | + if (!assets.length) return; |
| 36 | + |
| 37 | + const registry: Registry = isServer |
| 38 | + ? (getRequestEvent()!.locals[EVENT_REGISTRY] ??= {}) |
| 39 | + : globalRegistry; |
| 40 | + const ssrRequestAssets: Function[] = (sharedConfig.context as any)?.assets; |
| 41 | + const cssKeys: string[] = []; |
| 42 | + |
| 43 | + for (const asset of assets) { |
| 44 | + const entity = getEntity(registry, asset); |
| 45 | + const isCSS = asset.tag === "link" && asset.attrs.rel === "stylesheet"; |
| 46 | + if (isCSS && entity.el?.dataset.keep != "") { |
| 47 | + cssKeys.push(entity.key); |
| 48 | + } |
| 49 | + |
| 50 | + entity.consumers++; |
| 51 | + if (entity.consumers > 1 || entity.el) continue; |
| 52 | + |
| 53 | + // Mounting logic |
| 54 | + if (isServer) { |
| 55 | + if (!unmount && isCSS) { |
| 56 | + asset.attrs["data-keep" as AttrKeys] = ""; |
| 57 | + } |
| 58 | + useAssets(() => renderAsset(asset, nonce)); |
| 59 | + entity.ssrIdx = ssrRequestAssets.length - 1; |
| 60 | + } else { |
| 61 | + const el = (entity.el = document.createElement(asset.tag)); |
| 62 | + |
| 63 | + if (isCSS) { |
| 64 | + const [r] = createResource(() => { |
| 65 | + return new Promise(res => { |
| 66 | + el.addEventListener("load", res, { once: true }); |
| 67 | + el.addEventListener(CANCEL_EVENT, res, { once: true }); |
| 68 | + el.addEventListener("error", res, { once: true }); |
| 69 | + }); |
| 70 | + }); |
| 71 | + createRenderEffect(r); |
| 72 | + } |
| 73 | + |
| 74 | + for (const k of Object.keys(asset.attrs)) { |
| 75 | + if (k === "key") continue; |
| 76 | + el.setAttribute(k, asset.attrs[k as AttrKeys]); |
| 77 | + } |
| 78 | + document.head.appendChild(el); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Unmounting logic |
| 83 | + if (!unmount) return; |
| 84 | + onCleanup(() => { |
| 85 | + for (const key of cssKeys) { |
| 86 | + const entity = registry[key]!; |
| 87 | + entity.consumers--; |
| 88 | + if (entity.consumers != 0) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + if (isServer) { |
| 93 | + // Ideally this logic should be implemented directly in dom-expressions |
| 94 | + ssrRequestAssets.splice(entity.ssrIdx!, 1, NOOP); |
| 95 | + } else { |
| 96 | + entity.el!.dispatchEvent(new CustomEvent(CANCEL_EVENT)); |
| 97 | + entity.el!.remove(); |
| 98 | + delete registry[key]; |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | +}; |
0 commit comments