Skip to content

Commit

Permalink
Fixed race condition in instant loading
Browse files Browse the repository at this point in the history
  • Loading branch information
squidfunk committed Mar 6, 2020
1 parent 21e9396 commit 80f1d3e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/assets/javascripts/browser/document/switch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { ajax } from "rxjs/ajax"
import {
catchError,
distinctUntilKeyChanged,
pluck,
map,
share,
skip,
switchMap
Expand Down Expand Up @@ -56,7 +56,10 @@ interface WatchOptions {
* to the same page, the request is effectively ignored (i.e. when only the
* fragment identifier changes).
*
* In case the request fails, the location change is dispatched regularly.
* Theoretically, we could use `responseType: "document"`, but since all MkDocs
* links are relative, we need to make sure that the current location matches
* the document we just loaded. Otherwise any relative links in the document
* may use the old location.
*
* @param options - Options
*
Expand All @@ -65,6 +68,7 @@ interface WatchOptions {
export function watchDocumentSwitch(
{ location$ }: WatchOptions
): Observable<Document> {
const dom = new DOMParser()
return location$
.pipe(
distinctUntilKeyChanged("pathname"),
Expand All @@ -73,11 +77,14 @@ export function watchDocumentSwitch(
/* Fetch document */
switchMap(url => ajax({
url: url.href,
responseType: "document",
responseType: "text",
withCredentials: true
})
.pipe<Document, Document>(
pluck("response"),
.pipe(
map(({ response }): Document => {
history.pushState({}, "", url.toString()) // TODO: abstract into function
return dom.parseFromString(response, "text/html")
}),
catchError(() => {
setLocation(url)
return NEVER
Expand Down
5 changes: 0 additions & 5 deletions src/assets/javascripts/integrations/instant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ export function setupInstantLoading(
)
.subscribe(location$)

/* History: dispatch internal link */
push$.subscribe(({ url }) => {
history.pushState({}, "", url.toString())
})

/* History: debounce update of viewport offset */
viewport$
.pipe(
Expand Down

0 comments on commit 80f1d3e

Please sign in to comment.