@waldjs/canopy 0.1.0 → 0.2.0
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/prefetch.d.ts +3 -0
- package/dist/prefetch.d.ts.map +1 -0
- package/dist/prefetch.js +47 -0
- package/dist/prefetch.js.map +1 -0
- package/package.json +5 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefetch.d.ts","sourceRoot":"","sources":["../src/prefetch.ts"],"names":[],"mappings":"AAOA,wBAAgB,eAAe,IAAI,IAAI,CAmCtC;AAED,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C"}
|
package/dist/prefetch.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Powers wald:prefetch="hover" / wald:prefetch="visible" on any element with
|
|
2
|
+
// an href. Unlike canopy islands, this needs no compiler support — the
|
|
3
|
+
// attribute is plain unrecognized HTML on a lowercase tag, so it passes
|
|
4
|
+
// through the compiler untouched (same mechanism as data-wald-no-hoist).
|
|
5
|
+
// installPrefetch() itself is serialized via .toString() (see
|
|
6
|
+
// prefetchRuntimeScript()) and inlined as a raw <script> only on pages that
|
|
7
|
+
// actually use the directive — see packages/cli/src/prefetch-runtime.ts.
|
|
8
|
+
export function installPrefetch() {
|
|
9
|
+
const seen = new Set();
|
|
10
|
+
function prefetch(href) {
|
|
11
|
+
if (!href || seen.has(href))
|
|
12
|
+
return;
|
|
13
|
+
seen.add(href);
|
|
14
|
+
const link = document.createElement('link');
|
|
15
|
+
link.rel = 'prefetch';
|
|
16
|
+
link.href = href;
|
|
17
|
+
document.head.appendChild(link);
|
|
18
|
+
}
|
|
19
|
+
// Filtered in JS rather than via a `[wald\:prefetch]` CSS attribute
|
|
20
|
+
// selector — colon-escaping in attribute selectors is inconsistent across
|
|
21
|
+
// DOM implementations (confirmed failing in happy-dom during testing).
|
|
22
|
+
document.querySelectorAll('[href]').forEach((el) => {
|
|
23
|
+
const strategy = el.getAttribute('wald:prefetch');
|
|
24
|
+
const href = el.getAttribute('href');
|
|
25
|
+
if (!strategy || !href)
|
|
26
|
+
return;
|
|
27
|
+
if (strategy === 'hover') {
|
|
28
|
+
el.addEventListener('mouseenter', () => prefetch(href), { once: true });
|
|
29
|
+
}
|
|
30
|
+
else if (strategy === 'visible' && typeof IntersectionObserver !== 'undefined') {
|
|
31
|
+
const observer = new IntersectionObserver((entries) => {
|
|
32
|
+
for (const entry of entries) {
|
|
33
|
+
if (entry.isIntersecting) {
|
|
34
|
+
prefetch(href);
|
|
35
|
+
observer.disconnect();
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
observer.observe(el);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export function prefetchRuntimeScript() {
|
|
45
|
+
return `(${installPrefetch.toString()})();`;
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=prefetch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefetch.js","sourceRoot":"","sources":["../src/prefetch.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,uEAAuE;AACvE,wEAAwE;AACxE,yEAAyE;AACzE,8DAA8D;AAC9D,4EAA4E;AAC5E,yEAAyE;AACzE,MAAM,UAAU,eAAe;IAC7B,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAA;IAE9B,SAAS,QAAQ,CAAC,IAAY;QAC5B,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;YAAE,OAAM;QACnC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QACd,MAAM,IAAI,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAC3C,IAAI,CAAC,GAAG,GAAG,UAAU,CAAA;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC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version": "0.2.0",
|
|
4
4
|
"description": "Client-side hydration (islands) for WaldJS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -16,6 +16,10 @@
|
|
|
16
16
|
".": {
|
|
17
17
|
"import": "./dist/index.js",
|
|
18
18
|
"types": "./dist/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./prefetch": {
|
|
21
|
+
"import": "./dist/prefetch.js",
|
|
22
|
+
"types": "./dist/prefetch.d.ts"
|
|
19
23
|
}
|
|
20
24
|
},
|
|
21
25
|
"files": [
|