Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable service worker #757

Merged
merged 3 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions pkg/app/gen/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,36 @@ func main() {
fmt.Fprintln(f)

gen := []struct {
Var string
Filename string
Var string
Filename string
Documentation string
}{
{Var: "wasmExecJS", Filename: filepath.Join(
runtime.GOROOT(),
"misc",
"wasm",
"wasm_exec.js",
)},
{Var: "appJS", Filename: "gen/app.js"},
{Var: "appWorkerJS", Filename: "gen/app-worker.js"},
{Var: "manifestJSON", Filename: "gen/manifest.webmanifest"},
{Var: "appCSS", Filename: "gen/app.css"},
{
Var: "DefaultAppWorkerJS",
Filename: "gen/app-worker.js",
Documentation: "The default template used to generate app-worker.js.",
},
{
Var: "wasmExecJS",
Filename: filepath.Join(
runtime.GOROOT(),
"misc",
"wasm",
"wasm_exec.js",
),
},
{
Var: "appJS",
Filename: "gen/app.js",
},
{
Var: "manifestJSON",
Filename: "gen/manifest.webmanifest",
},
{
Var: "appCSS",
Filename: "gen/app.css",
},
}

fmt.Fprintln(f, "const(")
Expand All @@ -49,6 +66,11 @@ func main() {
panic(err)
}

if g.Documentation != "" {
fmt.Fprintln(f, "//", g.Documentation)

}

fmt.Fprintf(f, "%s = %q", g.Var, b)
fmt.Fprintln(f)
fmt.Fprintln(f)
Expand Down
17 changes: 16 additions & 1 deletion pkg/app/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ type Handler struct {
// no content length is found with the defined header.
WasmContentLengthHeader string

// The template used to generate app-worker.js. The template follows the
// text/template package model.
//
// By default set to DefaultAppWorkerJS, changing the template have very
// high chances to mess up go-app usage. Any issue related to a custom app
// worker template is not supported and will be closed.
ServiceWorkerTemplate string

once sync.Once
etag string
pwaResources PreRenderCache
Expand All @@ -191,6 +199,7 @@ func (h *Handler) init() {
h.initImage()
h.initStyles()
h.initScripts()
h.initServiceWorker()
h.initCacheableResources()
h.initIcon()
h.initPWA()
Expand Down Expand Up @@ -231,6 +240,12 @@ func (h *Handler) initScripts() {
}
}

func (h *Handler) initServiceWorker() {
if h.ServiceWorkerTemplate == "" {
h.ServiceWorkerTemplate = DefaultAppWorkerJS
}
}

func (h *Handler) initCacheableResources() {
for i, path := range h.CacheableResources {
h.CacheableResources[i] = h.resolveStaticPath(path)
Expand Down Expand Up @@ -404,7 +419,7 @@ func (h *Handler) makeAppWorkerJS() []byte {

var b bytes.Buffer
if err := template.
Must(template.New("app-worker.js").Parse(appWorkerJS)).
Must(template.New("app-worker.js").Parse(h.ServiceWorkerTemplate)).
Execute(&b, struct {
Version string
ResourcesToCache string
Expand Down
Loading