mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2026-07-22 13:53:43 -04:00
Added loading of sitemap to filter instant loading requests
This commit is contained in:
@@ -46,7 +46,8 @@ import {
|
||||
take,
|
||||
shareReplay,
|
||||
pluck,
|
||||
catchError
|
||||
catchError,
|
||||
map
|
||||
} from "rxjs/operators"
|
||||
|
||||
import {
|
||||
@@ -352,8 +353,28 @@ export function initialize(config: unknown) {
|
||||
})
|
||||
|
||||
/* Enable instant loading, if not on file:// protocol */
|
||||
if (config.features.includes("instant") && location.protocol !== "file:")
|
||||
setupInstantLoading({ document$, location$, viewport$ })
|
||||
if (config.features.includes("instant") && location.protocol !== "file:") {
|
||||
|
||||
/* Fetch sitemap and extract URL whitelist */
|
||||
base$
|
||||
.pipe(
|
||||
switchMap(base => ajax({
|
||||
url: `${base}/sitemap.xml`,
|
||||
responseType: "document",
|
||||
withCredentials: true
|
||||
})
|
||||
.pipe<Document>(
|
||||
pluck("response")
|
||||
)
|
||||
),
|
||||
map(document => (
|
||||
getElements("loc", document).map(node => node.textContent!)
|
||||
))
|
||||
)
|
||||
.subscribe(urls => {
|
||||
setupInstantLoading(urls, { document$, location$, viewport$ })
|
||||
})
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------- */
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ interface SetupOptions {
|
||||
* @param options - Options
|
||||
*/
|
||||
export function setupInstantLoading(
|
||||
{ document$, viewport$, location$ }: SetupOptions
|
||||
urls: string[], { document$, viewport$, location$ }: SetupOptions
|
||||
): void {
|
||||
|
||||
/* Disable automatic scroll restoration */
|
||||
@@ -125,7 +125,11 @@ export function setupInstantLoading(
|
||||
switchMap(ev => {
|
||||
if (ev.target instanceof HTMLElement) {
|
||||
const el = ev.target.closest("a")
|
||||
if (el && !el.target && isLocalLocation(el)) {
|
||||
if (
|
||||
el && !el.target &&
|
||||
isLocalLocation(el) &&
|
||||
urls.includes(el.href)
|
||||
) {
|
||||
if (!isAnchorLocation(el))
|
||||
ev.preventDefault()
|
||||
return of(el)
|
||||
|
||||
Reference in New Issue
Block a user