@wordpress/interactivity-router 2.18.0 → 2.19.1
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/CHANGELOG.md +6 -0
- package/README.md +5 -4
- package/build/head.js +113 -0
- package/build/head.js.map +1 -0
- package/build/index.js +21 -23
- package/build/index.js.map +1 -1
- package/build-module/head.js +103 -0
- package/build-module/head.js.map +1 -0
- package/build-module/index.js +21 -23
- package/build-module/index.js.map +1 -1
- package/build-types/head.d.ts +24 -0
- package/build-types/head.d.ts.map +1 -0
- package/build-types/index.d.ts +0 -1
- package/build-types/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/head.ts +126 -0
- package/src/index.ts +25 -40
- package/tsconfig.tsbuildinfo +1 -1
- package/build/assets/styles.js +0 -68
- package/build/assets/styles.js.map +0 -1
- package/build-module/assets/styles.js +0 -61
- package/build-module/assets/styles.js.map +0 -1
- package/build-types/assets/styles.d.ts +0 -2
- package/build-types/assets/styles.d.ts.map +0 -1
- package/src/assets/styles.ts +0 -80
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -17,12 +17,13 @@ The package is intended to be imported dynamically in the `view.js` files of int
|
|
|
17
17
|
```js
|
|
18
18
|
/* view.js */
|
|
19
19
|
|
|
20
|
-
import { store } from '@wordpress/interactivity';
|
|
20
|
+
import { store, withSyncEvent } from '@wordpress/interactivity';
|
|
21
21
|
|
|
22
22
|
// This is how you would typically use the navigate() action in your block.
|
|
23
23
|
store( 'my-namespace/myblock', {
|
|
24
24
|
actions: {
|
|
25
|
-
|
|
25
|
+
// The withSyncEvent() utility needs to be used because preventDefault() requires synchronous event access.
|
|
26
|
+
goToPage: withSyncEvent( function* ( e ) {
|
|
26
27
|
e.preventDefault();
|
|
27
28
|
|
|
28
29
|
// We import the package dynamically to reduce the initial JS bundle size.
|
|
@@ -31,7 +32,7 @@ store( 'my-namespace/myblock', {
|
|
|
31
32
|
'@wordpress/interactivity-router'
|
|
32
33
|
);
|
|
33
34
|
yield actions.navigate( e.target.href );
|
|
34
|
-
},
|
|
35
|
+
} ),
|
|
35
36
|
},
|
|
36
37
|
} );
|
|
37
38
|
```
|
|
@@ -80,7 +81,7 @@ Example:
|
|
|
80
81
|
<li><a href="/post-2">Post 2</a></li>
|
|
81
82
|
<li><a href="/post-3">Post 3</a></li>
|
|
82
83
|
</ul>
|
|
83
|
-
<a data-wp-on--click="actions.navigate" href="/page/2">
|
|
84
|
+
<a data-wp-on--click="actions.navigate" href="/page/2">Page 2</a>
|
|
84
85
|
</div>
|
|
85
86
|
```
|
|
86
87
|
|
package/build/head.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateHead = exports.headElements = exports.fetchHeadAssets = void 0;
|
|
7
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
8
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
9
|
+
/**
|
|
10
|
+
* The cache of prefetched stylesheets and scripts.
|
|
11
|
+
*/
|
|
12
|
+
const headElements = exports.headElements = new Map();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Helper to update only the necessary tags in the head.
|
|
16
|
+
*
|
|
17
|
+
* @async
|
|
18
|
+
* @param newHead The head elements of the new page.
|
|
19
|
+
*/
|
|
20
|
+
const updateHead = async newHead => {
|
|
21
|
+
// Helper to get the tag id store in the cache.
|
|
22
|
+
const getTagId = tag => tag.id || tag.outerHTML;
|
|
23
|
+
|
|
24
|
+
// Map incoming head tags by their content.
|
|
25
|
+
const newHeadMap = new Map();
|
|
26
|
+
for (const child of newHead) {
|
|
27
|
+
newHeadMap.set(getTagId(child), child);
|
|
28
|
+
}
|
|
29
|
+
const toRemove = [];
|
|
30
|
+
|
|
31
|
+
// Detect nodes that should be added or removed.
|
|
32
|
+
for (const child of document.head.children) {
|
|
33
|
+
const id = getTagId(child);
|
|
34
|
+
// Always remove styles and links as they might change.
|
|
35
|
+
if (child.nodeName === 'LINK' || child.nodeName === 'STYLE') {
|
|
36
|
+
toRemove.push(child);
|
|
37
|
+
} else if (newHeadMap.has(id)) {
|
|
38
|
+
newHeadMap.delete(id);
|
|
39
|
+
} else if (child.nodeName !== 'SCRIPT' && child.nodeName !== 'META') {
|
|
40
|
+
toRemove.push(child);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
await Promise.all([...headElements.entries()].filter(([, {
|
|
44
|
+
tag
|
|
45
|
+
}]) => tag.nodeName === 'SCRIPT').map(async ([url]) => {
|
|
46
|
+
await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(/* webpackIgnore: true */url);
|
|
47
|
+
}));
|
|
48
|
+
|
|
49
|
+
// Prepare new assets.
|
|
50
|
+
const toAppend = [...newHeadMap.values()];
|
|
51
|
+
|
|
52
|
+
// Apply the changes.
|
|
53
|
+
toRemove.forEach(n => n.remove());
|
|
54
|
+
document.head.append(...toAppend);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Fetches and processes head assets (stylesheets and scripts) from a specified document.
|
|
59
|
+
*
|
|
60
|
+
* @async
|
|
61
|
+
* @param doc The document from which to fetch head assets. It should support standard DOM querying methods.
|
|
62
|
+
*
|
|
63
|
+
* @return Returns an array of HTML elements representing the head assets.
|
|
64
|
+
*/
|
|
65
|
+
exports.updateHead = updateHead;
|
|
66
|
+
const fetchHeadAssets = async doc => {
|
|
67
|
+
const headTags = [];
|
|
68
|
+
|
|
69
|
+
// We only want to fetch module scripts because regular scripts (without
|
|
70
|
+
// `async` or `defer` attributes) can depend on the execution of other scripts.
|
|
71
|
+
// Scripts found in the head are blocking and must be executed in order.
|
|
72
|
+
const scripts = doc.querySelectorAll('script[type="module"][src]');
|
|
73
|
+
scripts.forEach(script => {
|
|
74
|
+
const src = script.getAttribute('src');
|
|
75
|
+
if (!headElements.has(src)) {
|
|
76
|
+
// add the <link> elements to prefetch the module scripts
|
|
77
|
+
const link = doc.createElement('link');
|
|
78
|
+
link.rel = 'modulepreload';
|
|
79
|
+
link.href = src;
|
|
80
|
+
document.head.append(link);
|
|
81
|
+
headElements.set(src, {
|
|
82
|
+
tag: script
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const stylesheets = doc.querySelectorAll('link[rel=stylesheet]');
|
|
87
|
+
await Promise.all(Array.from(stylesheets).map(async tag => {
|
|
88
|
+
const href = tag.getAttribute('href');
|
|
89
|
+
if (!href) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!headElements.has(href)) {
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(href);
|
|
95
|
+
const text = await response.text();
|
|
96
|
+
headElements.set(href, {
|
|
97
|
+
tag,
|
|
98
|
+
text
|
|
99
|
+
});
|
|
100
|
+
} catch (e) {
|
|
101
|
+
// eslint-disable-next-line no-console
|
|
102
|
+
console.error(e);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const headElement = headElements.get(href);
|
|
106
|
+
const styleElement = doc.createElement('style');
|
|
107
|
+
styleElement.textContent = headElement.text;
|
|
108
|
+
headTags.push(styleElement);
|
|
109
|
+
}));
|
|
110
|
+
return [doc.querySelector('title'), ...doc.querySelectorAll('style'), ...headTags];
|
|
111
|
+
};
|
|
112
|
+
exports.fetchHeadAssets = fetchHeadAssets;
|
|
113
|
+
//# sourceMappingURL=head.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["headElements","exports","Map","updateHead","newHead","getTagId","tag","id","outerHTML","newHeadMap","child","set","toRemove","document","head","children","nodeName","push","has","delete","Promise","all","entries","filter","map","url","specifier","r","then","s","_interopRequireWildcard","require","toAppend","values","forEach","n","remove","append","fetchHeadAssets","doc","headTags","scripts","querySelectorAll","script","src","getAttribute","link","createElement","rel","href","stylesheets","Array","from","response","fetch","text","e","console","error","headElement","get","styleElement","textContent","querySelector"],"sources":["@wordpress/interactivity-router/src/head.ts"],"sourcesContent":["/**\n * The cache of prefetched stylesheets and scripts.\n */\nexport const headElements = new Map<\n\tstring,\n\t{ tag: HTMLElement; text?: string }\n>();\n\n/**\n * Helper to update only the necessary tags in the head.\n *\n * @async\n * @param newHead The head elements of the new page.\n */\nexport const updateHead = async ( newHead: HTMLHeadElement[] ) => {\n\t// Helper to get the tag id store in the cache.\n\tconst getTagId = ( tag: Element ) => tag.id || tag.outerHTML;\n\n\t// Map incoming head tags by their content.\n\tconst newHeadMap = new Map< string, Element >();\n\tfor ( const child of newHead ) {\n\t\tnewHeadMap.set( getTagId( child ), child );\n\t}\n\n\tconst toRemove: Element[] = [];\n\n\t// Detect nodes that should be added or removed.\n\tfor ( const child of document.head.children ) {\n\t\tconst id = getTagId( child );\n\t\t// Always remove styles and links as they might change.\n\t\tif ( child.nodeName === 'LINK' || child.nodeName === 'STYLE' ) {\n\t\t\ttoRemove.push( child );\n\t\t} else if ( newHeadMap.has( id ) ) {\n\t\t\tnewHeadMap.delete( id );\n\t\t} else if ( child.nodeName !== 'SCRIPT' && child.nodeName !== 'META' ) {\n\t\t\ttoRemove.push( child );\n\t\t}\n\t}\n\n\tawait Promise.all(\n\t\t[ ...headElements.entries() ]\n\t\t\t.filter( ( [ , { tag } ] ) => tag.nodeName === 'SCRIPT' )\n\t\t\t.map( async ( [ url ] ) => {\n\t\t\t\tawait import( /* webpackIgnore: true */ url );\n\t\t\t} )\n\t);\n\n\t// Prepare new assets.\n\tconst toAppend = [ ...newHeadMap.values() ];\n\n\t// Apply the changes.\n\ttoRemove.forEach( ( n ) => n.remove() );\n\tdocument.head.append( ...toAppend );\n};\n\n/**\n * Fetches and processes head assets (stylesheets and scripts) from a specified document.\n *\n * @async\n * @param doc The document from which to fetch head assets. It should support standard DOM querying methods.\n *\n * @return Returns an array of HTML elements representing the head assets.\n */\nexport const fetchHeadAssets = async (\n\tdoc: Document\n): Promise< HTMLElement[] > => {\n\tconst headTags = [];\n\n\t// We only want to fetch module scripts because regular scripts (without\n\t// `async` or `defer` attributes) can depend on the execution of other scripts.\n\t// Scripts found in the head are blocking and must be executed in order.\n\tconst scripts = doc.querySelectorAll< HTMLScriptElement >(\n\t\t'script[type=\"module\"][src]'\n\t);\n\n\tscripts.forEach( ( script ) => {\n\t\tconst src = script.getAttribute( 'src' );\n\t\tif ( ! headElements.has( src ) ) {\n\t\t\t// add the <link> elements to prefetch the module scripts\n\t\t\tconst link = doc.createElement( 'link' );\n\t\t\tlink.rel = 'modulepreload';\n\t\t\tlink.href = src;\n\t\t\tdocument.head.append( link );\n\t\t\theadElements.set( src, { tag: script } );\n\t\t}\n\t} );\n\n\tconst stylesheets = doc.querySelectorAll< HTMLLinkElement >(\n\t\t'link[rel=stylesheet]'\n\t);\n\n\tawait Promise.all(\n\t\tArray.from( stylesheets ).map( async ( tag ) => {\n\t\t\tconst href = tag.getAttribute( 'href' );\n\t\t\tif ( ! href ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! headElements.has( href ) ) {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await fetch( href );\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\theadElements.set( href, {\n\t\t\t\t\t\ttag,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t} );\n\t\t\t\t} catch ( e ) {\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.error( e );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst headElement = headElements.get( href );\n\t\t\tconst styleElement = doc.createElement( 'style' );\n\t\t\tstyleElement.textContent = headElement.text;\n\n\t\t\theadTags.push( styleElement );\n\t\t} )\n\t);\n\n\treturn [\n\t\tdoc.querySelector( 'title' ),\n\t\t...doc.querySelectorAll( 'style' ),\n\t\t...headTags,\n\t];\n};\n"],"mappings":";;;;;;;;AAAA;AACA;AACA;AACO,MAAMA,YAAY,GAAAC,OAAA,CAAAD,YAAA,GAAG,IAAIE,GAAG,CAGjC,CAAC;;AAEH;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,UAAU,GAAG,MAAQC,OAA0B,IAAM;EACjE;EACA,MAAMC,QAAQ,GAAKC,GAAY,IAAMA,GAAG,CAACC,EAAE,IAAID,GAAG,CAACE,SAAS;;EAE5D;EACA,MAAMC,UAAU,GAAG,IAAIP,GAAG,CAAoB,CAAC;EAC/C,KAAM,MAAMQ,KAAK,IAAIN,OAAO,EAAG;IAC9BK,UAAU,CAACE,GAAG,CAAEN,QAAQ,CAAEK,KAAM,CAAC,EAAEA,KAAM,CAAC;EAC3C;EAEA,MAAME,QAAmB,GAAG,EAAE;;EAE9B;EACA,KAAM,MAAMF,KAAK,IAAIG,QAAQ,CAACC,IAAI,CAACC,QAAQ,EAAG;IAC7C,MAAMR,EAAE,GAAGF,QAAQ,CAAEK,KAAM,CAAC;IAC5B;IACA,IAAKA,KAAK,CAACM,QAAQ,KAAK,MAAM,IAAIN,KAAK,CAACM,QAAQ,KAAK,OAAO,EAAG;MAC9DJ,QAAQ,CAACK,IAAI,CAAEP,KAAM,CAAC;IACvB,CAAC,MAAM,IAAKD,UAAU,CAACS,GAAG,CAAEX,EAAG,CAAC,EAAG;MAClCE,UAAU,CAACU,MAAM,CAAEZ,EAAG,CAAC;IACxB,CAAC,MAAM,IAAKG,KAAK,CAACM,QAAQ,KAAK,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AC;EAED,OAAO,CACNtB,GAAG,CAACwB,aAAa,CAAE,OAAQ,CAAC,EAC5B,GAAGxB,GAAG,CAACG,gBAAgB,CAAE,OAAQ,CAAC,EAClC,GAAGF,QAAQ,CACX;AACF,CAAC;AAACvC,OAAA,CAAAqC,eAAA,GAAAA,eAAA","ignoreList":[]}
|
package/build/index.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.state = exports.actions = void 0;
|
|
7
7
|
var _interactivity = require("@wordpress/interactivity");
|
|
8
|
-
var
|
|
8
|
+
var _head = require("./head");
|
|
9
9
|
var _getConfig$navigation;
|
|
10
10
|
/**
|
|
11
11
|
* WordPress dependencies
|
|
@@ -51,9 +51,7 @@ const fetchPage = async (url, {
|
|
|
51
51
|
html = await res.text();
|
|
52
52
|
}
|
|
53
53
|
const dom = new window.DOMParser().parseFromString(html, 'text/html');
|
|
54
|
-
return regionsToVdom(dom
|
|
55
|
-
baseUrl: url
|
|
56
|
-
});
|
|
54
|
+
return regionsToVdom(dom);
|
|
57
55
|
} catch (e) {
|
|
58
56
|
return false;
|
|
59
57
|
}
|
|
@@ -61,17 +59,16 @@ const fetchPage = async (url, {
|
|
|
61
59
|
|
|
62
60
|
// Return an object with VDOM trees of those HTML regions marked with a
|
|
63
61
|
// `router-region` directive.
|
|
64
|
-
const regionsToVdom = (dom, {
|
|
65
|
-
vdom
|
|
66
|
-
baseUrl
|
|
62
|
+
const regionsToVdom = async (dom, {
|
|
63
|
+
vdom
|
|
67
64
|
} = {}) => {
|
|
68
65
|
const regions = {
|
|
69
66
|
body: undefined
|
|
70
67
|
};
|
|
71
|
-
|
|
72
|
-
const scriptModules = [...dom.querySelectorAll('script[type=module][src]')].map(s => s.src);
|
|
68
|
+
let head;
|
|
73
69
|
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
74
70
|
if (navigationMode === 'fullPage') {
|
|
71
|
+
head = await (0, _head.fetchHeadAssets)(dom);
|
|
75
72
|
regions.body = vdom ? vdom.get(document.body) : toVdom(dom.body);
|
|
76
73
|
}
|
|
77
74
|
}
|
|
@@ -86,8 +83,7 @@ const regionsToVdom = (dom, {
|
|
|
86
83
|
const initialData = parseServerData(dom);
|
|
87
84
|
return {
|
|
88
85
|
regions,
|
|
89
|
-
|
|
90
|
-
scriptModules,
|
|
86
|
+
head,
|
|
91
87
|
title,
|
|
92
88
|
initialData
|
|
93
89
|
};
|
|
@@ -95,15 +91,10 @@ const regionsToVdom = (dom, {
|
|
|
95
91
|
|
|
96
92
|
// Render all interactive regions contained in the given page.
|
|
97
93
|
const renderRegions = async page => {
|
|
98
|
-
// Wait for styles and modules to be ready.
|
|
99
|
-
await Promise.all([...page.styles, ...page.scriptModules.map(src => (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(/* webpackIgnore: true */src))]);
|
|
100
|
-
// Replace style sheets.
|
|
101
|
-
const sheets = await Promise.all(page.styles);
|
|
102
|
-
window.document.querySelectorAll('style,link[rel=stylesheet]').forEach(element => element.remove());
|
|
103
|
-
window.document.adoptedStyleSheets = sheets;
|
|
104
94
|
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
105
95
|
if (navigationMode === 'fullPage') {
|
|
106
|
-
//
|
|
96
|
+
// Once this code is tested and more mature, the head should be updated for region based navigation as well.
|
|
97
|
+
await (0, _head.updateHead)(page.head);
|
|
107
98
|
const fragment = getRegionRootFragment(document.body);
|
|
108
99
|
batch(() => {
|
|
109
100
|
populateServerData(page.initialData);
|
|
@@ -159,9 +150,19 @@ window.addEventListener('popstate', async () => {
|
|
|
159
150
|
// Initialize the router and cache the initial page using the initial vDOM.
|
|
160
151
|
// Once this code is tested and more mature, the head should be updated for
|
|
161
152
|
// region based navigation as well.
|
|
153
|
+
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
154
|
+
if (navigationMode === 'fullPage') {
|
|
155
|
+
// Cache the scripts. Has to be called before fetching the assets.
|
|
156
|
+
[].map.call(document.querySelectorAll('script[type="module"][src]'), script => {
|
|
157
|
+
_head.headElements.set(script.getAttribute('src'), {
|
|
158
|
+
tag: script
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
await (0, _head.fetchHeadAssets)(document);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
162
164
|
pages.set(getPagePath(window.location.href), Promise.resolve(regionsToVdom(document, {
|
|
163
|
-
vdom: initialVdom
|
|
164
|
-
baseUrl: window.location.href
|
|
165
|
+
vdom: initialVdom
|
|
165
166
|
})));
|
|
166
167
|
|
|
167
168
|
// Check if the link is valid for client-side navigation.
|
|
@@ -192,7 +193,6 @@ const {
|
|
|
192
193
|
state: {
|
|
193
194
|
url: window.location.href,
|
|
194
195
|
navigation: {
|
|
195
|
-
isLoading: false,
|
|
196
196
|
hasStarted: false,
|
|
197
197
|
hasFinished: false
|
|
198
198
|
}
|
|
@@ -244,7 +244,6 @@ const {
|
|
|
244
244
|
if (navigatingTo !== href) {
|
|
245
245
|
return;
|
|
246
246
|
}
|
|
247
|
-
navigation.isLoading = true;
|
|
248
247
|
if (loadingAnimation) {
|
|
249
248
|
navigation.hasStarted = true;
|
|
250
249
|
navigation.hasFinished = false;
|
|
@@ -273,7 +272,6 @@ const {
|
|
|
273
272
|
|
|
274
273
|
// Update the navigation status once the the new page rendering
|
|
275
274
|
// has been completed.
|
|
276
|
-
navigation.isLoading = false;
|
|
277
275
|
if (loadingAnimation) {
|
|
278
276
|
navigation.hasStarted = false;
|
|
279
277
|
navigation.hasFinished = true;
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_interactivity","require","_styles","_getConfig$navigation","_getRequireWildcardCache","e","WeakMap","r","t","_interopRequireWildcard","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseServerData","populateServerData","batch","privateApis","navigationMode","getConfig","pages","Map","getPagePath","url","URL","window","location","href","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","baseUrl","vdom","regions","body","undefined","styles","generateCSSStyleSheets","scriptModules","querySelectorAll","map","s","src","globalThis","IS_GUTENBERG_PLUGIN","document","attrName","forEach","region","id","getAttribute","title","querySelector","innerText","initialData","renderRegions","page","Promise","all","specifier","then","sheets","element","remove","adoptedStyleSheets","fragment","forcePageReload","assign","addEventListener","pagePath","state","reload","resolve","isValidLink","ref","HTMLAnchorElement","target","origin","startsWith","searchParams","isValidEvent","event","button","metaKey","ctrlKey","altKey","shiftKey","defaultPrevented","navigatingTo","hasLoadedNavigationTextsData","navigationTexts","loading","loaded","actions","store","navigation","isLoading","hasStarted","hasFinished","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","a11ySpeak","race","clearTimeout","config","history","replace","hash","scrollIntoView","force","exports","messageKey","content","getElementById","textContent","parsed","JSON","parse","i18n","texts","message","speak","closest","preventDefault","nodeName"],"sources":["@wordpress/interactivity-router/src/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store, privateApis, getConfig } from '@wordpress/interactivity';\n\n/**\n * Internal dependencies\n */\nimport { generateCSSStyleSheets } from './assets/styles';\n\nconst {\n\tdirectivePrefix,\n\tgetRegionRootFragment,\n\tinitialVdom,\n\ttoVdom,\n\trender,\n\tparseServerData,\n\tpopulateServerData,\n\tbatch,\n} = privateApis(\n\t'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'\n);\n\ninterface NavigateOptions {\n\tforce?: boolean;\n\thtml?: string;\n\treplace?: boolean;\n\ttimeout?: number;\n\tloadingAnimation?: boolean;\n\tscreenReaderAnnouncement?: boolean;\n}\n\ninterface PrefetchOptions {\n\tforce?: boolean;\n\thtml?: string;\n}\n\ninterface VdomParams {\n\tvdom?: typeof initialVdom;\n\tbaseUrl?: string;\n}\n\ninterface Page {\n\tregions: Record< string, any >;\n\tstyles: Promise< CSSStyleSheet >[];\n\tscriptModules: string[];\n\ttitle: string;\n\tinitialData: any;\n}\n\ntype RegionsToVdom = ( dom: Document, params?: VdomParams ) => Page;\n\n// Check if the navigation mode is full page or region based.\nconst navigationMode: 'regionBased' | 'fullPage' =\n\tgetConfig( 'core/router' ).navigationMode ?? 'regionBased';\n\n// The cache of visited and prefetched pages, stylesheets and scripts.\nconst pages = new Map< string, Promise< Page | false > >();\n\n// Helper to remove domain and hash from the URL. We are only interesting in\n// caching the path and the query.\nconst getPagePath = ( url: string ) => {\n\tconst u = new URL( url, window.location.href );\n\treturn u.pathname + u.search;\n};\n\n// Fetch a new page and convert it to a static virtual DOM.\nconst fetchPage = async ( url: string, { html }: { html: string } ) => {\n\ttry {\n\t\tif ( ! html ) {\n\t\t\tconst res = await window.fetch( url );\n\t\t\tif ( res.status !== 200 ) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\thtml = await res.text();\n\t\t}\n\t\tconst dom = new window.DOMParser().parseFromString( html, 'text/html' );\n\t\treturn regionsToVdom( dom, { baseUrl: url } );\n\t} catch ( e ) {\n\t\treturn false;\n\t}\n};\n\n// Return an object with VDOM trees of those HTML regions marked with a\n// `router-region` directive.\nconst regionsToVdom: RegionsToVdom = ( dom, { vdom, baseUrl } = {} ) => {\n\tconst regions = { body: undefined };\n\tconst styles = generateCSSStyleSheets( dom, baseUrl );\n\tconst scriptModules = [\n\t\t...dom.querySelectorAll< HTMLScriptElement >(\n\t\t\t'script[type=module][src]'\n\t\t),\n\t].map( ( s ) => s.src );\n\n\tif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\t\tif ( navigationMode === 'fullPage' ) {\n\t\t\tregions.body = vdom\n\t\t\t\t? vdom.get( document.body )\n\t\t\t\t: toVdom( dom.body );\n\t\t}\n\t}\n\tif ( navigationMode === 'regionBased' ) {\n\t\tconst attrName = `data-${ directivePrefix }-router-region`;\n\t\tdom.querySelectorAll( `[${ attrName }]` ).forEach( ( region ) => {\n\t\t\tconst id = region.getAttribute( attrName );\n\t\t\tregions[ id ] = vdom?.has( region )\n\t\t\t\t? vdom.get( region )\n\t\t\t\t: toVdom( region );\n\t\t} );\n\t}\n\tconst title = dom.querySelector( 'title' )?.innerText;\n\tconst initialData = parseServerData( dom );\n\treturn { regions, styles, scriptModules, title, initialData };\n};\n\n// Render all interactive regions contained in the given page.\nconst renderRegions = async ( page: Page ) => {\n\t// Wait for styles and modules to be ready.\n\tawait Promise.all( [\n\t\t...page.styles,\n\t\t...page.scriptModules.map(\n\t\t\t( src ) => import( /* webpackIgnore: true */ src )\n\t\t),\n\t] );\n\t// Replace style sheets.\n\tconst sheets = await Promise.all( page.styles );\n\twindow.document\n\t\t.querySelectorAll( 'style,link[rel=stylesheet]' )\n\t\t.forEach( ( element ) => element.remove() );\n\twindow.document.adoptedStyleSheets = sheets;\n\n\tif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\t\tif ( navigationMode === 'fullPage' ) {\n\t\t\t// Update HTML.\n\t\t\tconst fragment = getRegionRootFragment( document.body );\n\t\t\tbatch( () => {\n\t\t\t\tpopulateServerData( page.initialData );\n\t\t\t\trender( page.regions.body, fragment );\n\t\t\t} );\n\t\t}\n\t}\n\tif ( navigationMode === 'regionBased' ) {\n\t\tconst attrName = `data-${ directivePrefix }-router-region`;\n\t\tbatch( () => {\n\t\t\tpopulateServerData( page.initialData );\n\t\t\tdocument\n\t\t\t\t.querySelectorAll( `[${ attrName }]` )\n\t\t\t\t.forEach( ( region ) => {\n\t\t\t\t\tconst id = region.getAttribute( attrName );\n\t\t\t\t\tconst fragment = getRegionRootFragment( region );\n\t\t\t\t\trender( page.regions[ id ], fragment );\n\t\t\t\t} );\n\t\t} );\n\t}\n\tif ( page.title ) {\n\t\tdocument.title = page.title;\n\t}\n};\n\n/**\n * Load the given page forcing a full page reload.\n *\n * The function returns a promise that won't resolve, useful to prevent any\n * potential feedback indicating that the navigation has finished while the new\n * page is being loaded.\n *\n * @param href The page href.\n * @return Promise that never resolves.\n */\nconst forcePageReload = ( href: string ) => {\n\twindow.location.assign( href );\n\treturn new Promise( () => {} );\n};\n\n// Listen to the back and forward buttons and restore the page if it's in the\n// cache.\nwindow.addEventListener( 'popstate', async () => {\n\tconst pagePath = getPagePath( window.location.href ); // Remove hash.\n\tconst page = pages.has( pagePath ) && ( await pages.get( pagePath ) );\n\tif ( page ) {\n\t\tawait renderRegions( page );\n\t\t// Update the URL in the state.\n\t\tstate.url = window.location.href;\n\t} else {\n\t\twindow.location.reload();\n\t}\n} );\n\n// Initialize the router and cache the initial page using the initial vDOM.\n// Once this code is tested and more mature, the head should be updated for\n// region based navigation as well.\npages.set(\n\tgetPagePath( window.location.href ),\n\tPromise.resolve(\n\t\tregionsToVdom( document, {\n\t\t\tvdom: initialVdom,\n\t\t\tbaseUrl: window.location.href,\n\t\t} )\n\t)\n);\n\n// Check if the link is valid for client-side navigation.\nconst isValidLink = ( ref: HTMLAnchorElement ) =>\n\tref &&\n\tref instanceof window.HTMLAnchorElement &&\n\tref.href &&\n\t( ! ref.target || ref.target === '_self' ) &&\n\tref.origin === window.location.origin &&\n\t! ref.pathname.startsWith( '/wp-admin' ) &&\n\t! ref.pathname.startsWith( '/wp-login.php' ) &&\n\t! ref.getAttribute( 'href' ).startsWith( '#' ) &&\n\t! new URL( ref.href ).searchParams.has( '_wpnonce' );\n\n// Check if the event is valid for client-side navigation.\nconst isValidEvent = ( event: MouseEvent ) =>\n\tevent &&\n\tevent.button === 0 && // Left clicks only.\n\t! event.metaKey && // Open in new tab (Mac).\n\t! event.ctrlKey && // Open in new tab (Windows).\n\t! event.altKey && // Download.\n\t! event.shiftKey &&\n\t! event.defaultPrevented;\n\n// Variable to store the current navigation.\nlet navigatingTo = '';\n\nlet hasLoadedNavigationTextsData = false;\nconst navigationTexts = {\n\tloading: 'Loading page, please wait.',\n\tloaded: 'Page Loaded.',\n};\n\ninterface Store {\n\tstate: {\n\t\turl: string;\n\t\tnavigation: {\n\t\t\tisLoading: boolean;\n\t\t\thasStarted: boolean;\n\t\t\thasFinished: boolean;\n\t\t};\n\t};\n\tactions: {\n\t\tnavigate: ( href: string, options?: NavigateOptions ) => void;\n\t\tprefetch: ( url: string, options?: PrefetchOptions ) => void;\n\t};\n}\n\nexport const { state, actions } = store< Store >( 'core/router', {\n\tstate: {\n\t\turl: window.location.href,\n\t\tnavigation: {\n\t\t\tisLoading: false,\n\t\t\thasStarted: false,\n\t\t\thasFinished: false,\n\t\t},\n\t},\n\tactions: {\n\t\t/**\n\t\t * Navigates to the specified page.\n\t\t *\n\t\t * This function normalizes the passed href, fetches the page HTML if\n\t\t * needed, and updates any interactive regions whose contents have\n\t\t * changed. It also creates a new entry in the browser session history.\n\t\t *\n\t\t * @param href The page href.\n\t\t * @param [options] Options object.\n\t\t * @param [options.force] If true, it forces re-fetching the URL.\n\t\t * @param [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t * @param [options.replace] If true, it replaces the current entry in the browser session history.\n\t\t * @param [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000.\n\t\t * @param [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`.\n\t\t * @param [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`.\n\t\t *\n\t\t * @return Promise that resolves once the navigation is completed or aborted.\n\t\t */\n\t\t*navigate( href: string, options: NavigateOptions = {} ) {\n\t\t\tconst { clientNavigationDisabled } = getConfig();\n\t\t\tif ( clientNavigationDisabled ) {\n\t\t\t\tyield forcePageReload( href );\n\t\t\t}\n\n\t\t\tconst pagePath = getPagePath( href );\n\t\t\tconst { navigation } = state;\n\t\t\tconst {\n\t\t\t\tloadingAnimation = true,\n\t\t\t\tscreenReaderAnnouncement = true,\n\t\t\t\ttimeout = 10000,\n\t\t\t} = options;\n\n\t\t\tnavigatingTo = href;\n\t\t\tactions.prefetch( pagePath, options );\n\n\t\t\t// Create a promise that resolves when the specified timeout ends.\n\t\t\t// The timeout value is 10 seconds by default.\n\t\t\tconst timeoutPromise = new Promise< void >( ( resolve ) =>\n\t\t\t\tsetTimeout( resolve, timeout )\n\t\t\t);\n\n\t\t\t// Don't update the navigation status immediately, wait 400 ms.\n\t\t\tconst loadingTimeout = setTimeout( () => {\n\t\t\t\tif ( navigatingTo !== href ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tnavigation.isLoading = true;\n\t\t\t\tif ( loadingAnimation ) {\n\t\t\t\t\tnavigation.hasStarted = true;\n\t\t\t\t\tnavigation.hasFinished = false;\n\t\t\t\t}\n\t\t\t\tif ( screenReaderAnnouncement ) {\n\t\t\t\t\ta11ySpeak( 'loading' );\n\t\t\t\t}\n\t\t\t}, 400 );\n\n\t\t\tconst page = yield Promise.race( [\n\t\t\t\tpages.get( pagePath ),\n\t\t\t\ttimeoutPromise,\n\t\t\t] );\n\n\t\t\t// Dismiss loading message if it hasn't been added yet.\n\t\t\tclearTimeout( loadingTimeout );\n\n\t\t\t// Once the page is fetched, the destination URL could have changed\n\t\t\t// (e.g., by clicking another link in the meantime). If so, bail\n\t\t\t// out, and let the newer execution to update the HTML.\n\t\t\tif ( navigatingTo !== href ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tpage &&\n\t\t\t\t! page.initialData?.config?.[ 'core/router' ]\n\t\t\t\t\t?.clientNavigationDisabled\n\t\t\t) {\n\t\t\t\tyield renderRegions( page );\n\t\t\t\twindow.history[\n\t\t\t\t\toptions.replace ? 'replaceState' : 'pushState'\n\t\t\t\t]( {}, '', href );\n\n\t\t\t\t// Update the URL in the state.\n\t\t\t\tstate.url = href;\n\n\t\t\t\t// Update the navigation status once the the new page rendering\n\t\t\t\t// has been completed.\n\t\t\t\tnavigation.isLoading = false;\n\t\t\t\tif ( loadingAnimation ) {\n\t\t\t\t\tnavigation.hasStarted = false;\n\t\t\t\t\tnavigation.hasFinished = true;\n\t\t\t\t}\n\n\t\t\t\tif ( screenReaderAnnouncement ) {\n\t\t\t\t\ta11ySpeak( 'loaded' );\n\t\t\t\t}\n\n\t\t\t\t// Scroll to the anchor if exits in the link.\n\t\t\t\tconst { hash } = new URL( href, window.location.href );\n\t\t\t\tif ( hash ) {\n\t\t\t\t\tdocument.querySelector( hash )?.scrollIntoView();\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tyield forcePageReload( href );\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Prefetches the page with the passed URL.\n\t\t *\n\t\t * The function normalizes the URL and stores internally the fetch\n\t\t * promise, to avoid triggering a second fetch for an ongoing request.\n\t\t *\n\t\t * @param url The page URL.\n\t\t * @param [options] Options object.\n\t\t * @param [options.force] Force fetching the URL again.\n\t\t * @param [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t */\n\t\tprefetch( url: string, options: PrefetchOptions = {} ) {\n\t\t\tconst { clientNavigationDisabled } = getConfig();\n\t\t\tif ( clientNavigationDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst pagePath = getPagePath( url );\n\t\t\tif ( options.force || ! pages.has( pagePath ) ) {\n\t\t\t\tpages.set(\n\t\t\t\t\tpagePath,\n\t\t\t\t\tfetchPage( pagePath, { html: options.html } )\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t},\n} );\n\n/**\n * Announces a message to screen readers.\n *\n * This is a wrapper around the `@wordpress/a11y` package's `speak` function. It handles importing\n * the package on demand and should be used instead of calling `ally.speak` direacly.\n *\n * @param messageKey The message to be announced by assistive technologies.\n */\nfunction a11ySpeak( messageKey: keyof typeof navigationTexts ) {\n\tif ( ! hasLoadedNavigationTextsData ) {\n\t\thasLoadedNavigationTextsData = true;\n\t\tconst content = document.getElementById(\n\t\t\t'wp-script-module-data-@wordpress/interactivity-router'\n\t\t)?.textContent;\n\t\tif ( content ) {\n\t\t\ttry {\n\t\t\t\tconst parsed = JSON.parse( content );\n\t\t\t\tif ( typeof parsed?.i18n?.loading === 'string' ) {\n\t\t\t\t\tnavigationTexts.loading = parsed.i18n.loading;\n\t\t\t\t}\n\t\t\t\tif ( typeof parsed?.i18n?.loaded === 'string' ) {\n\t\t\t\t\tnavigationTexts.loaded = parsed.i18n.loaded;\n\t\t\t\t}\n\t\t\t} catch {}\n\t\t} else {\n\t\t\t// Fallback to localized strings from Interactivity API state.\n\t\t\t// @todo This block is for Core < 6.7.0. Remove when support is dropped.\n\n\t\t\t// @ts-expect-error\n\t\t\tif ( state.navigation.texts?.loading ) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tnavigationTexts.loading = state.navigation.texts.loading;\n\t\t\t}\n\t\t\t// @ts-expect-error\n\t\t\tif ( state.navigation.texts?.loaded ) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tnavigationTexts.loaded = state.navigation.texts.loaded;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst message = navigationTexts[ messageKey ];\n\n\timport( '@wordpress/a11y' ).then(\n\t\t( { speak } ) => speak( message ),\n\t\t// Ignore failures to load the a11y module.\n\t\t() => {}\n\t);\n}\n\n// Add click and prefetch to all links.\nif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\tif ( navigationMode === 'fullPage' ) {\n\t\t// Navigate on click.\n\t\tdocument.addEventListener(\n\t\t\t'click',\n\t\t\tfunction ( event ) {\n\t\t\t\tconst ref = ( event.target as Element ).closest( 'a' );\n\t\t\t\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tactions.navigate( ref.href );\n\t\t\t\t}\n\t\t\t},\n\t\t\ttrue\n\t\t);\n\t\t// Prefetch on hover.\n\t\tdocument.addEventListener(\n\t\t\t'mouseenter',\n\t\t\tfunction ( event ) {\n\t\t\t\tif ( ( event.target as Element )?.nodeName === 'A' ) {\n\t\t\t\t\tconst ref = ( event.target as Element ).closest( 'a' );\n\t\t\t\t\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\t\t\t\t\tactions.prefetch( ref.href );\n\t\t\t\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AAM,CAAcsD,OAAO,CAAE,GAAI,CAAC;QACtD,IAAKzD,WAAW,CAAEC,GAAI,CAAC,IAAIM,YAAY,CAAEC,KAAM,CAAC,EAAG;UAClDY,OAAO,CAACY,QAAQ,CAAE/B,GAAG,CAACxD,IAAK,CAAC;QAC7B;MACD;IACD,CAAC,EACD,IACD,CAAC;EACF;AACD","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_interactivity","require","_head","_getConfig$navigation","_getRequireWildcardCache","e","WeakMap","r","t","_interopRequireWildcard","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","hasOwnProperty","call","i","set","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseServerData","populateServerData","batch","privateApis","navigationMode","getConfig","pages","Map","getPagePath","url","URL","window","location","href","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","vdom","regions","body","undefined","head","globalThis","IS_GUTENBERG_PLUGIN","fetchHeadAssets","document","attrName","querySelectorAll","forEach","region","id","getAttribute","title","querySelector","innerText","initialData","renderRegions","page","updateHead","fragment","forcePageReload","assign","Promise","addEventListener","pagePath","state","reload","map","script","headElements","tag","resolve","isValidLink","ref","HTMLAnchorElement","target","origin","startsWith","searchParams","isValidEvent","event","button","metaKey","ctrlKey","altKey","shiftKey","defaultPrevented","navigatingTo","hasLoadedNavigationTextsData","navigationTexts","loading","loaded","actions","store","navigation","hasStarted","hasFinished","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","a11ySpeak","race","clearTimeout","config","history","replace","hash","scrollIntoView","force","exports","messageKey","content","getElementById","textContent","parsed","JSON","parse","i18n","texts","message","then","speak","closest","preventDefault","nodeName"],"sources":["@wordpress/interactivity-router/src/index.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store, privateApis, getConfig } from '@wordpress/interactivity';\n\n/**\n * Internal dependencies\n */\nimport { fetchHeadAssets, updateHead, headElements } from './head';\n\nconst {\n\tdirectivePrefix,\n\tgetRegionRootFragment,\n\tinitialVdom,\n\ttoVdom,\n\trender,\n\tparseServerData,\n\tpopulateServerData,\n\tbatch,\n} = privateApis(\n\t'I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.'\n);\n\ninterface NavigateOptions {\n\tforce?: boolean;\n\thtml?: string;\n\treplace?: boolean;\n\ttimeout?: number;\n\tloadingAnimation?: boolean;\n\tscreenReaderAnnouncement?: boolean;\n}\n\ninterface PrefetchOptions {\n\tforce?: boolean;\n\thtml?: string;\n}\n\ninterface VdomParams {\n\tvdom?: typeof initialVdom;\n}\n\ninterface Page {\n\tregions: Record< string, any >;\n\thead: HTMLHeadElement[];\n\ttitle: string;\n\tinitialData: any;\n}\n\ntype RegionsToVdom = ( dom: Document, params?: VdomParams ) => Promise< Page >;\n\n// Check if the navigation mode is full page or region based.\nconst navigationMode: 'regionBased' | 'fullPage' =\n\tgetConfig( 'core/router' ).navigationMode ?? 'regionBased';\n\n// The cache of visited and prefetched pages, stylesheets and scripts.\nconst pages = new Map< string, Promise< Page | false > >();\n\n// Helper to remove domain and hash from the URL. We are only interesting in\n// caching the path and the query.\nconst getPagePath = ( url: string ) => {\n\tconst u = new URL( url, window.location.href );\n\treturn u.pathname + u.search;\n};\n\n// Fetch a new page and convert it to a static virtual DOM.\nconst fetchPage = async ( url: string, { html }: { html: string } ) => {\n\ttry {\n\t\tif ( ! html ) {\n\t\t\tconst res = await window.fetch( url );\n\t\t\tif ( res.status !== 200 ) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\thtml = await res.text();\n\t\t}\n\t\tconst dom = new window.DOMParser().parseFromString( html, 'text/html' );\n\t\treturn regionsToVdom( dom );\n\t} catch ( e ) {\n\t\treturn false;\n\t}\n};\n\n// Return an object with VDOM trees of those HTML regions marked with a\n// `router-region` directive.\nconst regionsToVdom: RegionsToVdom = async ( dom, { vdom } = {} ) => {\n\tconst regions = { body: undefined };\n\tlet head: HTMLElement[];\n\tif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\t\tif ( navigationMode === 'fullPage' ) {\n\t\t\thead = await fetchHeadAssets( dom );\n\t\t\tregions.body = vdom\n\t\t\t\t? vdom.get( document.body )\n\t\t\t\t: toVdom( dom.body );\n\t\t}\n\t}\n\tif ( navigationMode === 'regionBased' ) {\n\t\tconst attrName = `data-${ directivePrefix }-router-region`;\n\t\tdom.querySelectorAll( `[${ attrName }]` ).forEach( ( region ) => {\n\t\t\tconst id = region.getAttribute( attrName );\n\t\t\tregions[ id ] = vdom?.has( region )\n\t\t\t\t? vdom.get( region )\n\t\t\t\t: toVdom( region );\n\t\t} );\n\t}\n\tconst title = dom.querySelector( 'title' )?.innerText;\n\tconst initialData = parseServerData( dom );\n\treturn { regions, head, title, initialData };\n};\n\n// Render all interactive regions contained in the given page.\nconst renderRegions = async ( page: Page ) => {\n\tif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\t\tif ( navigationMode === 'fullPage' ) {\n\t\t\t// Once this code is tested and more mature, the head should be updated for region based navigation as well.\n\t\t\tawait updateHead( page.head );\n\t\t\tconst fragment = getRegionRootFragment( document.body );\n\t\t\tbatch( () => {\n\t\t\t\tpopulateServerData( page.initialData );\n\t\t\t\trender( page.regions.body, fragment );\n\t\t\t} );\n\t\t}\n\t}\n\tif ( navigationMode === 'regionBased' ) {\n\t\tconst attrName = `data-${ directivePrefix }-router-region`;\n\t\tbatch( () => {\n\t\t\tpopulateServerData( page.initialData );\n\t\t\tdocument\n\t\t\t\t.querySelectorAll( `[${ attrName }]` )\n\t\t\t\t.forEach( ( region ) => {\n\t\t\t\t\tconst id = region.getAttribute( attrName );\n\t\t\t\t\tconst fragment = getRegionRootFragment( region );\n\t\t\t\t\trender( page.regions[ id ], fragment );\n\t\t\t\t} );\n\t\t} );\n\t}\n\tif ( page.title ) {\n\t\tdocument.title = page.title;\n\t}\n};\n\n/**\n * Load the given page forcing a full page reload.\n *\n * The function returns a promise that won't resolve, useful to prevent any\n * potential feedback indicating that the navigation has finished while the new\n * page is being loaded.\n *\n * @param href The page href.\n * @return Promise that never resolves.\n */\nconst forcePageReload = ( href: string ) => {\n\twindow.location.assign( href );\n\treturn new Promise( () => {} );\n};\n\n// Listen to the back and forward buttons and restore the page if it's in the\n// cache.\nwindow.addEventListener( 'popstate', async () => {\n\tconst pagePath = getPagePath( window.location.href ); // Remove hash.\n\tconst page = pages.has( pagePath ) && ( await pages.get( pagePath ) );\n\tif ( page ) {\n\t\tawait renderRegions( page );\n\t\t// Update the URL in the state.\n\t\tstate.url = window.location.href;\n\t} else {\n\t\twindow.location.reload();\n\t}\n} );\n\n// Initialize the router and cache the initial page using the initial vDOM.\n// Once this code is tested and more mature, the head should be updated for\n// region based navigation as well.\nif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\tif ( navigationMode === 'fullPage' ) {\n\t\t// Cache the scripts. Has to be called before fetching the assets.\n\t\t[].map.call(\n\t\t\tdocument.querySelectorAll( 'script[type=\"module\"][src]' ),\n\t\t\t( script ) => {\n\t\t\t\theadElements.set( script.getAttribute( 'src' ), {\n\t\t\t\t\ttag: script,\n\t\t\t\t} );\n\t\t\t}\n\t\t);\n\t\tawait fetchHeadAssets( document );\n\t}\n}\npages.set(\n\tgetPagePath( window.location.href ),\n\tPromise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )\n);\n\n// Check if the link is valid for client-side navigation.\nconst isValidLink = ( ref: HTMLAnchorElement ) =>\n\tref &&\n\tref instanceof window.HTMLAnchorElement &&\n\tref.href &&\n\t( ! ref.target || ref.target === '_self' ) &&\n\tref.origin === window.location.origin &&\n\t! ref.pathname.startsWith( '/wp-admin' ) &&\n\t! ref.pathname.startsWith( '/wp-login.php' ) &&\n\t! ref.getAttribute( 'href' ).startsWith( '#' ) &&\n\t! new URL( ref.href ).searchParams.has( '_wpnonce' );\n\n// Check if the event is valid for client-side navigation.\nconst isValidEvent = ( event: MouseEvent ) =>\n\tevent &&\n\tevent.button === 0 && // Left clicks only.\n\t! event.metaKey && // Open in new tab (Mac).\n\t! event.ctrlKey && // Open in new tab (Windows).\n\t! event.altKey && // Download.\n\t! event.shiftKey &&\n\t! event.defaultPrevented;\n\n// Variable to store the current navigation.\nlet navigatingTo = '';\n\nlet hasLoadedNavigationTextsData = false;\nconst navigationTexts = {\n\tloading: 'Loading page, please wait.',\n\tloaded: 'Page Loaded.',\n};\n\ninterface Store {\n\tstate: {\n\t\turl: string;\n\t\tnavigation: {\n\t\t\thasStarted: boolean;\n\t\t\thasFinished: boolean;\n\t\t};\n\t};\n\tactions: {\n\t\tnavigate: ( href: string, options?: NavigateOptions ) => void;\n\t\tprefetch: ( url: string, options?: PrefetchOptions ) => void;\n\t};\n}\n\nexport const { state, actions } = store< Store >( 'core/router', {\n\tstate: {\n\t\turl: window.location.href,\n\t\tnavigation: {\n\t\t\thasStarted: false,\n\t\t\thasFinished: false,\n\t\t},\n\t},\n\tactions: {\n\t\t/**\n\t\t * Navigates to the specified page.\n\t\t *\n\t\t * This function normalizes the passed href, fetches the page HTML if\n\t\t * needed, and updates any interactive regions whose contents have\n\t\t * changed. It also creates a new entry in the browser session history.\n\t\t *\n\t\t * @param href The page href.\n\t\t * @param [options] Options object.\n\t\t * @param [options.force] If true, it forces re-fetching the URL.\n\t\t * @param [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t * @param [options.replace] If true, it replaces the current entry in the browser session history.\n\t\t * @param [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000.\n\t\t * @param [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`.\n\t\t * @param [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`.\n\t\t *\n\t\t * @return Promise that resolves once the navigation is completed or aborted.\n\t\t */\n\t\t*navigate( href: string, options: NavigateOptions = {} ) {\n\t\t\tconst { clientNavigationDisabled } = getConfig();\n\t\t\tif ( clientNavigationDisabled ) {\n\t\t\t\tyield forcePageReload( href );\n\t\t\t}\n\n\t\t\tconst pagePath = getPagePath( href );\n\t\t\tconst { navigation } = state;\n\t\t\tconst {\n\t\t\t\tloadingAnimation = true,\n\t\t\t\tscreenReaderAnnouncement = true,\n\t\t\t\ttimeout = 10000,\n\t\t\t} = options;\n\n\t\t\tnavigatingTo = href;\n\t\t\tactions.prefetch( pagePath, options );\n\n\t\t\t// Create a promise that resolves when the specified timeout ends.\n\t\t\t// The timeout value is 10 seconds by default.\n\t\t\tconst timeoutPromise = new Promise< void >( ( resolve ) =>\n\t\t\t\tsetTimeout( resolve, timeout )\n\t\t\t);\n\n\t\t\t// Don't update the navigation status immediately, wait 400 ms.\n\t\t\tconst loadingTimeout = setTimeout( () => {\n\t\t\t\tif ( navigatingTo !== href ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif ( loadingAnimation ) {\n\t\t\t\t\tnavigation.hasStarted = true;\n\t\t\t\t\tnavigation.hasFinished = false;\n\t\t\t\t}\n\t\t\t\tif ( screenReaderAnnouncement ) {\n\t\t\t\t\ta11ySpeak( 'loading' );\n\t\t\t\t}\n\t\t\t}, 400 );\n\n\t\t\tconst page = yield Promise.race( [\n\t\t\t\tpages.get( pagePath ),\n\t\t\t\ttimeoutPromise,\n\t\t\t] );\n\n\t\t\t// Dismiss loading message if it hasn't been added yet.\n\t\t\tclearTimeout( loadingTimeout );\n\n\t\t\t// Once the page is fetched, the destination URL could have changed\n\t\t\t// (e.g., by clicking another link in the meantime). If so, bail\n\t\t\t// out, and let the newer execution to update the HTML.\n\t\t\tif ( navigatingTo !== href ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\tpage &&\n\t\t\t\t! page.initialData?.config?.[ 'core/router' ]\n\t\t\t\t\t?.clientNavigationDisabled\n\t\t\t) {\n\t\t\t\tyield renderRegions( page );\n\t\t\t\twindow.history[\n\t\t\t\t\toptions.replace ? 'replaceState' : 'pushState'\n\t\t\t\t]( {}, '', href );\n\n\t\t\t\t// Update the URL in the state.\n\t\t\t\tstate.url = href;\n\n\t\t\t\t// Update the navigation status once the the new page rendering\n\t\t\t\t// has been completed.\n\t\t\t\tif ( loadingAnimation ) {\n\t\t\t\t\tnavigation.hasStarted = false;\n\t\t\t\t\tnavigation.hasFinished = true;\n\t\t\t\t}\n\n\t\t\t\tif ( screenReaderAnnouncement ) {\n\t\t\t\t\ta11ySpeak( 'loaded' );\n\t\t\t\t}\n\n\t\t\t\t// Scroll to the anchor if exits in the link.\n\t\t\t\tconst { hash } = new URL( href, window.location.href );\n\t\t\t\tif ( hash ) {\n\t\t\t\t\tdocument.querySelector( hash )?.scrollIntoView();\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tyield forcePageReload( href );\n\t\t\t}\n\t\t},\n\n\t\t/**\n\t\t * Prefetches the page with the passed URL.\n\t\t *\n\t\t * The function normalizes the URL and stores internally the fetch\n\t\t * promise, to avoid triggering a second fetch for an ongoing request.\n\t\t *\n\t\t * @param url The page URL.\n\t\t * @param [options] Options object.\n\t\t * @param [options.force] Force fetching the URL again.\n\t\t * @param [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t */\n\t\tprefetch( url: string, options: PrefetchOptions = {} ) {\n\t\t\tconst { clientNavigationDisabled } = getConfig();\n\t\t\tif ( clientNavigationDisabled ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst pagePath = getPagePath( url );\n\t\t\tif ( options.force || ! pages.has( pagePath ) ) {\n\t\t\t\tpages.set(\n\t\t\t\t\tpagePath,\n\t\t\t\t\tfetchPage( pagePath, { html: options.html } )\n\t\t\t\t);\n\t\t\t}\n\t\t},\n\t},\n} );\n\n/**\n * Announces a message to screen readers.\n *\n * This is a wrapper around the `@wordpress/a11y` package's `speak` function. It handles importing\n * the package on demand and should be used instead of calling `ally.speak` direacly.\n *\n * @param messageKey The message to be announced by assistive technologies.\n */\nfunction a11ySpeak( messageKey: keyof typeof navigationTexts ) {\n\tif ( ! hasLoadedNavigationTextsData ) {\n\t\thasLoadedNavigationTextsData = true;\n\t\tconst content = document.getElementById(\n\t\t\t'wp-script-module-data-@wordpress/interactivity-router'\n\t\t)?.textContent;\n\t\tif ( content ) {\n\t\t\ttry {\n\t\t\t\tconst parsed = JSON.parse( content );\n\t\t\t\tif ( typeof parsed?.i18n?.loading === 'string' ) {\n\t\t\t\t\tnavigationTexts.loading = parsed.i18n.loading;\n\t\t\t\t}\n\t\t\t\tif ( typeof parsed?.i18n?.loaded === 'string' ) {\n\t\t\t\t\tnavigationTexts.loaded = parsed.i18n.loaded;\n\t\t\t\t}\n\t\t\t} catch {}\n\t\t} else {\n\t\t\t// Fallback to localized strings from Interactivity API state.\n\t\t\t// @todo This block is for Core < 6.7.0. Remove when support is dropped.\n\n\t\t\t// @ts-expect-error\n\t\t\tif ( state.navigation.texts?.loading ) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tnavigationTexts.loading = state.navigation.texts.loading;\n\t\t\t}\n\t\t\t// @ts-expect-error\n\t\t\tif ( state.navigation.texts?.loaded ) {\n\t\t\t\t// @ts-expect-error\n\t\t\t\tnavigationTexts.loaded = state.navigation.texts.loaded;\n\t\t\t}\n\t\t}\n\t}\n\n\tconst message = navigationTexts[ messageKey ];\n\n\timport( '@wordpress/a11y' ).then(\n\t\t( { speak } ) => speak( message ),\n\t\t// Ignore failures to load the a11y module.\n\t\t() => {}\n\t);\n}\n\n// Add click and prefetch to all links.\nif ( globalThis.IS_GUTENBERG_PLUGIN ) {\n\tif ( navigationMode === 'fullPage' ) {\n\t\t// Navigate on click.\n\t\tdocument.addEventListener(\n\t\t\t'click',\n\t\t\tfunction ( event ) {\n\t\t\t\tconst ref = ( event.target as Element ).closest( 'a' );\n\t\t\t\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tactions.navigate( ref.href );\n\t\t\t\t}\n\t\t\t},\n\t\t\ttrue\n\t\t);\n\t\t// Prefetch on hover.\n\t\tdocument.addEventListener(\n\t\t\t'mouseenter',\n\t\t\tfunction ( event ) {\n\t\t\t\tif ( ( event.target as Element )?.nodeName === 'A' ) {\n\t\t\t\t\tconst ref = ( event.target as Element ).closest( 'a' );\n\t\t\t\t\tif ( isValidLink( ref ) && isValidEvent( event ) ) {\n\t\t\t\t\t\tactions.prefetch( ref.href );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\ttrue\n\t\t);\n\t}\n}\n"],"mappings":";;;;;;AAGA,IAAAA,cAAA,GAAAC,OAAA;AAKA,IAAAC,KAAA,GAAAD,OAAA;AAAmE,IAAAE,qBAAA;AARnE;AACA;AACA;AAGA;AACA;AACA;AAFA,SAAAC,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAI,wBAAAJ,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAM,OAAA,EAAAN,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAI,GAAA,CAAAP,CAAA,UAAAG,CAAA,CAAAK,GAAA,CAAAR,CAAA,OAAAS,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAf,CAAA,oBAAAe,CAAA,OAAAC,cAAA,CAAAC,IAAA,CAAAjB,CAAA,EAAAe,CAAA,SAAAG,CAAA,GAAAP,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAd,CAAA,EAAAe,CAAA,UAAAG,CAAA,KAAAA,CAAA,CAAAV,GAAA,IAAAU,CAAA,CAAAC,GAAA,IAAA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|
|
3
|
+
*/
|
|
4
|
+
export const headElements = new Map();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Helper to update only the necessary tags in the head.
|
|
8
|
+
*
|
|
9
|
+
* @async
|
|
10
|
+
* @param newHead The head elements of the new page.
|
|
11
|
+
*/
|
|
12
|
+
export const updateHead = async newHead => {
|
|
13
|
+
// Helper to get the tag id store in the cache.
|
|
14
|
+
const getTagId = tag => tag.id || tag.outerHTML;
|
|
15
|
+
|
|
16
|
+
// Map incoming head tags by their content.
|
|
17
|
+
const newHeadMap = new Map();
|
|
18
|
+
for (const child of newHead) {
|
|
19
|
+
newHeadMap.set(getTagId(child), child);
|
|
20
|
+
}
|
|
21
|
+
const toRemove = [];
|
|
22
|
+
|
|
23
|
+
// Detect nodes that should be added or removed.
|
|
24
|
+
for (const child of document.head.children) {
|
|
25
|
+
const id = getTagId(child);
|
|
26
|
+
// Always remove styles and links as they might change.
|
|
27
|
+
if (child.nodeName === 'LINK' || child.nodeName === 'STYLE') {
|
|
28
|
+
toRemove.push(child);
|
|
29
|
+
} else if (newHeadMap.has(id)) {
|
|
30
|
+
newHeadMap.delete(id);
|
|
31
|
+
} else if (child.nodeName !== 'SCRIPT' && child.nodeName !== 'META') {
|
|
32
|
+
toRemove.push(child);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
await Promise.all([...headElements.entries()].filter(([, {
|
|
36
|
+
tag
|
|
37
|
+
}]) => tag.nodeName === 'SCRIPT').map(async ([url]) => {
|
|
38
|
+
await import(/* webpackIgnore: true */url);
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
// Prepare new assets.
|
|
42
|
+
const toAppend = [...newHeadMap.values()];
|
|
43
|
+
|
|
44
|
+
// Apply the changes.
|
|
45
|
+
toRemove.forEach(n => n.remove());
|
|
46
|
+
document.head.append(...toAppend);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Fetches and processes head assets (stylesheets and scripts) from a specified document.
|
|
51
|
+
*
|
|
52
|
+
* @async
|
|
53
|
+
* @param doc The document from which to fetch head assets. It should support standard DOM querying methods.
|
|
54
|
+
*
|
|
55
|
+
* @return Returns an array of HTML elements representing the head assets.
|
|
56
|
+
*/
|
|
57
|
+
export const fetchHeadAssets = async doc => {
|
|
58
|
+
const headTags = [];
|
|
59
|
+
|
|
60
|
+
// We only want to fetch module scripts because regular scripts (without
|
|
61
|
+
// `async` or `defer` attributes) can depend on the execution of other scripts.
|
|
62
|
+
// Scripts found in the head are blocking and must be executed in order.
|
|
63
|
+
const scripts = doc.querySelectorAll('script[type="module"][src]');
|
|
64
|
+
scripts.forEach(script => {
|
|
65
|
+
const src = script.getAttribute('src');
|
|
66
|
+
if (!headElements.has(src)) {
|
|
67
|
+
// add the <link> elements to prefetch the module scripts
|
|
68
|
+
const link = doc.createElement('link');
|
|
69
|
+
link.rel = 'modulepreload';
|
|
70
|
+
link.href = src;
|
|
71
|
+
document.head.append(link);
|
|
72
|
+
headElements.set(src, {
|
|
73
|
+
tag: script
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const stylesheets = doc.querySelectorAll('link[rel=stylesheet]');
|
|
78
|
+
await Promise.all(Array.from(stylesheets).map(async tag => {
|
|
79
|
+
const href = tag.getAttribute('href');
|
|
80
|
+
if (!href) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (!headElements.has(href)) {
|
|
84
|
+
try {
|
|
85
|
+
const response = await fetch(href);
|
|
86
|
+
const text = await response.text();
|
|
87
|
+
headElements.set(href, {
|
|
88
|
+
tag,
|
|
89
|
+
text
|
|
90
|
+
});
|
|
91
|
+
} catch (e) {
|
|
92
|
+
// eslint-disable-next-line no-console
|
|
93
|
+
console.error(e);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const headElement = headElements.get(href);
|
|
97
|
+
const styleElement = doc.createElement('style');
|
|
98
|
+
styleElement.textContent = headElement.text;
|
|
99
|
+
headTags.push(styleElement);
|
|
100
|
+
}));
|
|
101
|
+
return [doc.querySelector('title'), ...doc.querySelectorAll('style'), ...headTags];
|
|
102
|
+
};
|
|
103
|
+
//# sourceMappingURL=head.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["headElements","Map","updateHead","newHead","getTagId","tag","id","outerHTML","newHeadMap","child","set","toRemove","document","head","children","nodeName","push","has","delete","Promise","all","entries","filter","map","url","toAppend","values","forEach","n","remove","append","fetchHeadAssets","doc","headTags","scripts","querySelectorAll","script","src","getAttribute","link","createElement","rel","href","stylesheets","Array","from","response","fetch","text","e","console","error","headElement","get","styleElement","textContent","querySelector"],"sources":["@wordpress/interactivity-router/src/head.ts"],"sourcesContent":["/**\n * The cache of prefetched stylesheets and scripts.\n */\nexport const headElements = new Map<\n\tstring,\n\t{ tag: HTMLElement; text?: string }\n>();\n\n/**\n * Helper to update only the necessary tags in the head.\n *\n * @async\n * @param newHead The head elements of the new page.\n */\nexport const updateHead = async ( newHead: HTMLHeadElement[] ) => {\n\t// Helper to get the tag id store in the cache.\n\tconst getTagId = ( tag: Element ) => tag.id || tag.outerHTML;\n\n\t// Map incoming head tags by their content.\n\tconst newHeadMap = new Map< string, Element >();\n\tfor ( const child of newHead ) {\n\t\tnewHeadMap.set( getTagId( child ), child );\n\t}\n\n\tconst toRemove: Element[] = [];\n\n\t// Detect nodes that should be added or removed.\n\tfor ( const child of document.head.children ) {\n\t\tconst id = getTagId( child );\n\t\t// Always remove styles and links as they might change.\n\t\tif ( child.nodeName === 'LINK' || child.nodeName === 'STYLE' ) {\n\t\t\ttoRemove.push( child );\n\t\t} else if ( newHeadMap.has( id ) ) {\n\t\t\tnewHeadMap.delete( id );\n\t\t} else if ( child.nodeName !== 'SCRIPT' && child.nodeName !== 'META' ) {\n\t\t\ttoRemove.push( child );\n\t\t}\n\t}\n\n\tawait Promise.all(\n\t\t[ ...headElements.entries() ]\n\t\t\t.filter( ( [ , { tag } ] ) => tag.nodeName === 'SCRIPT' )\n\t\t\t.map( async ( [ url ] ) => {\n\t\t\t\tawait import( /* webpackIgnore: true */ url );\n\t\t\t} )\n\t);\n\n\t// Prepare new assets.\n\tconst toAppend = [ ...newHeadMap.values() ];\n\n\t// Apply the changes.\n\ttoRemove.forEach( ( n ) => n.remove() );\n\tdocument.head.append( ...toAppend );\n};\n\n/**\n * Fetches and processes head assets (stylesheets and scripts) from a specified document.\n *\n * @async\n * @param doc The document from which to fetch head assets. It should support standard DOM querying methods.\n *\n * @return Returns an array of HTML elements representing the head assets.\n */\nexport const fetchHeadAssets = async (\n\tdoc: Document\n): Promise< HTMLElement[] > => {\n\tconst headTags = [];\n\n\t// We only want to fetch module scripts because regular scripts (without\n\t// `async` or `defer` attributes) can depend on the execution of other scripts.\n\t// Scripts found in the head are blocking and must be executed in order.\n\tconst scripts = doc.querySelectorAll< HTMLScriptElement >(\n\t\t'script[type=\"module\"][src]'\n\t);\n\n\tscripts.forEach( ( script ) => {\n\t\tconst src = script.getAttribute( 'src' );\n\t\tif ( ! headElements.has( src ) ) {\n\t\t\t// add the <link> elements to prefetch the module scripts\n\t\t\tconst link = doc.createElement( 'link' );\n\t\t\tlink.rel = 'modulepreload';\n\t\t\tlink.href = src;\n\t\t\tdocument.head.append( link );\n\t\t\theadElements.set( src, { tag: script } );\n\t\t}\n\t} );\n\n\tconst stylesheets = doc.querySelectorAll< HTMLLinkElement >(\n\t\t'link[rel=stylesheet]'\n\t);\n\n\tawait Promise.all(\n\t\tArray.from( stylesheets ).map( async ( tag ) => {\n\t\t\tconst href = tag.getAttribute( 'href' );\n\t\t\tif ( ! href ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! headElements.has( href ) ) {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await fetch( href );\n\t\t\t\t\tconst text = await response.text();\n\t\t\t\t\theadElements.set( href, {\n\t\t\t\t\t\ttag,\n\t\t\t\t\t\ttext,\n\t\t\t\t\t} );\n\t\t\t\t} catch ( e ) {\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.error( e );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst headElement = headElements.get( href );\n\t\t\tconst styleElement = doc.createElement( 'style' );\n\t\t\tstyleElement.textContent = headElement.text;\n\n\t\t\theadTags.push( styleElement );\n\t\t} )\n\t);\n\n\treturn [\n\t\tdoc.querySelector( 'title' ),\n\t\t...doc.querySelectorAll( 'style' ),\n\t\t...headTags,\n\t];\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAO,MAAMA,YAAY,GAAG,IAAIC,GAAG,CAGjC,CAAC;;AAEH;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,UAAU,GAAG,MAAQC,OAA0B,IAAM;EACjE;EACA,MAAMC,QAAQ,GAAKC,GAAY,IAAMA,GAAG,CAACC,EAAE,IAAID,GAAG,CAACE,SAAS;;EAE5D;EACA,MAAMC,UAAU,GAAG,IAAIP,GAAG,CAAoB,CAAC;EAC/C,KAAM,MAAMQ,KAAK,IAAIN,OAAO,EAAG;IAC9BK,UAAU,CAACE,GAAG,CAAEN,QAAQ,CAAEK,KAAM,CAAC,EAAEA,KAAM,CAAC;EAC3C;EAEA,MAAME,QAAmB,GAAG,EAAE;;EAE9B;EACA,KAAM,MAAMF,KAAK,IAAIG,QAAQ,CAACC,IAAI,CAACC,QAAQ,EAAG;IAC7C,MAAMR,EAAE,GAAGF,QAAQ,CAAEK,KAAM,CAAC;IAC5B;IACA,IAAKA,KAAK,CAACM,QAAQ,KAAK,MAAM,IAAIN,KAAK,CAACM,QAAQ,KAAK,OAAO,EAAG;MAC9DJ,QAAQ,CAACK,IAAI,CAAEP,KAAM,CAAC;IACvB,CAAC,MAAM,IAAKD,UAAU,CAACS,GAAG,CAAEX,EAAG,CAAC,EAAG;MAClCE,UAAU,CAACU,MAAM,CAAEZ,EAAG,CAAC;IACxB,CAAC,MAAM,IAAKG,KAAK,CAACM,QAAQ,KAAK,QAAQ,IAAIN,KAA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