Readme

Add a pwa manifest to an http val (to open in as an app in iOS).

See https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps for available fields.

Usage

import handler from "https://esm.town/v/maxm/staticChess"; import { pwa } from "https://esm.town/v/pomdtr/pwa"; export default pwa(handler, { name: "Static Chess", display: "standalone", background_color: "#ffffff", start_url: "/", });
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { HTMLRewriter } from "https://ghuc.cc/worker-tools/html-rewriter/index.ts";
const rewriter = new HTMLRewriter()
.on("head", {
element(element: any) {
element.append(`<link rel="manifest" href="/manifest.json">`, {
html: true,
});
},
});
export type Handler = (req: Request) => Response | Promise<Response>;
export function pwa(handler: Handler, manifest: Partial<ManifestOptions>): Handler {
return async (req: Request) => {
const url = new URL(req.url);
if (url.pathname === "/manifest.json") {
return new Response(JSON.stringify(manifest), {
headers: {
"Content-Type": "application/json",
},
});
}
const res = await handler(req);
return url.pathname == "/" ? rewriter.transform(res) : res;
};
}
export interface ManifestOptions {
/**
* @default _npm_package_name_
*/
name: string
/**
* @default _npm_package_name_
*/
short_name: string
/**
* @default _npm_package_description_
*/
description: string
/**
* @default []
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/icons
* @see https://w3c.github.io/manifest/#icons-member
*/
icons: IconResource[]
/**
* @default []
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/file_handlers
* @see https://wicg.github.io/manifest-incubations/#file_handlers-member
*/
file_handlers: {
action: string
accept: Record<string, string[]>
}[]
/**
* @default `routerBase`
*/
start_url: string
/**
* Restricts what web pages can be viewed while the manifest is applied
*/
scope: string
/**
* A string that represents the identity for the application
*/
id: string
/**
* Defines the default orientation for all the website's top-level
*/
orientation: 'any' | 'natural' | 'landscape' | 'landscape-primary' | 'landscape-secondary' | 'portrait' | 'portrait-primary' | 'portrait-secondary'
/**
* @default `standalone`
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/display
* @see https://w3c.github.io/manifest/#display-member
*/
display: Display
/**
* @default []
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/display_override
* @see https://wicg.github.io/manifest-incubations/#display_override-member
*/
display_override: DisplayOverride[]
/**
* @default `#ffffff`
*/
background_color: string
/**
* @default '#42b883
*/
theme_color: string
/**
* @default `ltr`
*/
dir: 'ltr' | 'rtl'
/**
* @default `en`
*/
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v21
June 17, 2024