Skip to content

Commit 963ebac

Browse files
committed
Register a service worker
To make installing as a PWA possible. Fixes coder#1181.
1 parent eef2ed0 commit 963ebac

17 files changed

Lines changed: 140 additions & 78 deletions

File tree

ci/build.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,24 @@ class Builder {
339339
}
340340

341341
private createBundler(out = "dist", commit?: string): Bundler {
342-
return new Bundler(path.join(this.rootPath, "src/browser/pages/app.ts"), {
343-
cache: true,
344-
cacheDir: path.join(this.rootPath, ".cache"),
345-
detailedReport: true,
346-
minify: !!process.env.MINIFY,
347-
hmr: false,
348-
logLevel: 1,
349-
outDir: path.join(this.rootPath, out),
350-
publicUrl: `/static-${commit}/dist`,
351-
target: "browser",
352-
})
342+
return new Bundler(
343+
[
344+
path.join(this.rootPath, "src/browser/pages/app.ts"),
345+
path.join(this.rootPath, "src/browser/register.ts"),
346+
path.join(this.rootPath, "src/browser/serviceWorker.ts"),
347+
],
348+
{
349+
cache: true,
350+
cacheDir: path.join(this.rootPath, ".cache"),
351+
detailedReport: true,
352+
minify: !!process.env.MINIFY,
353+
hmr: false,
354+
logLevel: 1,
355+
outDir: path.join(this.rootPath, out),
356+
publicUrl: `/static-${commit || "development"}/dist`,
357+
target: "browser",
358+
},
359+
)
353360
}
354361
}
355362

src/browser/media/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sizes": "96x96"
1313
},
1414
{
15-
"src": "/{{BASE}}/static-{{COMMIT}}/src/browser/media/pwa-icon-128.png",
15+
"src": "{{BASE}}/static-{{COMMIT}}/src/browser/media/pwa-icon-128.png",
1616
"type": "image/png",
1717
"sizes": "128x128"
1818
},

src/browser/pages/app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<meta id="coder-options" data-settings="{{OPTIONS}}" />
2020
</head>
2121
<body>
22+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
2223
<script src="{{BASE}}/static-{{COMMIT}}/dist/app.js"></script>
2324
</body>
2425
</html>

src/browser/pages/app.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@ import "./login.css"
88
import "./update.css"
99

1010
const options = getOptions()
11-
const parts = window.location.pathname.replace(/^\//g, "").split("/")
12-
parts[parts.length - 1] = options.base
13-
const url = new URL(window.location.origin + "/" + parts.join("/"))
1411

15-
console.log(url)
12+
console.log(options)

src/browser/pages/error.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/>
1717
<link rel="apple-touch-icon" href="{{BASE}}/static-{{COMMIT}}/src/browser/media/code-server.png" />
1818
<link href="{{BASE}}/static-{{COMMIT}}/dist/app.css" rel="stylesheet" />
19+
<meta id="coder-options" data-settings="{{OPTIONS}}" />
1920
</head>
2021
<body>
2122
<div class="center-container">
@@ -29,5 +30,6 @@ <h2 class="header">{{ERROR_HEADER}}</h2>
2930
</div>
3031
</div>
3132
</div>
33+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
3234
</body>
3335
</html>

src/browser/pages/home.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ <h2 class="main">Version</h2>
6060
</div>
6161
</div>
6262
</div>
63+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
6364
</body>
6465
</html>

src/browser/pages/login.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/>
99
<meta
1010
http-equiv="Content-Security-Policy"
11-
content="style-src 'self'; script-src 'unsafe-inline'; manifest-src 'self'; img-src 'self' data:;"
11+
content="style-src 'self'; script-src 'self' 'unsafe-inline'; manifest-src 'self'; img-src 'self' data:;"
1212
/>
1313
<title>code-server login</title>
1414
<link rel="icon" href="{{BASE}}/static-{{COMMIT}}/src/browser/media/favicon.ico" type="image/x-icon" />
@@ -19,6 +19,7 @@
1919
/>
2020
<link rel="apple-touch-icon" href="{{BASE}}/static-{{COMMIT}}/src/browser/media/code-server.png" />
2121
<link href="{{BASE}}/static-{{COMMIT}}/dist/app.css" rel="stylesheet" />
22+
<meta id="coder-options" data-settings="{{OPTIONS}}" />
2223
</head>
2324
<body>
2425
<div class="center-container">
@@ -52,6 +53,7 @@ <h1 class="main">Welcome to code-server</h1>
5253
</div>
5354
</div>
5455
</body>
56+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
5557
<script>
5658
const parts = window.location.pathname.replace(/^\//g, "").split("/")
5759
parts[parts.length - 1] = "{{BASE}}"

src/browser/pages/update.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/>
1717
<link rel="apple-touch-icon" href="{{BASE}}/static-{{COMMIT}}/src/browser/media/code-server.png" />
1818
<link href="{{BASE}}/static-{{COMMIT}}/dist/app.css" rel="stylesheet" />
19+
<meta id="coder-options" data-settings="{{OPTIONS}}" />
1920
</head>
2021
<body>
2122
<div class="center-container">
@@ -32,5 +33,6 @@ <h1 class="main">Update</h1>
3233
</div>
3334
</div>
3435
</div>
36+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
3537
</body>
3638
</html>

src/browser/pages/vscode.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<!-- PROD_ONLY
4242
<link rel="prefetch" href="{{VS_BASE}}/static-{{COMMIT}}/node_modules/semver-umd/lib/semver-umd.js">
4343
END_PROD_ONLY -->
44+
45+
<meta id="coder-options" data-settings="{{OPTIONS}}" />
4446
</head>
4547

4648
<body aria-label=""></body>
@@ -91,6 +93,7 @@
9193
"vs/nls": nlsConfig,
9294
}
9395
</script>
96+
<script src="{{BASE}}/static-{{COMMIT}}/dist/register.js"></script>
9497
<script src="{{VS_BASE}}/static-{{COMMIT}}/out/vs/loader.js"></script>
9598
<!-- PROD_ONLY
9699
<script src="{{VS_BASE}}/static-{{COMMIT}}/out/vs/workbench/workbench.web.api.nls.js"></script>

src/browser/register.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getOptions, normalize } from "../common/util"
2+
3+
const options = getOptions()
4+
5+
if ("serviceWorker" in navigator) {
6+
const path = normalize(`${options.base}/static-${options.commit}/dist/serviceWorker.js`)
7+
navigator.serviceWorker
8+
.register(path, {
9+
scope: options.base || "/",
10+
})
11+
.then(function() {
12+
console.log("[Service Worker] registered")
13+
})
14+
}

0 commit comments

Comments
 (0)