feat(web): the app can be installed and opens offline (PWA)
- A web app manifest with the name, the colours, the display mode and the icons (also a maskable one), so a browser offers to install it; Settings has an Install button where the browser lets the page ask. - A service worker keeps the files of the app (the page, its scripts and styles, the manifest and the icons) so it opens at once, and shows its own page when the server cannot be reached. It never touches /api/: nothing from the API is kept, answered from a cache, or asked for by the worker. Files that failed, and anything that is not a file of the app, are not kept; the scripts it keeps are capped. - A new version waits until the page says it may take over: a line says a new version is ready and its button reloads the page after the new worker took over. - The server gives the manifest its type (application/manifest+json); the worker and the manifest are asked again every time, and the policy of the page needs no change.
This commit is contained in:
parent
f86b5a3d7b
commit
2217a12042
2 changed files with 823 additions and 2 deletions
|
|
@ -19,6 +19,11 @@ const appPolicy = "default-src 'self'; script-src 'self'; style-src 'self'; styl
|
|||
"img-src 'self' data: blob:; media-src 'self' blob:; font-src 'self'; connect-src 'self'; " +
|
||||
"object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"
|
||||
|
||||
func init() {
|
||||
// The manifest of the app has no type in the tables of the system, and "text/plain" would stop a browser from installing it.
|
||||
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
|
||||
}
|
||||
|
||||
// Files under assets/ have a hash in their name, so a name never means two different files.
|
||||
const (
|
||||
assetsDir = "assets"
|
||||
|
|
|
|||
|
|
@ -234,3 +234,50 @@ func TestWithNoAppTheRootSaysHowToBuildIt(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the app can be installed ----
|
||||
|
||||
func TestTheManifestAndTheServiceWorkerAreServedWithTheirTypeAndAskedAgain(t *testing.T) {
|
||||
app := theApp()
|
||||
app["manifest.webmanifest"] = &fstest.MapFile{Data: []byte(`{"name":"Sonda"}`)}
|
||||
app["sw.js"] = &fstest.MapFile{Data: []byte("self.addEventListener('fetch', () => {})")}
|
||||
for path, wantType := range map[string]string{
|
||||
"/manifest.webmanifest": "application/manifest+json",
|
||||
"/sw.js": "text/javascript",
|
||||
} {
|
||||
rec := serve(t, app, http.MethodGet, path)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("%s: status %d", path, rec.Code)
|
||||
continue
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, wantType) {
|
||||
t.Errorf("%s: Content-Type %q, want %s", path, ct, wantType)
|
||||
}
|
||||
// A new version of the worker or of the manifest must be seen at once: they are asked again every time.
|
||||
if got := rec.Header().Get("Cache-Control"); got != "no-cache" {
|
||||
t.Errorf("%s: Cache-Control %q, want no-cache", path, got)
|
||||
}
|
||||
if rec.Header().Get("X-Content-Type-Options") != "nosniff" {
|
||||
t.Errorf("%s: no nosniff", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestThePolicyOfTheAppLetsItRunItsOwnWorkerAndNothingElse(t *testing.T) {
|
||||
rec := serve(t, theApp(), http.MethodGet, "/")
|
||||
policy := rec.Header().Get("Content-Security-Policy")
|
||||
// Workers and the manifest fall back to the sources of scripts and of everything else: the server itself.
|
||||
for _, want := range []string{"default-src 'self'", "script-src 'self'", "connect-src 'self'"} {
|
||||
if !strings.Contains(policy, want) {
|
||||
t.Errorf("policy %q lacks %q", policy, want)
|
||||
}
|
||||
}
|
||||
for _, never := range []string{"worker-src *", "worker-src http", "unsafe-eval", "script-src *"} {
|
||||
if strings.Contains(policy, never) {
|
||||
t.Errorf("policy %q has %q", policy, never)
|
||||
}
|
||||
}
|
||||
if strings.Contains(policy, "worker-src") || strings.Contains(policy, "manifest-src") {
|
||||
t.Errorf("policy %q names worker-src or manifest-src: they must follow default-src", policy)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue