Removed deprecated XMLHttpRequest
This commit is contained in:
parent
4856d789a5
commit
63bd2a96fa
@ -13,7 +13,6 @@ export class Application {
|
|||||||
loading: HTMLElement
|
loading: HTMLElement
|
||||||
currentPath: string
|
currentPath: string
|
||||||
originalPath: string
|
originalPath: string
|
||||||
lastRequest: XMLHttpRequest | null
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.currentPath = window.location.pathname
|
this.currentPath = window.location.pathname
|
||||||
@ -35,28 +34,9 @@ export class Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(url: string): Promise<string> {
|
get(url: string): Promise<string> {
|
||||||
if(this.lastRequest) {
|
return fetch(url, {
|
||||||
this.lastRequest.abort()
|
credentials: "same-origin"
|
||||||
this.lastRequest = null
|
}).then(response => response.text())
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
let request = new XMLHttpRequest()
|
|
||||||
|
|
||||||
request.onerror = () => reject(new Error("You are either offline or the requested page doesn't exist."))
|
|
||||||
request.ontimeout = () => reject(new Error("The page took too much time to respond."))
|
|
||||||
request.onload = () => {
|
|
||||||
if(request.status < 200 || request.status >= 400)
|
|
||||||
reject(request.responseText)
|
|
||||||
else
|
|
||||||
resolve(request.responseText)
|
|
||||||
}
|
|
||||||
|
|
||||||
request.open("GET", url, true)
|
|
||||||
request.send()
|
|
||||||
|
|
||||||
this.lastRequest = request
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
load(url: string, options?: LoadOptions) {
|
load(url: string, options?: LoadOptions) {
|
||||||
@ -87,6 +67,11 @@ export class Application {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Outdated response.
|
||||||
|
if(this.currentPath !== url) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Remove listener after we finally got the correct event.
|
// Remove listener after we finally got the correct event.
|
||||||
this.content.removeEventListener("transitionend", onTransitionEnd)
|
this.content.removeEventListener("transitionend", onTransitionEnd)
|
||||||
|
|
||||||
|
@ -13,9 +13,17 @@
|
|||||||
// If the style or script resources changed after being served, we need
|
// If the style or script resources changed after being served, we need
|
||||||
// to force a real page reload.
|
// to force a real page reload.
|
||||||
|
|
||||||
|
// Promises
|
||||||
const RELOADS = new Map<string, Promise<Response>>()
|
const RELOADS = new Map<string, Promise<Response>>()
|
||||||
const ETAGS = new Map<string, string>()
|
|
||||||
const CACHEREFRESH = new Map<string, Promise<void>>()
|
const CACHEREFRESH = new Map<string, Promise<void>>()
|
||||||
|
|
||||||
|
// E-Tags that we served for a given URL
|
||||||
|
const ETAGS = new Map<string, string>()
|
||||||
|
|
||||||
|
// When these patterns are matched for the request URL, we exclude them from being
|
||||||
|
// served cache-first and instead serve them via a network request.
|
||||||
|
// Note that the service worker URL is automatically excluded from fetch events
|
||||||
|
// and therefore doesn't need to be added here.
|
||||||
const EXCLUDECACHE = new Set<string>([
|
const EXCLUDECACHE = new Set<string>([
|
||||||
// API requests
|
// API requests
|
||||||
"/api/",
|
"/api/",
|
||||||
@ -98,6 +106,8 @@ class MyServiceWorker {
|
|||||||
onRequest(evt: FetchEvent) {
|
onRequest(evt: FetchEvent) {
|
||||||
let request = evt.request as Request
|
let request = evt.request as Request
|
||||||
|
|
||||||
|
console.log("fetch", request.url)
|
||||||
|
|
||||||
// If it's not a GET request, fetch it normally
|
// If it's not a GET request, fetch it normally
|
||||||
if(request.method !== "GET") {
|
if(request.method !== "GET") {
|
||||||
return evt.respondWith(fetch(request))
|
return evt.respondWith(fetch(request))
|
||||||
@ -130,6 +140,10 @@ class MyServiceWorker {
|
|||||||
|
|
||||||
CACHEREFRESH.set(request.url, cacheRefresh)
|
CACHEREFRESH.set(request.url, cacheRefresh)
|
||||||
|
|
||||||
|
if(request.url === "/styles") {
|
||||||
|
console.log("/styles fetched", response.headers.get("ETag"))
|
||||||
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -142,6 +156,11 @@ class MyServiceWorker {
|
|||||||
let onResponse = response => {
|
let onResponse = response => {
|
||||||
servedETag = response.headers.get("ETag")
|
servedETag = response.headers.get("ETag")
|
||||||
ETAGS.set(request.url, servedETag)
|
ETAGS.set(request.url, servedETag)
|
||||||
|
|
||||||
|
if(request.url === "/styles") {
|
||||||
|
console.log("/styles served", servedETag)
|
||||||
|
}
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user