mirror of
https://github.com/nlepage/go-wasm-http-server.git
synced 2025-03-10 17:29:10 +00:00
feat: allow using cache for wasm binary
This commit is contained in:
parent
b2bd8679fd
commit
98257b470a
@ -1,7 +1,7 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>go-wasm-http-server hello with state demo</title>
|
<title>go-wasm-http-server hello with state and keepalive demo</title>
|
||||||
<script>
|
<script>
|
||||||
navigator.serviceWorker.register('sw.js').then(registration => {
|
navigator.serviceWorker.register('sw.js').then(registration => {
|
||||||
const sw = registration.active ?? registration.installing ?? registration.waiting
|
const sw = registration.active ?? registration.installing ?? registration.waiting
|
||||||
|
@ -2,7 +2,7 @@ importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exe
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||||
|
|
||||||
addEventListener('install', event => {
|
addEventListener('install', event => {
|
||||||
event.waitUntil(skipWaiting())
|
event.waitUntil(caches.open('hello-state').then((cache) => cache.add('api.wasm')))
|
||||||
})
|
})
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
addEventListener('activate', event => {
|
||||||
|
@ -2,10 +2,11 @@ importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exe
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||||
|
|
||||||
addEventListener('install', (event) => {
|
addEventListener('install', (event) => {
|
||||||
|
event.waitUntil(caches.open('hello-state').then((cache) => cache.add('api.wasm')))
|
||||||
event.waitUntil(skipWaiting())
|
event.waitUntil(skipWaiting())
|
||||||
})
|
})
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
addEventListener('activate', (event) => {
|
||||||
event.waitUntil(clients.claim())
|
event.waitUntil(clients.claim())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -2,10 +2,10 @@ importScripts('https://cdn.jsdelivr.net/gh/golang/go@go1.23.1/misc/wasm/wasm_exe
|
|||||||
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
importScripts('https://cdn.jsdelivr.net/gh/nlepage/go-wasm-http-server@v2.0.2/sw.js')
|
||||||
|
|
||||||
addEventListener('install', (event) => {
|
addEventListener('install', (event) => {
|
||||||
event.waitUntil(skipWaiting())
|
event.waitUntil(caches.open('hello').then((cache) => cache.add('api.wasm')))
|
||||||
})
|
})
|
||||||
|
|
||||||
addEventListener('activate', event => {
|
addEventListener('activate', (event) => {
|
||||||
event.waitUntil(clients.claim())
|
event.waitUntil(clients.claim())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
7
sw.js
7
sw.js
@ -1,4 +1,4 @@
|
|||||||
function registerWasmHTTPListener(wasm, { base, args = [] } = {}) {
|
function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) {
|
||||||
let path = new URL(registration.scope).pathname
|
let path = new URL(registration.scope).pathname
|
||||||
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
|
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
|
||||||
|
|
||||||
@ -11,7 +11,10 @@ function registerWasmHTTPListener(wasm, { base, args = [] } = {}) {
|
|||||||
|
|
||||||
const go = new Go()
|
const go = new Go()
|
||||||
go.argv = [wasm, ...args]
|
go.argv = [wasm, ...args]
|
||||||
WebAssembly.instantiateStreaming(fetch(wasm), go.importObject).then(({ instance }) => go.run(instance))
|
const source = cacheName
|
||||||
|
? caches.open(cacheName).then((cache) => cache.match(wasm)).then((response) => response ?? fetch(wasm))
|
||||||
|
: caches.match(wasm).then(response => (response) ?? fetch(wasm))
|
||||||
|
WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance))
|
||||||
|
|
||||||
addEventListener('fetch', e => {
|
addEventListener('fetch', e => {
|
||||||
const { pathname } = new URL(e.request.url)
|
const { pathname } = new URL(e.request.url)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user