@solidjs/web 2.0.0-beta.29 → 2.0.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dev.cjs CHANGED
@@ -170,6 +170,89 @@ function reconcileArrays(parentNode, a, b, marker) {
170
170
  }
171
171
  }
172
172
 
173
+ const HEAD_ELIGIBLE_TAGS = new Set(["title", "meta", "link", "style", "script", "base"]);
174
+ const HEAD_ATTR_NAME = /^[a-zA-Z_][a-zA-Z0-9_:.-]*$/;
175
+ const RESOURCE_LINK_RELS = new Set(["preload", "modulepreload", "prefetch", "preconnect", "dns-prefetch", "icon", "stylesheet"]);
176
+ const RESOURCE_QUALIFIERS = ["as", "crossorigin", "type", "media", "imagesrcset", "imagesizes"];
177
+ function evalHeadValue(v) {
178
+ return typeof v === "function" ? v() : v;
179
+ }
180
+ function evalHeadProps(props, presets) {
181
+ const out = {};
182
+ for (const name in props) out[name] = presets && name in presets ? presets[name] : evalHeadValue(props[name]);
183
+ return out;
184
+ }
185
+ function classifyHeadTag(desc) {
186
+ const tag = desc.tag;
187
+ if (tag === "link") {
188
+ const rel = evalHeadValue(desc.props && desc.props.rel);
189
+ return {
190
+ resource: RESOURCE_LINK_RELS.has(rel),
191
+ rel
192
+ };
193
+ }
194
+ if (tag === "style") return {
195
+ resource: !!(desc.props && "href" in desc.props)
196
+ };
197
+ if (tag === "script") return {
198
+ resource: !!(desc.props && "src" in desc.props)
199
+ };
200
+ return {
201
+ resource: false
202
+ };
203
+ }
204
+ function resourceIdentity(tag, props) {
205
+ let id = "res:" + tag + ":" + (props.rel || "") + ":" + (props.href || props.src || "");
206
+ for (let i = 0; i < RESOURCE_QUALIFIERS.length; i++) {
207
+ const q = RESOURCE_QUALIFIERS[i];
208
+ if (props[q] != null) id += ":" + q + "=" + props[q];
209
+ }
210
+ return id;
211
+ }
212
+ function replaceableIdentity(tag, props, key, unique) {
213
+ if (tag === "title") return "title";
214
+ if (tag === "base") return "base";
215
+ if (tag === "meta" && props.charset != null) return "charset";
216
+ if (key != null) return tag + ":key:" + key;
217
+ if (tag === "meta") {
218
+ if (props.name != null) return "meta:name:" + props.name;
219
+ if (props.property != null) return "meta:property:" + props.property;
220
+ if (props["http-equiv"] != null) return "meta:http-equiv:" + props["http-equiv"];
221
+ return unique;
222
+ }
223
+ if (tag === "link") return "link:" + (props.rel || "") + ":" + (props.href || "");
224
+ return unique;
225
+ }
226
+ function resolveHead(groups) {
227
+ const winners = new Map();
228
+ const sorted = groups.slice().sort((a, b) => a.seq - b.seq);
229
+ for (let i = 0; i < sorted.length; i++) {
230
+ const group = sorted[i];
231
+ const byIdentity = new Map();
232
+ for (let j = 0; j < group.tags.length; j++) {
233
+ const t = group.tags[j];
234
+ let list = byIdentity.get(t.identity);
235
+ if (!list) byIdentity.set(t.identity, list = []);
236
+ list.push(t);
237
+ }
238
+ for (const [identity, tags] of byIdentity) {
239
+ if (identity === "title") {
240
+ if (tags.length > 1) console.warn("Multiple <title> tags in one head group; the last one wins.");
241
+ winners.set(identity, {
242
+ seq: group.seq,
243
+ tags: [tags[tags.length - 1]]
244
+ });
245
+ } else {
246
+ winners.set(identity, {
247
+ seq: group.seq,
248
+ tags
249
+ });
250
+ }
251
+ }
252
+ }
253
+ return winners;
254
+ }
255
+
173
256
  const $$EVENT_OWNER = "_$DX_EVENT_OWNER";
174
257
  const INNER_OWNED = {};
175
258
  const delegatedEvents = new Set();
@@ -655,6 +738,235 @@ function acquireAsset(descriptor) {
655
738
  }, ASSET_REMOVAL_GRACE);
656
739
  };
657
740
  }
741
+ let headRegistrations = null;
742
+ let headSeq = 0;
743
+ let headUid = 0;
744
+ let headOwned = null;
745
+ let headApplied = null;
746
+ let headFallbackTitle = null;
747
+ let headScheduled = false;
748
+ const headMountedResources = new Set();
749
+ function initHeadRegistry() {
750
+ if (headRegistrations) return;
751
+ headRegistrations = [];
752
+ headOwned = new Map();
753
+ headApplied = new Map();
754
+ const t = document.querySelector("title");
755
+ headFallbackTitle = t && !t.hasAttribute("data-dh") ? t.textContent : null;
756
+ if (globalThis._$HY) globalThis._$HY.h = applyServerHeadOps;
757
+ }
758
+ function applyServerHeadOps(ops) {
759
+ for (let i = 0; i < ops.length; i++) {
760
+ const op = ops[i];
761
+ const identity = op[0] === "t" ? "title" : op[1];
762
+ if (headOwned.has(identity)) continue;
763
+ if (op[0] === "t") setHeadTitle(op[1]);else if (op[0] === "r") {
764
+ const els = headMarkedElements(identity);
765
+ for (let j = 0; j < els.length; j++) els[j].remove();
766
+ } else {
767
+ const el = document.createElement(op[2]);
768
+ for (const name in op[3]) el.setAttribute(name, op[3][name]);
769
+ if (op[4] != null) el.textContent = op[4];
770
+ el.setAttribute("data-dh", identity);
771
+ document.head.appendChild(el);
772
+ }
773
+ }
774
+ }
775
+ function headMarkedElements(identity) {
776
+ const nodes = document.head.querySelectorAll("[data-dh]");
777
+ const out = [];
778
+ for (let i = 0; i < nodes.length; i++) {
779
+ if (nodes[i].getAttribute("data-dh") === identity) out.push(nodes[i]);
780
+ }
781
+ return out;
782
+ }
783
+ function setHeadTitle(text) {
784
+ let el = document.querySelector("title");
785
+ if (!el) {
786
+ el = document.createElement("title");
787
+ document.head.appendChild(el);
788
+ }
789
+ el.textContent = text;
790
+ el.setAttribute("data-dh", "title");
791
+ return el;
792
+ }
793
+ function scheduleHeadApply() {
794
+ if (headScheduled) return;
795
+ headScheduled = true;
796
+ queueMicrotask(flushHeadRegistry);
797
+ }
798
+ function flushHeadRegistry() {
799
+ if (solidJs.sharedConfig.hydrating) {
800
+ setTimeout(flushHeadRegistry, 0);
801
+ return;
802
+ }
803
+ headScheduled = false;
804
+ const winners = resolveHead(headRegistrations);
805
+ for (const [identity, els] of headOwned) {
806
+ if (winners.has(identity)) continue;
807
+ headOwned.delete(identity);
808
+ headApplied.delete(identity);
809
+ if (identity === "title") {
810
+ if (headFallbackTitle != null) setHeadTitle(headFallbackTitle).removeAttribute("data-dh");
811
+ } else {
812
+ for (let i = 0; i < els.length; i++) els[i].remove();
813
+ }
814
+ }
815
+ for (const [identity, winner] of winners) {
816
+ let sig = "";
817
+ for (let i = 0; i < winner.tags.length; i++) sig += winner.tags[i].tag + JSON.stringify(winner.tags[i].props) + "|";
818
+ if (headApplied.get(identity) === sig) continue;
819
+ headApplied.set(identity, sig);
820
+ if (identity === "base" || identity === "charset") {
821
+ console.warn(`useHead: <${winner.tags[0].tag}> (${identity}) is shell-only and ignored on the client`);
822
+ continue;
823
+ }
824
+ if (identity === "title") {
825
+ const children = winner.tags[0].props.children;
826
+ setHeadTitle(children == null ? "" : String(children));
827
+ headOwned.set(identity, []);
828
+ continue;
829
+ }
830
+ const existing = headMarkedElements(identity);
831
+ const els = [];
832
+ for (let i = 0; i < winner.tags.length; i++) {
833
+ els.push(renderHeadElement(winner.tags[i], identity, existing));
834
+ }
835
+ for (let i = 0; i < existing.length; i++) {
836
+ if (els.indexOf(existing[i]) === -1) existing[i].remove();
837
+ }
838
+ headOwned.set(identity, els);
839
+ }
840
+ }
841
+ function renderHeadElement(t, identity, existing) {
842
+ for (let i = 0; i < existing.length; i++) {
843
+ if (headElementMatches(existing[i], t)) return existing.splice(i, 1)[0];
844
+ }
845
+ const el = document.createElement(t.tag);
846
+ for (const name in t.props) {
847
+ if (name === "children" || name === "ref" || name.slice(0, 2) === "on") continue;
848
+ if (!HEAD_ATTR_NAME.test(name)) {
849
+ console.warn(`useHead: ignoring invalid attribute name "${name}"`);
850
+ continue;
851
+ }
852
+ const v = t.props[name];
853
+ if (v == null || v === false) continue;
854
+ el.setAttribute(name, v === true ? "" : String(v));
855
+ }
856
+ if (t.props.children != null) el.textContent = String(t.props.children);
857
+ el.setAttribute("data-dh", identity);
858
+ document.head.appendChild(el);
859
+ return el;
860
+ }
861
+ function headElementMatches(el, t) {
862
+ if (el.tagName.toLowerCase() !== t.tag) return false;
863
+ for (const name in t.props) {
864
+ if (name === "children" || name === "ref" || name.slice(0, 2) === "on") continue;
865
+ if (!HEAD_ATTR_NAME.test(name)) continue;
866
+ const v = t.props[name];
867
+ if (v == null || v === false) {
868
+ if (el.hasAttribute(name)) return false;
869
+ } else if (el.getAttribute(name) !== (v === true ? "" : String(v))) return false;
870
+ }
871
+ const children = t.props.children;
872
+ return (children == null ? "" : String(children)) === el.textContent;
873
+ }
874
+ function acquireHeadResource(tag, props) {
875
+ if (tag === "link" && (props.rel === "stylesheet" || props.rel === "modulepreload")) {
876
+ const descriptor = {
877
+ type: props.rel === "stylesheet" ? "style" : "module",
878
+ href: props.href
879
+ };
880
+ let attrs = null;
881
+ for (const name in props) {
882
+ if (name === "rel" || name === "href") continue;
883
+ if (!HEAD_ATTR_NAME.test(name)) continue;
884
+ const v = props[name];
885
+ if (v == null || v === false) continue;
886
+ (attrs || (attrs = {}))[name] = v === true ? "" : String(v);
887
+ }
888
+ if (attrs) descriptor.attrs = attrs;
889
+ return acquireAsset(descriptor);
890
+ }
891
+ const identity = resourceIdentity(tag, props);
892
+ if (headMountedResources.has(identity)) return noopFn;
893
+ headMountedResources.add(identity);
894
+ const url = props.href || props.src;
895
+ let el = null;
896
+ if (url != null) {
897
+ if (tag === "link") el = findAssetElement(`link[rel="${props.rel}"]`, "href", url);else if (tag === "script") el = findAssetElement("script[src]", "src", url);else el = findAssetElement("style[href]", "href", url);
898
+ }
899
+ if (!el) {
900
+ el = document.createElement(tag);
901
+ for (const name in props) {
902
+ if (name === "children" || name === "ref" || name.slice(0, 2) === "on") continue;
903
+ if (!HEAD_ATTR_NAME.test(name)) continue;
904
+ const v = props[name];
905
+ if (v == null || v === false) continue;
906
+ el.setAttribute(name, v === true ? "" : String(v));
907
+ }
908
+ if (props.children != null) el.textContent = String(props.children);
909
+ document.head.appendChild(el);
910
+ }
911
+ return noopFn;
912
+ }
913
+ function noopFn() {}
914
+ function useHead(tags) {
915
+ const list = Array.isArray(tags) ? tags : [tags];
916
+ initHeadRegistry();
917
+ const reg = {
918
+ seq: -1,
919
+ tags: null
920
+ };
921
+ const uid = ++headUid;
922
+ effect(() => {
923
+ const replaceable = [];
924
+ const resources = [];
925
+ for (let i = 0; i < list.length; i++) {
926
+ const desc = list[i];
927
+ if (!desc || !HEAD_ELIGIBLE_TAGS.has(desc.tag)) {
928
+ console.warn(`useHead: ignoring non-head tag`, desc);
929
+ continue;
930
+ }
931
+ const cls = classifyHeadTag(desc);
932
+ const props = evalHeadProps(desc.props || {}, cls.rel !== undefined ? {
933
+ rel: cls.rel
934
+ } : undefined);
935
+ if (cls.resource) {
936
+ resources.push({
937
+ tag: desc.tag,
938
+ props
939
+ });
940
+ } else {
941
+ const key = evalHeadValue(desc.key);
942
+ replaceable.push({
943
+ tag: desc.tag,
944
+ props,
945
+ identity: replaceableIdentity(desc.tag, props, key, "u:c" + uid + ":" + i)
946
+ });
947
+ }
948
+ }
949
+ return {
950
+ replaceable,
951
+ resources
952
+ };
953
+ }, evaluated => {
954
+ const releases = [];
955
+ for (let i = 0; i < evaluated.resources.length; i++) {
956
+ releases.push(acquireHeadResource(evaluated.resources[i].tag, evaluated.resources[i].props));
957
+ }
958
+ reg.tags = evaluated.replaceable;
959
+ if (reg.seq < 0) reg.seq = ++headSeq;
960
+ if (headRegistrations.indexOf(reg) === -1) headRegistrations.push(reg);
961
+ scheduleHeadApply();
962
+ return () => {
963
+ const idx = headRegistrations.indexOf(reg);
964
+ if (idx > -1) headRegistrations.splice(idx, 1);
965
+ for (let i = 0; i < releases.length; i++) releases[i]();
966
+ scheduleHeadApply();
967
+ };
968
+ });
969
+ }
658
970
  function loadModuleAssets(mapping) {
659
971
  const hy = globalThis._$HY;
660
972
  if (!hy) return;
@@ -1310,8 +1622,21 @@ function portalImpl(props) {
1310
1622
  });
1311
1623
  return treeMarker;
1312
1624
  }
1625
+ const COMPONENT_HANDOFF = Symbol.for("dom-expressions.component-handoff");
1626
+ function resolveHandoff(next, prev) {
1627
+ const handoff = typeof next === "function" && next !== null && next[COMPONENT_HANDOFF];
1628
+ return handoff && handoff.take(prev) ? prev : next;
1629
+ }
1313
1630
  function dynamic(source) {
1314
- const cached = solidJs.createMemo(source, {
1631
+ let latest = 0;
1632
+ const cached = solidJs.createMemo(prev => {
1633
+ const next = source();
1634
+ if (!next || typeof next.then !== "function") return resolveHandoff(next, prev);
1635
+ const token = ++latest;
1636
+ return {
1637
+ then: (onFulfilled, onRejected) => next.then(resolved => onFulfilled(token === latest ? resolveHandoff(resolved, prev) : resolved), onRejected)
1638
+ };
1639
+ }, {
1315
1640
  lazy: true
1316
1641
  });
1317
1642
  return props => {
@@ -1343,7 +1668,8 @@ function createElement(tagName, is = undefined) {
1343
1668
  function loadClientOnly(fn, setComp) {
1344
1669
  fn().then(m => setComp(() => m.default));
1345
1670
  }
1346
- function clientOnly(fn, options = {}) {
1671
+ function clientOnly(fn, options = {},
1672
+ _moduleUrl) {
1347
1673
  const [comp, setComp] = solidJs.createSignal();
1348
1674
  let started = !options.lazy;
1349
1675
  started && loadClientOnly(fn, setComp);
@@ -1500,3 +1826,4 @@ exports.template = template;
1500
1826
  exports.unregisterDelegatedContainer = unregisterDelegatedContainer;
1501
1827
  exports.unregisterDelegatedRoot = unregisterDelegatedRoot;
1502
1828
  exports.useAssets = voidFn;
1829
+ exports.useHead = useHead;