parent
d16ccd5d5f
commit
c83c12e617
|
@ -1,4 +0,0 @@
|
||||||
import { thunk } from '../thunk'
|
|
||||||
|
|
||||||
export const isSafari = thunk(() => process.browser && /Safari/.test(navigator.userAgent) &&
|
|
||||||
!/Chrome/.test(navigator.userAgent))
|
|
|
@ -4,7 +4,6 @@ import {
|
||||||
routes as __routes__
|
routes as __routes__
|
||||||
} from '../__sapper__/service-worker.js'
|
} from '../__sapper__/service-worker.js'
|
||||||
import { get, post } from './routes/_utils/ajax'
|
import { get, post } from './routes/_utils/ajax'
|
||||||
import { isSafari } from './routes/_utils/userAgent/isSafari'
|
|
||||||
|
|
||||||
const timestamp = process.env.SAPPER_TIMESTAMP
|
const timestamp = process.env.SAPPER_TIMESTAMP
|
||||||
const ASSETS = `assets_${timestamp}`
|
const ASSETS = `assets_${timestamp}`
|
||||||
|
@ -86,30 +85,6 @@ self.addEventListener('activate', event => {
|
||||||
})())
|
})())
|
||||||
})
|
})
|
||||||
|
|
||||||
async function returnRangeRequest (request) {
|
|
||||||
const response = await fetch(request, { headers: {}, mode: 'cors', credentials: 'omit' })
|
|
||||||
const arrayBuffer = await response.arrayBuffer()
|
|
||||||
const bytes = /^bytes=(\d+)-(\d+)?$/g.exec(request.headers.get('range'))
|
|
||||||
if (bytes) {
|
|
||||||
const start = parseInt(bytes[1], 10)
|
|
||||||
const end = parseInt(bytes[2], 10) || arrayBuffer.byteLength - 1
|
|
||||||
|
|
||||||
return new Response(arrayBuffer.slice(start, end + 1), {
|
|
||||||
status: 206,
|
|
||||||
statusText: 'Partial Content',
|
|
||||||
headers: [
|
|
||||||
['Content-Range', `bytes ${start}-${end}/${arrayBuffer.byteLength}`]
|
|
||||||
]
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return new Response(null, {
|
|
||||||
status: 416,
|
|
||||||
statusText: 'Range Not Satisfiable',
|
|
||||||
headers: [['Content-Range', `*/${arrayBuffer.byteLength}`]]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.addEventListener('fetch', event => {
|
self.addEventListener('fetch', event => {
|
||||||
const req = event.request
|
const req = event.request
|
||||||
const url = new URL(req.url)
|
const url = new URL(req.url)
|
||||||
|
@ -154,14 +129,6 @@ self.addEventListener('fetch', event => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// for everything else, go network-only
|
// for everything else, go network-only
|
||||||
|
|
||||||
// range request need to be be patched with a 206 response to satisfy
|
|
||||||
// Safari (https://stackoverflow.com/questions/52087208)
|
|
||||||
// Once this bug is fixed in WebKit we can remove this https://bugs.webkit.org/show_bug.cgi?id=186050
|
|
||||||
if (isSafari() && event.request.headers.get('range')) {
|
|
||||||
return returnRangeRequest(req)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fetch(req)
|
return fetch(req)
|
||||||
})())
|
})())
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue