@wordpress/interactivity-router 1.6.0 → 1.7.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/CHANGELOG.md +2 -0
- package/build/head.js +103 -0
- package/build/head.js.map +1 -0
- package/build/index.js +116 -23
- package/build/index.js.map +1 -1
- package/build-module/head.js +95 -0
- package/build-module/head.js.map +1 -0
- package/build-module/index.js +117 -22
- package/build-module/index.js.map +1 -1
- package/build-types/head.d.ts +3 -0
- package/build-types/head.d.ts.map +1 -0
- package/build-types/index.d.ts +1 -2
- package/build-types/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/head.js +98 -0
- package/src/index.js +137 -25
- package/tsconfig.tsbuildinfo +1 -1
package/build-module/index.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
var _getConfig$navigation;
|
|
1
2
|
/**
|
|
2
3
|
* WordPress dependencies
|
|
3
4
|
*/
|
|
4
5
|
import { store, privateApis, getConfig } from '@wordpress/interactivity';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
import { fetchHeadAssets, updateHead } from './head';
|
|
5
11
|
const {
|
|
6
12
|
directivePrefix,
|
|
7
13
|
getRegionRootFragment,
|
|
@@ -13,8 +19,12 @@ const {
|
|
|
13
19
|
batch
|
|
14
20
|
} = privateApis('I acknowledge that using private APIs means my theme or plugin will inevitably break in the next version of WordPress.');
|
|
15
21
|
|
|
16
|
-
//
|
|
22
|
+
// Check if the navigation mode is full page or region based.
|
|
23
|
+
const navigationMode = (_getConfig$navigation = getConfig('core/router').navigationMode) !== null && _getConfig$navigation !== void 0 ? _getConfig$navigation : 'regionBased';
|
|
24
|
+
|
|
25
|
+
// The cache of visited and prefetched pages, stylesheets and scripts.
|
|
17
26
|
const pages = new Map();
|
|
27
|
+
const headElements = new Map();
|
|
18
28
|
|
|
19
29
|
// Helper to remove domain and hash from the URL. We are only interesting in
|
|
20
30
|
// caching the path and the query.
|
|
@@ -30,7 +40,9 @@ const fetchPage = async (url, {
|
|
|
30
40
|
try {
|
|
31
41
|
if (!html) {
|
|
32
42
|
const res = await window.fetch(url);
|
|
33
|
-
if (res.status !== 200)
|
|
43
|
+
if (res.status !== 200) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
34
46
|
html = await res.text();
|
|
35
47
|
}
|
|
36
48
|
const dom = new window.DOMParser().parseFromString(html, 'text/html');
|
|
@@ -42,19 +54,29 @@ const fetchPage = async (url, {
|
|
|
42
54
|
|
|
43
55
|
// Return an object with VDOM trees of those HTML regions marked with a
|
|
44
56
|
// `router-region` directive.
|
|
45
|
-
const regionsToVdom = (dom, {
|
|
57
|
+
const regionsToVdom = async (dom, {
|
|
46
58
|
vdom
|
|
47
59
|
} = {}) => {
|
|
48
60
|
const regions = {};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
61
|
+
let head;
|
|
62
|
+
if (process.env.IS_GUTENBERG_PLUGIN) {
|
|
63
|
+
if (navigationMode === 'fullPage') {
|
|
64
|
+
head = await fetchHeadAssets(dom, headElements);
|
|
65
|
+
regions.body = vdom ? vdom.get(document.body) : toVdom(dom.body);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (navigationMode === 'regionBased') {
|
|
69
|
+
const attrName = `data-${directivePrefix}-router-region`;
|
|
70
|
+
dom.querySelectorAll(`[${attrName}]`).forEach(region => {
|
|
71
|
+
const id = region.getAttribute(attrName);
|
|
72
|
+
regions[id] = vdom?.has(region) ? vdom.get(region) : toVdom(region);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
54
75
|
const title = dom.querySelector('title')?.innerText;
|
|
55
76
|
const initialData = parseInitialData(dom);
|
|
56
77
|
return {
|
|
57
78
|
regions,
|
|
79
|
+
head,
|
|
58
80
|
title,
|
|
59
81
|
initialData
|
|
60
82
|
};
|
|
@@ -63,13 +85,23 @@ const regionsToVdom = (dom, {
|
|
|
63
85
|
// Render all interactive regions contained in the given page.
|
|
64
86
|
const renderRegions = page => {
|
|
65
87
|
batch(() => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
88
|
+
if (process.env.IS_GUTENBERG_PLUGIN) {
|
|
89
|
+
if (navigationMode === 'fullPage') {
|
|
90
|
+
// Once this code is tested and more mature, the head should be updated for region based navigation as well.
|
|
91
|
+
updateHead(page.head);
|
|
92
|
+
const fragment = getRegionRootFragment(document.body);
|
|
93
|
+
render(page.regions.body, fragment);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (navigationMode === 'regionBased') {
|
|
97
|
+
populateInitialData(page.initialData);
|
|
98
|
+
const attrName = `data-${directivePrefix}-router-region`;
|
|
99
|
+
document.querySelectorAll(`[${attrName}]`).forEach(region => {
|
|
100
|
+
const id = region.getAttribute(attrName);
|
|
101
|
+
const fragment = getRegionRootFragment(region);
|
|
102
|
+
render(page.regions[id], fragment);
|
|
103
|
+
});
|
|
104
|
+
}
|
|
73
105
|
if (page.title) {
|
|
74
106
|
document.title = page.title;
|
|
75
107
|
}
|
|
@@ -105,11 +137,38 @@ window.addEventListener('popstate', async () => {
|
|
|
105
137
|
}
|
|
106
138
|
});
|
|
107
139
|
|
|
108
|
-
//
|
|
140
|
+
// Initialize the router and cache the initial page using the initial vDOM.
|
|
141
|
+
// Once this code is tested and more mature, the head should be updated for region based navigation as well.
|
|
142
|
+
if (process.env.IS_GUTENBERG_PLUGIN) {
|
|
143
|
+
if (navigationMode === 'fullPage') {
|
|
144
|
+
// Cache the scripts. Has to be called before fetching the assets.
|
|
145
|
+
[].map.call(document.querySelectorAll('script[src]'), script => {
|
|
146
|
+
headElements.set(script.getAttribute('src'), {
|
|
147
|
+
tag: script,
|
|
148
|
+
text: script.textContent
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
await fetchHeadAssets(document, headElements);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
109
154
|
pages.set(getPagePath(window.location), Promise.resolve(regionsToVdom(document, {
|
|
110
155
|
vdom: initialVdom
|
|
111
156
|
})));
|
|
112
157
|
|
|
158
|
+
// Check if the link is valid for client-side navigation.
|
|
159
|
+
const isValidLink = ref => ref && ref instanceof window.HTMLAnchorElement && ref.href && (!ref.target || ref.target === '_self') && ref.origin === window.location.origin && !ref.pathname.startsWith('/wp-admin') && !ref.pathname.startsWith('/wp-login.php') && !ref.getAttribute('href').startsWith('#') && !new URL(ref.href).searchParams.has('_wpnonce');
|
|
160
|
+
|
|
161
|
+
// Check if the event is valid for client-side navigation.
|
|
162
|
+
const isValidEvent = event => event && event.button === 0 &&
|
|
163
|
+
// Left clicks only.
|
|
164
|
+
!event.metaKey &&
|
|
165
|
+
// Open in new tab (Mac).
|
|
166
|
+
!event.ctrlKey &&
|
|
167
|
+
// Open in new tab (Windows).
|
|
168
|
+
!event.altKey &&
|
|
169
|
+
// Download.
|
|
170
|
+
!event.shiftKey && !event.defaultPrevented;
|
|
171
|
+
|
|
113
172
|
// Variable to store the current navigation.
|
|
114
173
|
let navigatingTo = '';
|
|
115
174
|
export const {
|
|
@@ -168,7 +227,9 @@ export const {
|
|
|
168
227
|
|
|
169
228
|
// Don't update the navigation status immediately, wait 400 ms.
|
|
170
229
|
const loadingTimeout = setTimeout(() => {
|
|
171
|
-
if (navigatingTo !== href)
|
|
230
|
+
if (navigatingTo !== href) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
172
233
|
if (loadingAnimation) {
|
|
173
234
|
navigation.hasStarted = true;
|
|
174
235
|
navigation.hasFinished = false;
|
|
@@ -185,9 +246,11 @@ export const {
|
|
|
185
246
|
// Once the page is fetched, the destination URL could have changed
|
|
186
247
|
// (e.g., by clicking another link in the meantime). If so, bail
|
|
187
248
|
// out, and let the newer execution to update the HTML.
|
|
188
|
-
if (navigatingTo !== href)
|
|
249
|
+
if (navigatingTo !== href) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
189
252
|
if (page && !page.initialData?.config?.['core/router']?.clientNavigationDisabled) {
|
|
190
|
-
renderRegions(page);
|
|
253
|
+
yield renderRegions(page);
|
|
191
254
|
window.history[options.replace ? 'replaceState' : 'pushState']({}, '', href);
|
|
192
255
|
|
|
193
256
|
// Update the URL in the state.
|
|
@@ -205,6 +268,14 @@ export const {
|
|
|
205
268
|
// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26
|
|
206
269
|
navigation.message = navigation.texts.loaded + (navigation.message === navigation.texts.loaded ? '\u00A0' : '');
|
|
207
270
|
}
|
|
271
|
+
|
|
272
|
+
// Scroll to the anchor if exits in the link.
|
|
273
|
+
const {
|
|
274
|
+
hash
|
|
275
|
+
} = new URL(href, window.location);
|
|
276
|
+
if (hash) {
|
|
277
|
+
document.querySelector(hash)?.scrollIntoView();
|
|
278
|
+
}
|
|
208
279
|
} else {
|
|
209
280
|
yield forcePageReload(href);
|
|
210
281
|
}
|
|
@@ -218,14 +289,15 @@ export const {
|
|
|
218
289
|
* @param {string} url The page URL.
|
|
219
290
|
* @param {Object} [options] Options object.
|
|
220
291
|
* @param {boolean} [options.force] Force fetching the URL again.
|
|
221
|
-
* @param {string} [options.html] HTML string to be used instead of
|
|
222
|
-
* fetching the requested URL.
|
|
292
|
+
* @param {string} [options.html] HTML string to be used instead of fetching the requested URL.
|
|
223
293
|
*/
|
|
224
294
|
prefetch(url, options = {}) {
|
|
225
295
|
const {
|
|
226
296
|
clientNavigationDisabled
|
|
227
297
|
} = getConfig();
|
|
228
|
-
if (clientNavigationDisabled)
|
|
298
|
+
if (clientNavigationDisabled) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
229
301
|
const pagePath = getPagePath(url);
|
|
230
302
|
if (options.force || !pages.has(pagePath)) {
|
|
231
303
|
pages.set(pagePath, fetchPage(pagePath, options));
|
|
@@ -233,4 +305,27 @@ export const {
|
|
|
233
305
|
}
|
|
234
306
|
}
|
|
235
307
|
});
|
|
308
|
+
|
|
309
|
+
// Add click and prefetch to all links.
|
|
310
|
+
if (process.env.IS_GUTENBERG_PLUGIN) {
|
|
311
|
+
if (navigationMode === 'fullPage') {
|
|
312
|
+
// Navigate on click.
|
|
313
|
+
document.addEventListener('click', function (event) {
|
|
314
|
+
const ref = event.target.closest('a');
|
|
315
|
+
if (isValidLink(ref) && isValidEvent(event)) {
|
|
316
|
+
event.preventDefault();
|
|
317
|
+
actions.navigate(ref.href);
|
|
318
|
+
}
|
|
319
|
+
}, true);
|
|
320
|
+
// Prefetch on hover.
|
|
321
|
+
document.addEventListener('mouseenter', function (event) {
|
|
322
|
+
if (event.target?.nodeName === 'A') {
|
|
323
|
+
const ref = event.target.closest('a');
|
|
324
|
+
if (isValidLink(ref) && isValidEvent(event)) {
|
|
325
|
+
actions.prefetch(ref.href);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}, true);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
236
331
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["store","privateApis","getConfig","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseInitialData","populateInitialData","batch","pages","Map","getPagePath","url","u","URL","window","location","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","e","vdom","regions","attrName","querySelectorAll","forEach","region","id","getAttribute","has","get","title","querySelector","innerText","initialData","renderRegions","page","document","fragment","forcePageReload","href","assign","Promise","addEventListener","pagePath","state","reload","set","resolve","navigatingTo","actions","navigation","hasStarted","hasFinished","texts","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","message","loading","race","clearTimeout","config","history","replace","loaded","force"],"sources":["@wordpress/interactivity-router/src/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store, privateApis, getConfig } from '@wordpress/interactivity';\n\nconst {\n\tdirectivePrefix,\n\tgetRegionRootFragment,\n\tinitialVdom,\n\ttoVdom,\n\trender,\n\tparseInitialData,\n\tpopulateInitialData,\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\n// The cache of visited and prefetched pages.\nconst pages = new Map();\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 ) => {\n\tconst u = new URL( url, window.location );\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, { html } ) => {\n\ttry {\n\t\tif ( ! html ) {\n\t\t\tconst res = await window.fetch( url );\n\t\t\tif ( res.status !== 200 ) return false;\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 = ( dom, { vdom } = {} ) => {\n\tconst regions = {};\n\tconst attrName = `data-${ directivePrefix }-router-region`;\n\tdom.querySelectorAll( `[${ attrName }]` ).forEach( ( region ) => {\n\t\tconst id = region.getAttribute( attrName );\n\t\tregions[ id ] = vdom?.has( region )\n\t\t\t? vdom.get( region )\n\t\t\t: toVdom( region );\n\t} );\n\tconst title = dom.querySelector( 'title' )?.innerText;\n\tconst initialData = parseInitialData( dom );\n\treturn { regions, title, initialData };\n};\n\n// Render all interactive regions contained in the given page.\nconst renderRegions = ( page ) => {\n\tbatch( () => {\n\t\tpopulateInitialData( page.initialData );\n\t\tconst attrName = `data-${ directivePrefix }-router-region`;\n\t\tdocument.querySelectorAll( `[${ attrName }]` ).forEach( ( region ) => {\n\t\t\tconst id = region.getAttribute( attrName );\n\t\t\tconst fragment = getRegionRootFragment( region );\n\t\t\trender( page.regions[ id ], fragment );\n\t\t} );\n\t\tif ( page.title ) {\n\t\t\tdocument.title = page.title;\n\t\t}\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 {string} href The page href.\n * @return {Promise} Promise that never resolves.\n */\nconst forcePageReload = ( href ) => {\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 ); // Remove hash.\n\tconst page = pages.has( pagePath ) && ( await pages.get( pagePath ) );\n\tif ( page ) {\n\t\trenderRegions( 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// Cache the initial page using the intially parsed vDOM.\npages.set(\n\tgetPagePath( window.location ),\n\tPromise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )\n);\n\n// Variable to store the current navigation.\nlet navigatingTo = '';\n\nexport const { state, actions } = 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\ttexts: {},\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, fetchs 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 {string} href The page href.\n\t\t * @param {Object} [options] Options object.\n\t\t * @param {boolean} [options.force] If true, it forces re-fetching the URL.\n\t\t * @param {string} [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t * @param {boolean} [options.replace] If true, it replaces the current entry in the browser session history.\n\t\t * @param {number} [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000.\n\t\t * @param {boolean} [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`.\n\t\t * @param {boolean} [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`.\n\t\t *\n\t\t * @return {Promise} Promise that resolves once the navigation is completed or aborted.\n\t\t */\n\t\t*navigate( href, options = {} ) {\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( ( 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 ) return;\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\tnavigation.message = navigation.texts.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 ) return;\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\trenderRegions( 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\t// Announce that the page has been loaded. If the message is the\n\t\t\t\t\t// same, we use a no-break space similar to the @wordpress/a11y\n\t\t\t\t\t// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26\n\t\t\t\t\tnavigation.message =\n\t\t\t\t\t\tnavigation.texts.loaded +\n\t\t\t\t\t\t( navigation.message === navigation.texts.loaded\n\t\t\t\t\t\t\t? '\\u00A0'\n\t\t\t\t\t\t\t: '' );\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 * Prefetchs 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 {string} url The page URL.\n\t\t * @param {Object} [options] Options object.\n\t\t * @param {boolean} [options.force] Force fetching the URL again.\n\t\t * @param {string} [options.html] HTML string to be used instead of\n\t\t * fetching the requested URL.\n\t\t */\n\t\tprefetch( url, options = {} ) {\n\t\t\tconst { clientNavigationDisabled } = getConfig();\n\t\t\tif ( clientNavigationDisabled ) return;\n\n\t\t\tconst pagePath = getPagePath( url );\n\t\t\tif ( options.force || ! pages.has( pagePath ) ) {\n\t\t\t\tpages.set( pagePath, fetchPage( pag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"names":["store","privateApis","getConfig","fetchHeadAssets","updateHead","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseInitialData","populateInitialData","batch","navigationMode","_getConfig$navigation","pages","Map","headElements","getPagePath","url","u","URL","window","location","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","e","vdom","regions","head","process","env","IS_GUTENBERG_PLUGIN","body","get","document","attrName","querySelectorAll","forEach","region","id","getAttribute","has","title","querySelector","innerText","initialData","renderRegions","page","fragment","forcePageReload","href","assign","Promise","addEventListener","pagePath","state","reload","map","call","script","set","tag","textContent","resolve","isValidLink","ref","HTMLAnchorElement","target","origin","startsWith","searchParams","isValidEvent","event","button","metaKey","ctrlKey","altKey","shiftKey","defaultPrevented","navigatingTo","actions","navigation","hasStarted","hasFinished","texts","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","message","loading","race","clearTimeout","config","history","replace","loaded","hash","scrollIntoView","force","closest","preventDefault","nodeName"],"sources":["@wordpress/interactivity-router/src/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { store, privateApis, getConfig } from '@wordpress/interactivity';\n\n/**\n * Internal dependencies\n */\nimport { fetchHeadAssets, updateHead } from './head';\n\nconst {\n\tdirectivePrefix,\n\tgetRegionRootFragment,\n\tinitialVdom,\n\ttoVdom,\n\trender,\n\tparseInitialData,\n\tpopulateInitialData,\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\n// Check if the navigation mode is full page or region based.\nconst navigationMode =\n\tgetConfig( 'core/router' ).navigationMode ?? 'regionBased';\n\n// The cache of visited and prefetched pages, stylesheets and scripts.\nconst pages = new Map();\nconst headElements = new Map();\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 ) => {\n\tconst u = new URL( url, window.location );\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, { html } ) => {\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 = async ( dom, { vdom } = {} ) => {\n\tconst regions = {};\n\tlet head;\n\tif ( process.env.IS_GUTENBERG_PLUGIN ) {\n\t\tif ( navigationMode === 'fullPage' ) {\n\t\t\thead = await fetchHeadAssets( dom, headElements );\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 = parseInitialData( dom );\n\treturn { regions, head, title, initialData };\n};\n\n// Render all interactive regions contained in the given page.\nconst renderRegions = ( page ) => {\n\tbatch( () => {\n\t\tif ( process.env.IS_GUTENBERG_PLUGIN ) {\n\t\t\tif ( navigationMode === 'fullPage' ) {\n\t\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\t\tupdateHead( page.head );\n\t\t\t\tconst fragment = getRegionRootFragment( document.body );\n\t\t\t\trender( page.regions.body, fragment );\n\t\t\t}\n\t\t}\n\t\tif ( navigationMode === 'regionBased' ) {\n\t\t\tpopulateInitialData( page.initialData );\n\t\t\tconst attrName = `data-${ directivePrefix }-router-region`;\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\tif ( page.title ) {\n\t\t\tdocument.title = page.title;\n\t\t}\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 {string} href The page href.\n * @return {Promise} Promise that never resolves.\n */\nconst forcePageReload = ( href ) => {\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 ); // Remove hash.\n\tconst page = pages.has( pagePath ) && ( await pages.get( pagePath ) );\n\tif ( page ) {\n\t\trenderRegions( 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 region based navigation as well.\nif ( process.env.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( document.querySelectorAll( 'script[src]' ), ( script ) => {\n\t\t\theadElements.set( script.getAttribute( 'src' ), {\n\t\t\t\ttag: script,\n\t\t\t\ttext: script.textContent,\n\t\t\t} );\n\t\t} );\n\t\tawait fetchHeadAssets( document, headElements );\n\t}\n}\npages.set(\n\tgetPagePath( window.location ),\n\tPromise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )\n);\n\n// Check if the link is valid for client-side navigation.\nconst isValidLink = ( ref ) =>\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 ) =>\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\nexport const { state, actions } = 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\ttexts: {},\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, fetchs 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 {string} href The page href.\n\t\t * @param {Object} [options] Options object.\n\t\t * @param {boolean} [options.force] If true, it forces re-fetching the URL.\n\t\t * @param {string} [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t * @param {boolean} [options.replace] If true, it replaces the current entry in the browser session history.\n\t\t * @param {number} [options.timeout] Time until the navigation is aborted, in milliseconds. Default is 10000.\n\t\t * @param {boolean} [options.loadingAnimation] Whether an animation should be shown while navigating. Default to `true`.\n\t\t * @param {boolean} [options.screenReaderAnnouncement] Whether a message for screen readers should be announced while navigating. Default to `true`.\n\t\t *\n\t\t * @return {Promise} Promise that resolves once the navigation is completed or aborted.\n\t\t */\n\t\t*navigate( href, options = {} ) {\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( ( 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\tnavigation.message = navigation.texts.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\t// Announce that the page has been loaded. If the message is the\n\t\t\t\t\t// same, we use a no-break space similar to the @wordpress/a11y\n\t\t\t\t\t// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26\n\t\t\t\t\tnavigation.message =\n\t\t\t\t\t\tnavigation.texts.loaded +\n\t\t\t\t\t\t( navigation.message === navigation.texts.loaded\n\t\t\t\t\t\t\t? '\\u00A0'\n\t\t\t\t\t\t\t: '' );\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 );\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 * Prefetchs 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 {string} url The page URL.\n\t\t * @param {Object} [options] Options object.\n\t\t * @param {boolean} [options.force] Force fetching the URL again.\n\t\t * @param {string} [options.html] HTML string to be used instead of fetching the requested URL.\n\t\t */\n\t\tprefetch( url, options = {} ) {\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( pagePath, fetchPage( pagePath, options ) );\n\t\t\t}\n\t\t},\n\t},\n} );\n\n// Add click and prefetch to all links.\nif ( process.env.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.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?.nodeName === 'A' ) {\n\t\t\t\t\tconst ref = event.target.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":";AAAA;AACA;AACA;AACA,SAASA,KAAK,EAAEC,WAAW,EAAEC,SAAS,QAAQ,0BAA0B;;AAExE;AACA;AACA;AACA,SAASC,eAAe,EAAEC,UAAU,QAAQ,QAAQ;AAEpD,MAAM;EACLC,eAAe;EACfC,qBAAqB;EACrBC,WAAW;EACXC,MAAM;EACNC,MAAM;EACNC,gBAAgB;EAChBC,mBAAmB;EACnBC;AACD,CAAC,GAAGX,WAAW,CACd,wHACD,CAAC;;AAED;AACA,MAAMY,cAAc,IAAAC,qBAAA,GACnBZ,SAAS,CAAE,aAAc,CAAC,CAACW,cAAc,cAAAC,qBAAA,cAAAA,qBAAA,GAAI,aAAa;;AAE3D;AACA,MAAMC,KAAK,GAAG,IAAIC,GAAG,CAAC,CAAC;AACvB,MAAMC,YAAY,GAAG,IAAID,GAAG,CAAC,CAAC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AAK,EAAG;MAClB,IAAKA,KAAK,CAACL,MAAM,EAAEyC,QAAQ,KAAK,GAAG,EAAG;QACrC,MAAM3C,GAAG,GAAGO,KAAK,CAACL,MAAM,CAACuC,OAAO,CAAE,GAAI,CAAC;QACvC,IAAK1C,WAAW,CAAEC,GAAI,CAAC,IAAIM,YAAY,CAAEC,KAAM,CAAC,EAAG;UAClDQ,OAAO,CAACW,QAAQ,CAAE1B,GAAG,CAACf,IAAK,CAAC;QAC7B;MACD;IACD,CAAC,EACD,IACD,CAAC;EACF;AACD","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"head.d.ts","sourceRoot":"","sources":["../src/head.js"],"names":[],"mappings":"AAOO,0DA+BN;AAWM,qCALI,QAAQ,gCAGP,QAAQ,WAAW,EAAE,CAAC,CAkDjC"}
|
package/build-types/index.d.ts
CHANGED
|
@@ -42,8 +42,7 @@ export namespace actions {
|
|
|
42
42
|
* @param {string} url The page URL.
|
|
43
43
|
* @param {Object} [options] Options object.
|
|
44
44
|
* @param {boolean} [options.force] Force fetching the URL again.
|
|
45
|
-
* @param {string} [options.html] HTML string to be used instead of
|
|
46
|
-
* fetching the requested URL.
|
|
45
|
+
* @param {string} [options.html] HTML string to be used instead of fetching the requested URL.
|
|
47
46
|
*/
|
|
48
47
|
function prefetch(url: string, options?: {
|
|
49
48
|
force?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";;;;;;;;;IAiME;;;;;;;;;;;;;;;;;OAiBG;IACH;;;;;;;qBA4FC;IAED;;;;;;;;;;OAUG;IACH;;;aAUC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/interactivity-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Package that exposes state and actions from the `core/router` store, part of the Interactivity API.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"react-native": "src/index",
|
|
27
27
|
"types": "build-types",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@wordpress/interactivity": "^5.
|
|
29
|
+
"@wordpress/interactivity": "^5.6.0"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "581d8a5580dba8f600b7268d51eb554771ae482c"
|
|
35
35
|
}
|
package/src/head.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper to update only the necessary tags in the head.
|
|
3
|
+
*
|
|
4
|
+
* @async
|
|
5
|
+
* @param {Array} newHead The head elements of the new page.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export const updateHead = async ( newHead ) => {
|
|
9
|
+
// Helper to get the tag id store in the cache.
|
|
10
|
+
const getTagId = ( tag ) => tag.id || tag.outerHTML;
|
|
11
|
+
|
|
12
|
+
// Map incoming head tags by their content.
|
|
13
|
+
const newHeadMap = new Map();
|
|
14
|
+
for ( const child of newHead ) {
|
|
15
|
+
newHeadMap.set( getTagId( child ), child );
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const toRemove = [];
|
|
19
|
+
|
|
20
|
+
// Detect nodes that should be added or removed.
|
|
21
|
+
for ( const child of document.head.children ) {
|
|
22
|
+
const id = getTagId( child );
|
|
23
|
+
// Always remove styles and links as they might change.
|
|
24
|
+
if ( child.nodeName === 'LINK' || child.nodeName === 'STYLE' ) {
|
|
25
|
+
toRemove.push( child );
|
|
26
|
+
} else if ( newHeadMap.has( id ) ) {
|
|
27
|
+
newHeadMap.delete( id );
|
|
28
|
+
} else if ( child.nodeName !== 'SCRIPT' && child.nodeName !== 'META' ) {
|
|
29
|
+
toRemove.push( child );
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Prepare new assets.
|
|
34
|
+
const toAppend = [ ...newHeadMap.values() ];
|
|
35
|
+
|
|
36
|
+
// Apply the changes.
|
|
37
|
+
toRemove.forEach( ( n ) => n.remove() );
|
|
38
|
+
document.head.append( ...toAppend );
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Fetches and processes head assets (stylesheets and scripts) from a specified document.
|
|
43
|
+
*
|
|
44
|
+
* @async
|
|
45
|
+
* @param {Document} doc The document from which to fetch head assets. It should support standard DOM querying methods.
|
|
46
|
+
* @param {Map} headElements A map of head elements to modify tracking the URLs of already processed assets to avoid duplicates.
|
|
47
|
+
*
|
|
48
|
+
* @return {Promise<HTMLElement[]>} Returns an array of HTML elements representing the head assets.
|
|
49
|
+
*/
|
|
50
|
+
export const fetchHeadAssets = async ( doc, headElements ) => {
|
|
51
|
+
const headTags = [];
|
|
52
|
+
const assets = [
|
|
53
|
+
{
|
|
54
|
+
tagName: 'style',
|
|
55
|
+
selector: 'link[rel=stylesheet]',
|
|
56
|
+
attribute: 'href',
|
|
57
|
+
},
|
|
58
|
+
{ tagName: 'script', selector: 'script[src]', attribute: 'src' },
|
|
59
|
+
];
|
|
60
|
+
for ( const asset of assets ) {
|
|
61
|
+
const { tagName, selector, attribute } = asset;
|
|
62
|
+
const tags = doc.querySelectorAll( selector );
|
|
63
|
+
|
|
64
|
+
// Use Promise.all to wait for fetch to complete
|
|
65
|
+
await Promise.all(
|
|
66
|
+
Array.from( tags ).map( async ( tag ) => {
|
|
67
|
+
const attributeValue = tag.getAttribute( attribute );
|
|
68
|
+
if ( ! headElements.has( attributeValue ) ) {
|
|
69
|
+
try {
|
|
70
|
+
const response = await fetch( attributeValue );
|
|
71
|
+
const text = await response.text();
|
|
72
|
+
headElements.set( attributeValue, {
|
|
73
|
+
tag,
|
|
74
|
+
text,
|
|
75
|
+
} );
|
|
76
|
+
} catch ( e ) {
|
|
77
|
+
// eslint-disable-next-line no-console
|
|
78
|
+
console.error( e );
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const headElement = headElements.get( attributeValue );
|
|
83
|
+
const element = doc.createElement( tagName );
|
|
84
|
+
element.innerText = headElement.text;
|
|
85
|
+
for ( const attr of headElement.tag.attributes ) {
|
|
86
|
+
element.setAttribute( attr.name, attr.value );
|
|
87
|
+
}
|
|
88
|
+
headTags.push( element );
|
|
89
|
+
} )
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [
|
|
94
|
+
doc.querySelector( 'title' ),
|
|
95
|
+
...doc.querySelectorAll( 'style' ),
|
|
96
|
+
...headTags,
|
|
97
|
+
];
|
|
98
|
+
};
|