@wordpress/interactivity-router 2.18.0 → 2.19.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 +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/build-module/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { store, privateApis, getConfig } from '@wordpress/interactivity';
|
|
|
7
7
|
/**
|
|
8
8
|
* Internal dependencies
|
|
9
9
|
*/
|
|
10
|
-
import {
|
|
10
|
+
import { fetchHeadAssets, updateHead, headElements } from './head';
|
|
11
11
|
const {
|
|
12
12
|
directivePrefix,
|
|
13
13
|
getRegionRootFragment,
|
|
@@ -44,9 +44,7 @@ const fetchPage = async (url, {
|
|
|
44
44
|
html = await res.text();
|
|
45
45
|
}
|
|
46
46
|
const dom = new window.DOMParser().parseFromString(html, 'text/html');
|
|
47
|
-
return regionsToVdom(dom
|
|
48
|
-
baseUrl: url
|
|
49
|
-
});
|
|
47
|
+
return regionsToVdom(dom);
|
|
50
48
|
} catch (e) {
|
|
51
49
|
return false;
|
|
52
50
|
}
|
|
@@ -54,17 +52,16 @@ const fetchPage = async (url, {
|
|
|
54
52
|
|
|
55
53
|
// Return an object with VDOM trees of those HTML regions marked with a
|
|
56
54
|
// `router-region` directive.
|
|
57
|
-
const regionsToVdom = (dom, {
|
|
58
|
-
vdom
|
|
59
|
-
baseUrl
|
|
55
|
+
const regionsToVdom = async (dom, {
|
|
56
|
+
vdom
|
|
60
57
|
} = {}) => {
|
|
61
58
|
const regions = {
|
|
62
59
|
body: undefined
|
|
63
60
|
};
|
|
64
|
-
|
|
65
|
-
const scriptModules = [...dom.querySelectorAll('script[type=module][src]')].map(s => s.src);
|
|
61
|
+
let head;
|
|
66
62
|
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
67
63
|
if (navigationMode === 'fullPage') {
|
|
64
|
+
head = await fetchHeadAssets(dom);
|
|
68
65
|
regions.body = vdom ? vdom.get(document.body) : toVdom(dom.body);
|
|
69
66
|
}
|
|
70
67
|
}
|
|
@@ -79,8 +76,7 @@ const regionsToVdom = (dom, {
|
|
|
79
76
|
const initialData = parseServerData(dom);
|
|
80
77
|
return {
|
|
81
78
|
regions,
|
|
82
|
-
|
|
83
|
-
scriptModules,
|
|
79
|
+
head,
|
|
84
80
|
title,
|
|
85
81
|
initialData
|
|
86
82
|
};
|
|
@@ -88,15 +84,10 @@ const regionsToVdom = (dom, {
|
|
|
88
84
|
|
|
89
85
|
// Render all interactive regions contained in the given page.
|
|
90
86
|
const renderRegions = async page => {
|
|
91
|
-
// Wait for styles and modules to be ready.
|
|
92
|
-
await Promise.all([...page.styles, ...page.scriptModules.map(src => import(/* webpackIgnore: true */src))]);
|
|
93
|
-
// Replace style sheets.
|
|
94
|
-
const sheets = await Promise.all(page.styles);
|
|
95
|
-
window.document.querySelectorAll('style,link[rel=stylesheet]').forEach(element => element.remove());
|
|
96
|
-
window.document.adoptedStyleSheets = sheets;
|
|
97
87
|
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
98
88
|
if (navigationMode === 'fullPage') {
|
|
99
|
-
//
|
|
89
|
+
// Once this code is tested and more mature, the head should be updated for region based navigation as well.
|
|
90
|
+
await updateHead(page.head);
|
|
100
91
|
const fragment = getRegionRootFragment(document.body);
|
|
101
92
|
batch(() => {
|
|
102
93
|
populateServerData(page.initialData);
|
|
@@ -152,9 +143,19 @@ window.addEventListener('popstate', async () => {
|
|
|
152
143
|
// Initialize the router and cache the initial page using the initial vDOM.
|
|
153
144
|
// Once this code is tested and more mature, the head should be updated for
|
|
154
145
|
// region based navigation as well.
|
|
146
|
+
if (globalThis.IS_GUTENBERG_PLUGIN) {
|
|
147
|
+
if (navigationMode === 'fullPage') {
|
|
148
|
+
// Cache the scripts. Has to be called before fetching the assets.
|
|
149
|
+
[].map.call(document.querySelectorAll('script[type="module"][src]'), script => {
|
|
150
|
+
headElements.set(script.getAttribute('src'), {
|
|
151
|
+
tag: script
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
await fetchHeadAssets(document);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
155
157
|
pages.set(getPagePath(window.location.href), Promise.resolve(regionsToVdom(document, {
|
|
156
|
-
vdom: initialVdom
|
|
157
|
-
baseUrl: window.location.href
|
|
158
|
+
vdom: initialVdom
|
|
158
159
|
})));
|
|
159
160
|
|
|
160
161
|
// Check if the link is valid for client-side navigation.
|
|
@@ -185,7 +186,6 @@ export const {
|
|
|
185
186
|
state: {
|
|
186
187
|
url: window.location.href,
|
|
187
188
|
navigation: {
|
|
188
|
-
isLoading: false,
|
|
189
189
|
hasStarted: false,
|
|
190
190
|
hasFinished: false
|
|
191
191
|
}
|
|
@@ -237,7 +237,6 @@ export const {
|
|
|
237
237
|
if (navigatingTo !== href) {
|
|
238
238
|
return;
|
|
239
239
|
}
|
|
240
|
-
navigation.isLoading = true;
|
|
241
240
|
if (loadingAnimation) {
|
|
242
241
|
navigation.hasStarted = true;
|
|
243
242
|
navigation.hasFinished = false;
|
|
@@ -266,7 +265,6 @@ export const {
|
|
|
266
265
|
|
|
267
266
|
// Update the navigation status once the the new page rendering
|
|
268
267
|
// has been completed.
|
|
269
|
-
navigation.isLoading = false;
|
|
270
268
|
if (loadingAnimation) {
|
|
271
269
|
navigation.hasStarted = false;
|
|
272
270
|
navigation.hasFinished = true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["store","privateApis","getConfig","generateCSSStyleSheets","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseServerData","populateServerData","batch","navigationMode","_getConfig$navigation","pages","Map","getPagePath","url","u","URL","window","location","href","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","baseUrl","e","vdom","regions","body","undefined","styles","scriptModules","querySelectorAll","map","s","src","globalThis","IS_GUTENBERG_PLUGIN","get","document","attrName","forEach","region","id","getAttribute","has","title","querySelector","innerText","initialData","renderRegions","page","Promise","all","sheets","element","remove","adoptedStyleSheets","fragment","forcePageReload","assign","addEventListener","pagePath","state","reload","set","resolve","isValidLink","ref","HTMLAnchorElement","target","origin","startsWith","searchParams","isValidEvent","event","button","metaKey","ctrlKey","altKey","shiftKey","defaultPrevented","navigatingTo","hasLoadedNavigationTextsData","navigationTexts","loading","loaded","actions","navigation","isLoading","hasStarted","hasFinished","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","a11ySpeak","race","clearTimeout","config","history","replace","hash","scrollIntoView","force","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 { 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\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,sBAAsB,QAAQ,iBAAiB;AAExD,MAAM;EACLC,eAAe;EACfC,qBAAqB;EACrBC,WAAW;EACXC,MAAM;EACNC,MAAM;EACNC,eAAe;EACfC,kBAAkB;EAClBC;AACD,CAAC,GAAGV,WAAW,CACd,wHACD,CAAC;AA+BD;AACA,MAAMW,cAA0C,IAAAC,qBAAA,GAC/CX,SAAS,CAAE,aAAc,CAAC,CAACU,cAAc,cAAAC,qBAAA,cAAAA,qBAAA,GAAI,aAAa;;AAE3D;AACA,MAAMC,KAAK,GAAG,IAAIC,GAAG,CAAoC,CAAC;;AAE1D;AACA;AACA,MAAMC,WAAW,GAAKC,GAAW,IAAM;EACtC,MAAMC,CAAC,GAAG,IAAIC,GAAG,CAAEF,GAAG,EAAEG,MAAM,CAACC,QAAQ,CAACC,IAAK,CAAC;EAC9C,OAAOJ,CAAC,CAACK,QAAQ,GAAGL,CAAC,CAACM,MAAM;AAC7B,CAAC;;AAED;AACA,MAAMC,SAAS,GAAG,MAAAA,CAAQR,GAAW,EAAE;EAAES;AAAuB,CAAC,KAAM;EACtE,IAAI;IACH,IAAK,CAAEA,IAAI,EAAG;MACb,MAAMC,GAAG,GAAG,MAAMP,MAAM,CAACQ,KAAK,CAAEX,GAAI,CAAC;MACrC,IAAKU,GAAG,CAACE,MAAM,KAAK,GAAG,EAAG;QACzB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MAAM,EAAeuD,QAAQ,KAAK,GAAG,EAAG;QACpD,MAAMzD,GAAG,GAAKO,KAAK,CAACL,MAAM,CAAcqD,OAAO,CAAE,GAAI,CAAC;QACtD,IAAKxD,WAAW,CAAEC,GAAI,CAAC,IAAIM,YAAY,CAAEC,KAAM,CAAC,EAAG;UAClDY,OAAO,CAACW,QAAQ,CAAE9B,GAAG,CAACzD,IAAK,CAAC;QAC7B;MACD;IACD,CAAC,EACD,IACD,CAAC;EACF;AACD","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["store","privateApis","getConfig","fetchHeadAssets","updateHead","headElements","directivePrefix","getRegionRootFragment","initialVdom","toVdom","render","parseServerData","populateServerData","batch","navigationMode","_getConfig$navigation","pages","Map","getPagePath","url","u","URL","window","location","href","pathname","search","fetchPage","html","res","fetch","status","text","dom","DOMParser","parseFromString","regionsToVdom","e","vdom","regions","body","undefined","head","globalThis","IS_GUTENBERG_PLUGIN","get","document","attrName","querySelectorAll","forEach","region","id","getAttribute","has","title","querySelector","innerText","initialData","renderRegions","page","fragment","forcePageReload","assign","Promise","addEventListener","pagePath","state","reload","map","call","script","set","tag","resolve","isValidLink","ref","HTMLAnchorElement","target","origin","startsWith","searchParams","isValidEvent","event","button","metaKey","ctrlKey","altKey","shiftKey","defaultPrevented","navigatingTo","hasLoadedNavigationTextsData","navigationTexts","loading","loaded","actions","navigation","hasStarted","hasFinished","navigate","options","clientNavigationDisabled","loadingAnimation","screenReaderAnnouncement","timeout","prefetch","timeoutPromise","setTimeout","loadingTimeout","a11ySpeak","race","clearTimeout","config","history","replace","hash","scrollIntoView","force","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":";AAAA;AACA;AACA;AACA,SAASA,KAAK,EAAEC,WAAW,EAAEC,SAAS,QAAQ,0BAA0B;;AAExE;AACA;AACA;AACA,SAASC,eAAe,EAAEC,UAAU,EAAEC,YAAY,QAAQ,QAAQ;AAElE,MAAM;EACLC,eAAe;EACfC,qBAAqB;EACrBC,WAAW;EACXC,MAAM;EACNC,MAAM;EACNC,eAAe;EACfC,kBAAkB;EAClBC;AACD,CAAC,GAAGZ,WAAW,CACd,wHACD,CAAC;AA6BD;AACA,MAAMa,cAA0C,IAAAC,qBAAA,GAC/Cb,SAAS,CAAE,aAAc,CAAC,CAACY,cAAc,cAAAC,qBAAA,cAAAA,qBAAA,GAAI,aAAa;;AAE3D;AACA,MAAMC,KAAK,GAAG,IAAIC,GAAG,CAAoC,CAAC;;AAE1D;AACA;AACA,MAAMC,WAAW,GAAKC,GAAW,IAAM;EACtC,MAAMC,CAAC,GAAG,IAAIC,GAAG,CAAEF,GAAG,EAAEG,MAAM,CAACC,QAAQ,CAACC,IAAK,CAAC;EAC9C,OAAOJ,CAAC,CAACK,QAAQ,GAAGL,CAAC,CAACM,MAAM;AAC7B,CAAC;;AAED;AACA,MAAMC,SAAS,GAAG,MAAAA,CAAQR,GAAW,EAAE;EAAES;AAAuB,CAAC,KAAM;EACtE,IAAI;IACH,IAAK,CAAEA,IAAI,EAAG;MACb,MAAMC,GAAG,GAAG,MAAMP,MAAM,CAACQ,KAAK,CAAEX,GAAI,CAAC;MACrC,IAAKU,GAAG,CAACE,MAAM,KAAK,GAAG,EAAG;QACzB,OAAO,KAAK;MACb;MACAH,IAAI,GAAG,MAAM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MAAM;MACvD;IACD;EACD;EAEA,MAAMiC,OAAO,GAAGnC,eAAe,CAAE0B,UAAU,CAAE;EAE7C,MAAM,CAAE,iBAAkB,CAAC,CAACU,IAAI,CAC/B,CAAE;IAAEC;EAAM,CAAC,KAAMA,KAAK,CAAEF,OAAQ,CAAC;EACjC;EACA,MAAM,CAAC,CACR,CAAC;AACF;;AAEA;AACA,IAAKnF,UAAU,CAACC,mBAAmB,EAAG;EACrC,IAAK9B,cAAc,KAAK,UAAU,EAAG;IACpC;IACAgC,QAAQ,CAACkB,gBAAgB,CACxB,OAAO,EACP,UAAWkB,KAAK,EAAG;MAClB,MAAMP,GAAG,GAAKO,KAAK,CAACL,MAAM,CAAcoD,OAAO,CAAE,GAAI,CAAC;MACtD,IAAKvD,WAAW,CAAEC,GAAI,CAAC,IAAIM,YAAY,CAAEC,KAAM,CAAC,EAAG;QAClDA,KAAK,CAACgD,cAAc,CAAC,CAAC;QACtBpC,OAAO,CAACI,QAAQ,CAAEvB,GAAG,CAACnD,IAAK,CAAC;MAC7B;IACD,CAAC,EACD,IACD,CAAC;IACD;IACAsB,QAAQ,CAACkB,gBAAgB,CACxB,YAAY,EACZ,UAAWkB,KAAK,EAAG;MAClB,IAAOA,KAAK,CAACL,MAAM,EAAesD,QAAQ,KAAK,GAAG,EAAG;QACpD,MAAMxD,GAAG,GAAKO,KAAK,CAACL,MAAM,CAAcoD,OAAO,CAAE,GAAI,CAAC;QACtD,IAAKvD,WAAW,CAAEC,GAAI,CAAC,IAAIM,YAAY,CAAEC,KAAM,CAAC,EAAG;UAClDY,OAAO,CAACU,QAAQ,CAAE7B,GAAG,CAACnD,IAAK,CAAC;QAC7B;MACD;IACD,CAAC,EACD,IACD,CAAC;EACF;AACD","ignoreList":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cache of prefetched stylesheets and scripts.
|
|
3
|
+
*/
|
|
4
|
+
export declare const headElements: Map<string, {
|
|
5
|
+
tag: HTMLElement;
|
|
6
|
+
text?: string;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* Helper to update only the necessary tags in the head.
|
|
10
|
+
*
|
|
11
|
+
* @async
|
|
12
|
+
* @param newHead The head elements of the new page.
|
|
13
|
+
*/
|
|
14
|
+
export declare const updateHead: (newHead: HTMLHeadElement[]) => Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Fetches and processes head assets (stylesheets and scripts) from a specified document.
|
|
17
|
+
*
|
|
18
|
+
* @async
|
|
19
|
+
* @param doc The document from which to fetch head assets. It should support standard DOM querying methods.
|
|
20
|
+
*
|
|
21
|
+
* @return Returns an array of HTML elements representing the head assets.
|
|
22
|
+
*/
|
|
23
|
+
export declare const fetchHeadAssets: (doc: Document) => Promise<HTMLElement[]>;
|
|
24
|
+
//# sourceMappingURL=head.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"head.d.ts","sourceRoot":"","sources":["../src/head.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,YAAY;SAEjB,WAAW;WAAS,MAAM;EAC/B,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,UAAU,YAAoB,eAAe,EAAE,kBAuC3D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,QACtB,QAAQ,KACX,OAAO,CAAE,WAAW,EAAE,CA4DxB,CAAC"}
|
package/build-types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA,UAAU,eAAe;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,UAAU,eAAe;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAuBA,UAAU,eAAe;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACnC;AAED,UAAU,eAAe;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACd;AAwMD,eAAO,MAAQ,KAAK;SAZb,MAAM;;oBAEE,OAAO;qBACN,OAAO;;GASD,OAAO;cALjB,CAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,KAAM,IAAI;cACnD,CAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,KAAM,IAAI;CAgJ3D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/interactivity-router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.19.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",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"wpScriptModuleExports": "./build-module/index.js",
|
|
29
29
|
"types": "build-types",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@wordpress/a11y": "^4.
|
|
32
|
-
"@wordpress/interactivity": "^6.
|
|
31
|
+
"@wordpress/a11y": "^4.19.0",
|
|
32
|
+
"@wordpress/interactivity": "^6.19.0"
|
|
33
33
|
},
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "d6b0b20fa927b110140dc7fdd906a7e0bf662004"
|
|
38
38
|
}
|
package/src/head.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cache of prefetched stylesheets and scripts.
|
|
3
|
+
*/
|
|
4
|
+
export const headElements = new Map<
|
|
5
|
+
string,
|
|
6
|
+
{ tag: HTMLElement; text?: string }
|
|
7
|
+
>();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Helper to update only the necessary tags in the head.
|
|
11
|
+
*
|
|
12
|
+
* @async
|
|
13
|
+
* @param newHead The head elements of the new page.
|
|
14
|
+
*/
|
|
15
|
+
export const updateHead = async ( newHead: HTMLHeadElement[] ) => {
|
|
16
|
+
// Helper to get the tag id store in the cache.
|
|
17
|
+
const getTagId = ( tag: Element ) => tag.id || tag.outerHTML;
|
|
18
|
+
|
|
19
|
+
// Map incoming head tags by their content.
|
|
20
|
+
const newHeadMap = new Map< string, Element >();
|
|
21
|
+
for ( const child of newHead ) {
|
|
22
|
+
newHeadMap.set( getTagId( child ), child );
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const toRemove: Element[] = [];
|
|
26
|
+
|
|
27
|
+
// Detect nodes that should be added or removed.
|
|
28
|
+
for ( const child of document.head.children ) {
|
|
29
|
+
const id = getTagId( child );
|
|
30
|
+
// Always remove styles and links as they might change.
|
|
31
|
+
if ( child.nodeName === 'LINK' || child.nodeName === 'STYLE' ) {
|
|
32
|
+
toRemove.push( child );
|
|
33
|
+
} else if ( newHeadMap.has( id ) ) {
|
|
34
|
+
newHeadMap.delete( id );
|
|
35
|
+
} else if ( child.nodeName !== 'SCRIPT' && child.nodeName !== 'META' ) {
|
|
36
|
+
toRemove.push( child );
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await Promise.all(
|
|
41
|
+
[ ...headElements.entries() ]
|
|
42
|
+
.filter( ( [ , { tag } ] ) => tag.nodeName === 'SCRIPT' )
|
|
43
|
+
.map( async ( [ url ] ) => {
|
|
44
|
+
await import( /* webpackIgnore: true */ url );
|
|
45
|
+
} )
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Prepare new assets.
|
|
49
|
+
const toAppend = [ ...newHeadMap.values() ];
|
|
50
|
+
|
|
51
|
+
// Apply the changes.
|
|
52
|
+
toRemove.forEach( ( n ) => n.remove() );
|
|
53
|
+
document.head.append( ...toAppend );
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Fetches and processes head assets (stylesheets and scripts) from a specified document.
|
|
58
|
+
*
|
|
59
|
+
* @async
|
|
60
|
+
* @param doc The document from which to fetch head assets. It should support standard DOM querying methods.
|
|
61
|
+
*
|
|
62
|
+
* @return Returns an array of HTML elements representing the head assets.
|
|
63
|
+
*/
|
|
64
|
+
export const fetchHeadAssets = async (
|
|
65
|
+
doc: Document
|
|
66
|
+
): Promise< HTMLElement[] > => {
|
|
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< HTMLScriptElement >(
|
|
73
|
+
'script[type="module"][src]'
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
scripts.forEach( ( script ) => {
|
|
77
|
+
const src = script.getAttribute( 'src' );
|
|
78
|
+
if ( ! headElements.has( src ) ) {
|
|
79
|
+
// add the <link> elements to prefetch the module scripts
|
|
80
|
+
const link = doc.createElement( 'link' );
|
|
81
|
+
link.rel = 'modulepreload';
|
|
82
|
+
link.href = src;
|
|
83
|
+
document.head.append( link );
|
|
84
|
+
headElements.set( src, { tag: script } );
|
|
85
|
+
}
|
|
86
|
+
} );
|
|
87
|
+
|
|
88
|
+
const stylesheets = doc.querySelectorAll< HTMLLinkElement >(
|
|
89
|
+
'link[rel=stylesheet]'
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
await Promise.all(
|
|
93
|
+
Array.from( stylesheets ).map( async ( tag ) => {
|
|
94
|
+
const href = tag.getAttribute( 'href' );
|
|
95
|
+
if ( ! href ) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if ( ! headElements.has( href ) ) {
|
|
100
|
+
try {
|
|
101
|
+
const response = await fetch( href );
|
|
102
|
+
const text = await response.text();
|
|
103
|
+
headElements.set( href, {
|
|
104
|
+
tag,
|
|
105
|
+
text,
|
|
106
|
+
} );
|
|
107
|
+
} catch ( e ) {
|
|
108
|
+
// eslint-disable-next-line no-console
|
|
109
|
+
console.error( e );
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const headElement = headElements.get( href );
|
|
114
|
+
const styleElement = doc.createElement( 'style' );
|
|
115
|
+
styleElement.textContent = headElement.text;
|
|
116
|
+
|
|
117
|
+
headTags.push( styleElement );
|
|
118
|
+
} )
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return [
|
|
122
|
+
doc.querySelector( 'title' ),
|
|
123
|
+
...doc.querySelectorAll( 'style' ),
|
|
124
|
+
...headTags,
|
|
125
|
+
];
|
|
126
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { store, privateApis, getConfig } from '@wordpress/interactivity';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
9
|
+
import { fetchHeadAssets, updateHead, headElements } from './head';
|
|
10
10
|
|
|
11
11
|
const {
|
|
12
12
|
directivePrefix,
|
|
@@ -37,18 +37,16 @@ interface PrefetchOptions {
|
|
|
37
37
|
|
|
38
38
|
interface VdomParams {
|
|
39
39
|
vdom?: typeof initialVdom;
|
|
40
|
-
baseUrl?: string;
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
interface Page {
|
|
44
43
|
regions: Record< string, any >;
|
|
45
|
-
|
|
46
|
-
scriptModules: string[];
|
|
44
|
+
head: HTMLHeadElement[];
|
|
47
45
|
title: string;
|
|
48
46
|
initialData: any;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
type RegionsToVdom = ( dom: Document, params?: VdomParams ) => Page
|
|
49
|
+
type RegionsToVdom = ( dom: Document, params?: VdomParams ) => Promise< Page >;
|
|
52
50
|
|
|
53
51
|
// Check if the navigation mode is full page or region based.
|
|
54
52
|
const navigationMode: 'regionBased' | 'fullPage' =
|
|
@@ -75,7 +73,7 @@ const fetchPage = async ( url: string, { html }: { html: string } ) => {
|
|
|
75
73
|
html = await res.text();
|
|
76
74
|
}
|
|
77
75
|
const dom = new window.DOMParser().parseFromString( html, 'text/html' );
|
|
78
|
-
return regionsToVdom( dom
|
|
76
|
+
return regionsToVdom( dom );
|
|
79
77
|
} catch ( e ) {
|
|
80
78
|
return false;
|
|
81
79
|
}
|
|
@@ -83,17 +81,12 @@ const fetchPage = async ( url: string, { html }: { html: string } ) => {
|
|
|
83
81
|
|
|
84
82
|
// Return an object with VDOM trees of those HTML regions marked with a
|
|
85
83
|
// `router-region` directive.
|
|
86
|
-
const regionsToVdom: RegionsToVdom = ( dom, { vdom
|
|
84
|
+
const regionsToVdom: RegionsToVdom = async ( dom, { vdom } = {} ) => {
|
|
87
85
|
const regions = { body: undefined };
|
|
88
|
-
|
|
89
|
-
const scriptModules = [
|
|
90
|
-
...dom.querySelectorAll< HTMLScriptElement >(
|
|
91
|
-
'script[type=module][src]'
|
|
92
|
-
),
|
|
93
|
-
].map( ( s ) => s.src );
|
|
94
|
-
|
|
86
|
+
let head: HTMLElement[];
|
|
95
87
|
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
|
|
96
88
|
if ( navigationMode === 'fullPage' ) {
|
|
89
|
+
head = await fetchHeadAssets( dom );
|
|
97
90
|
regions.body = vdom
|
|
98
91
|
? vdom.get( document.body )
|
|
99
92
|
: toVdom( dom.body );
|
|
@@ -110,28 +103,15 @@ const regionsToVdom: RegionsToVdom = ( dom, { vdom, baseUrl } = {} ) => {
|
|
|
110
103
|
}
|
|
111
104
|
const title = dom.querySelector( 'title' )?.innerText;
|
|
112
105
|
const initialData = parseServerData( dom );
|
|
113
|
-
return { regions,
|
|
106
|
+
return { regions, head, title, initialData };
|
|
114
107
|
};
|
|
115
108
|
|
|
116
109
|
// Render all interactive regions contained in the given page.
|
|
117
110
|
const renderRegions = async ( page: Page ) => {
|
|
118
|
-
// Wait for styles and modules to be ready.
|
|
119
|
-
await Promise.all( [
|
|
120
|
-
...page.styles,
|
|
121
|
-
...page.scriptModules.map(
|
|
122
|
-
( src ) => import( /* webpackIgnore: true */ src )
|
|
123
|
-
),
|
|
124
|
-
] );
|
|
125
|
-
// Replace style sheets.
|
|
126
|
-
const sheets = await Promise.all( page.styles );
|
|
127
|
-
window.document
|
|
128
|
-
.querySelectorAll( 'style,link[rel=stylesheet]' )
|
|
129
|
-
.forEach( ( element ) => element.remove() );
|
|
130
|
-
window.document.adoptedStyleSheets = sheets;
|
|
131
|
-
|
|
132
111
|
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
|
|
133
112
|
if ( navigationMode === 'fullPage' ) {
|
|
134
|
-
//
|
|
113
|
+
// Once this code is tested and more mature, the head should be updated for region based navigation as well.
|
|
114
|
+
await updateHead( page.head );
|
|
135
115
|
const fragment = getRegionRootFragment( document.body );
|
|
136
116
|
batch( () => {
|
|
137
117
|
populateServerData( page.initialData );
|
|
@@ -189,14 +169,23 @@ window.addEventListener( 'popstate', async () => {
|
|
|
189
169
|
// Initialize the router and cache the initial page using the initial vDOM.
|
|
190
170
|
// Once this code is tested and more mature, the head should be updated for
|
|
191
171
|
// region based navigation as well.
|
|
172
|
+
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
|
|
173
|
+
if ( navigationMode === 'fullPage' ) {
|
|
174
|
+
// Cache the scripts. Has to be called before fetching the assets.
|
|
175
|
+
[].map.call(
|
|
176
|
+
document.querySelectorAll( 'script[type="module"][src]' ),
|
|
177
|
+
( script ) => {
|
|
178
|
+
headElements.set( script.getAttribute( 'src' ), {
|
|
179
|
+
tag: script,
|
|
180
|
+
} );
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
await fetchHeadAssets( document );
|
|
184
|
+
}
|
|
185
|
+
}
|
|
192
186
|
pages.set(
|
|
193
187
|
getPagePath( window.location.href ),
|
|
194
|
-
Promise.resolve(
|
|
195
|
-
regionsToVdom( document, {
|
|
196
|
-
vdom: initialVdom,
|
|
197
|
-
baseUrl: window.location.href,
|
|
198
|
-
} )
|
|
199
|
-
)
|
|
188
|
+
Promise.resolve( regionsToVdom( document, { vdom: initialVdom } ) )
|
|
200
189
|
);
|
|
201
190
|
|
|
202
191
|
// Check if the link is valid for client-side navigation.
|
|
@@ -234,7 +223,6 @@ interface Store {
|
|
|
234
223
|
state: {
|
|
235
224
|
url: string;
|
|
236
225
|
navigation: {
|
|
237
|
-
isLoading: boolean;
|
|
238
226
|
hasStarted: boolean;
|
|
239
227
|
hasFinished: boolean;
|
|
240
228
|
};
|
|
@@ -249,7 +237,6 @@ export const { state, actions } = store< Store >( 'core/router', {
|
|
|
249
237
|
state: {
|
|
250
238
|
url: window.location.href,
|
|
251
239
|
navigation: {
|
|
252
|
-
isLoading: false,
|
|
253
240
|
hasStarted: false,
|
|
254
241
|
hasFinished: false,
|
|
255
242
|
},
|
|
@@ -302,7 +289,6 @@ export const { state, actions } = store< Store >( 'core/router', {
|
|
|
302
289
|
return;
|
|
303
290
|
}
|
|
304
291
|
|
|
305
|
-
navigation.isLoading = true;
|
|
306
292
|
if ( loadingAnimation ) {
|
|
307
293
|
navigation.hasStarted = true;
|
|
308
294
|
navigation.hasFinished = false;
|
|
@@ -342,7 +328,6 @@ export const { state, actions } = store< Store >( 'core/router', {
|
|
|
342
328
|
|
|
343
329
|
// Update the navigation status once the the new page rendering
|
|
344
330
|
// has been completed.
|
|
345
|
-
navigation.isLoading = false;
|
|
346
331
|
if ( loadingAnimation ) {
|
|
347
332
|
navigation.hasStarted = false;
|
|
348
333
|
navigation.hasFinished = true;
|