Skip to content

Commit 943e978

Browse files
authored
Merge pull request #6662 from squidfunk/fix/instant-loading-bugs
Fixed instant navigation bugs
2 parents be95f49 + 95ad78a commit 943e978

File tree

12 files changed

+445
-397
lines changed

12 files changed

+445
-397
lines changed

material/overrides/assets/javascripts/custom.526c59dc.min.js renamed to material/overrides/assets/javascripts/custom.129bd6ad.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/overrides/assets/javascripts/custom.526c59dc.min.js.map renamed to material/overrides/assets/javascripts/custom.129bd6ad.min.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/overrides/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
{% endblock %}
2424
{% block scripts %}
2525
{{ super() }}
26-
<script src="{{ 'assets/javascripts/custom.526c59dc.min.js' | url }}"></script>
26+
<script src="{{ 'assets/javascripts/custom.129bd6ad.min.js' | url }}"></script>
2727
{% endblock %}

material/templates/assets/javascripts/bundle.a963951d.min.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

material/templates/assets/javascripts/bundle.c18c5fb9.min.js

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/templates/assets/javascripts/bundle.a963951d.min.js.map renamed to material/templates/assets/javascripts/bundle.c18c5fb9.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
</script>
250250
{% endblock %}
251251
{% block scripts %}
252-
<script src="{{ 'assets/javascripts/bundle.a963951d.min.js' | url }}"></script>
252+
<script src="{{ 'assets/javascripts/bundle.c18c5fb9.min.js' | url }}"></script>
253253
{% for script in config.extra_javascript %}
254254
{{ script | script_tag }}
255255
{% endfor %}

src/templates/assets/javascripts/browser/request/index.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,59 +46,72 @@ interface Options {
4646
/**
4747
* Fetch the given URL
4848
*
49-
* If the request fails (e.g. when dispatched from `file://` locations), the
50-
* observable will complete without emitting a value.
49+
* This function returns an observable that emits the response as a blob and
50+
* completes, or emits an error if the request failed. The caller can cancel
51+
* the request by unsubscribing at any time, which will automatically abort
52+
* the inflight request and complete the observable.
53+
*
54+
* Note that we use `XMLHTTPRequest` not because we're nostalgic, but because
55+
* it's the only way to get progress events for downloads and also allow for
56+
* cancellation of requests, as the official Fetch API does not support this
57+
* yet, even though we're already in 2024.
5158
*
5259
* @param url - Request URL
5360
* @param options - Options
5461
*
55-
* @returns Response observable
62+
* @returns Data observable
5663
*/
5764
export function request(
5865
url: URL | string, options?: Options
5966
): Observable<Blob> {
6067
return new Observable<Blob>(observer => {
6168
const req = new XMLHttpRequest()
62-
req.open("GET", `${url}`)
69+
req.open("GET", `${url}`)
6370
req.responseType = "blob"
6471

6572
// Handle response
6673
req.addEventListener("load", () => {
6774
if (req.status >= 200 && req.status < 300) {
6875
observer.next(req.response)
6976
observer.complete()
77+
78+
// Every response that is not in the 2xx range is considered an error
7079
} else {
7180
observer.error(new Error(req.statusText))
7281
}
7382
})
7483

7584
// Handle network errors
7685
req.addEventListener("error", () => {
77-
observer.error(new Error("Network Error"))
86+
observer.error(new Error("Network error"))
7887
})
7988

8089
// Handle aborted requests
8190
req.addEventListener("abort", () => {
82-
observer.error(new Error("Request aborted"))
91+
observer.complete()
8392
})
8493

8594
// Handle download progress
8695
if (typeof options?.progress$ !== "undefined") {
8796
req.addEventListener("progress", event => {
8897
if (event.lengthComputable) {
8998
options.progress$!.next((event.loaded / event.total) * 100)
90-
} else { // https://bugs.chromium.org/p/chromium/issues/detail?id=463622
91-
const totalFromHeader = Number(req.getResponseHeader("Content-Length")) || 0
92-
options.progress$!.next((event.loaded / totalFromHeader) * 100)
99+
100+
// Hack: Chromium doesn't report the total number of bytes if content
101+
// is compressed, so we need this fallback - see https://t.ly/ZXofI
102+
} else {
103+
const length = req.getResponseHeader("Content-Length") ?? 0
104+
options.progress$!.next((event.loaded / +length) * 100)
93105
}
94106
})
95107

96108
// Immediately set progress to 5% to indicate that we're loading
97109
options.progress$.next(5)
98110
}
99111

100-
// Send request
112+
// Send request and automatically abort request upon unsubscription
101113
req.send()
114+
return () => req.abort()
102115
})
103116
}
104117

@@ -125,6 +138,26 @@ export function requestJSON<T>(
125138
)
126139
}
127140

141+
/**
142+
* Fetch HTML from the given URL
143+
*
144+
* @param url - Request URL
145+
* @param options - Options
146+
*
147+
* @returns Data observable
148+
*/
149+
export function requestHTML(
150+
url: URL | string, options?: Options
151+
): Observable<Document> {
152+
const dom = new DOMParser()
153+
return request(url, options)
154+
.pipe(
155+
switchMap(res => res.text()),
156+
map(res => dom.parseFromString(res, "text/html")),
157+
shareReplay(1)
158+
)
159+
}
160+
128161
/**
129162
* Fetch XML from the given URL
130163
*

0 commit comments

Comments
 (0)