forked from the-human-guy/cryptic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
35 lines (31 loc) · 1.31 KB
/
Copy pathservice-worker.js
File metadata and controls
35 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// transpile JSX using babel so it can be run in the browser, you don't need to edit this
// if you do edit this file, call `await window.serviceWorkerRegistration.unregister()` in the browser to update it
let curVersion = 0
self.addEventListener('install', e => {
self.skipWaiting()
e.waitUntil(getBabel())
})
self.addEventListener('fetch', e => e.respondWith(handleRequest(e.request)))
async function getBabel() {
const r = await fetch('https://unpkg.com/@babel/standalone/babel.min.js')
eval(await r.text())
}
async function handleRequest(request) {
if (typeof Babel === 'undefined') {
await getBabel()
}
const url = new URL(request.url)
const r = await fetch(request)
if (r.status === 200 && url.host === location.host && (url.pathname.endsWith('.jsx') || url.pathname.endsWith('.js')) && !url.pathname.startsWith('./lib') && !url.pathname.startsWith('/lib') && !url.pathname.includes('/lib')) {
const parsedVersion = request.url.split('?cryptic-version=')[1] || undefined
if (!!parsedVersion && parsedVersion !== curVersion) {
curVersion = parsedVersion
}
let jsx = await r.text()
jsx = jsx.replaceAll(/(?<=import.*)(.js')/g, `.js?cryptic-version=${curVersion}'`)
const js = Babel.transform(jsx, {presets: ['react']}).code
return new Response(js, r)
} else {
return r
}
}