Skip to content

Commit a2e3dd2

Browse files
authored
fix(app): make the web app installable as a PWA (#53905)
1 parent 7d179e8 commit a2e3dd2

4 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎packages/app/e2e/service-worker/cache.spec.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test, type Page } from "@playwright/test"
1+
import { chromium, expect, test, type Page } from "@playwright/test"
22
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"
33
import { createServer, type ServerResponse } from "node:http"
44
import type { AddressInfo } from "node:net"
@@ -453,3 +453,44 @@ test("the production build precaches every deployable file except on-demand Offi
453453
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())))
454454
}
455455
})
456+
457+
test("the production build installs as an app behind an authenticating proxy", async () => {
458+
const directory = new URL("../../dist/", import.meta.url)
459+
460+
const server = createServer(async (request, response) => {
461+
// Model a reverse proxy that rejects every request without the signed-in user's cookie.
462+
if (!request.headers.cookie?.includes("session=signed-in")) return void response.writeHead(401).end()
463+
const path = new URL(request.url ?? "/", "http://localhost").pathname
464+
const file = extname(path) ? path : "/index.html"
465+
const bytes = await readFile(new URL(`.${file}`, directory)).catch(() => undefined)
466+
467+
if (!bytes) return void response.writeHead(404).end("Not found")
468+
469+
// Check the real HTML without starting the app or its service worker.
470+
if (file === "/index.html")
471+
response.writeHead(200, { "content-type": "text/html", "content-security-policy": "script-src 'none'" })
472+
473+
response.end(bytes)
474+
})
475+
476+
server.listen(0, "127.0.0.1")
477+
await once(server, "listening")
478+
// SAFETY: a server listening on a host and port reports an AddressInfo; only pipe servers report a string.
479+
const address = server.address() as AddressInfo
480+
const url = `http://127.0.0.1:${address.port}`
481+
// Chrome Headless Shell has no install checks and reports no errors for any page, and an incognito context always
482+
// reports one, so check in a persistent Chrome profile.
483+
const context = await chromium.launchPersistentContext("", { channel: "chromium" })
484+
485+
try {
486+
await context.addCookies([{ name: "session", value: "signed-in", url }])
487+
const page = await context.newPage()
488+
await page.goto(url)
489+
const session = await context.newCDPSession(page)
490+
expect((await session.send("Page.getInstallabilityErrors")).installabilityErrors).toEqual([])
491+
} finally {
492+
await context.close()
493+
server.closeAllConnections()
494+
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())))
495+
}
496+
})

‎packages/app/index.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<title>OpenCode</title>
1010
<link rel="icon" type="image/x-icon" href="%OPENCODE_FAVICON%" />
1111
<link rel="apple-touch-icon" sizes="180x180" href="%OPENCODE_APPLE_TOUCH_ICON%" />
12-
<link rel="manifest" href="/site.webmanifest" />
12+
<link rel="manifest" href="/site.webmanifest" crossorigin="use-credentials" />
1313
<meta name="theme-color" content="#fafafa" />
1414
<meta name="mobile-web-app-capable" content="yes" />
1515
<meta name="apple-mobile-web-app-capable" content="yes" />

‎packages/app/manifest.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
"start_url": "/",
66
"scope": "/",
77
"icons": [
8+
{
9+
"src": "/web-app-manifest-192x192.png",
10+
"sizes": "192x192",
11+
"type": "image/png",
12+
"purpose": "any"
13+
},
14+
{
15+
"src": "/web-app-manifest-512x512.png",
16+
"sizes": "512x512",
17+
"type": "image/png",
18+
"purpose": "any"
19+
},
820
{
921
"src": "/web-app-manifest-192x192.png",
1022
"sizes": "192x192",

‎packages/app/vite.icons.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test.each(["dev", "beta", "prod", "local"])("bundles %s app icons", async (chann
2929

3030
if (file?.type !== "asset") throw new Error(`Missing asset: ${path}`)
3131

32-
return typeof file.source === "string" ? new TextEncoder().encode(file.source) : file.source
32+
return new Uint8Array(Buffer.from(file.source))
3333
})
3434
})
3535

@@ -45,12 +45,12 @@ test.each(["dev", "beta", "prod"])("serves %s app icons", async (channel) => {
4545

4646
try {
4747
await server.listen()
48-
const address = server.httpServer?.address()
48+
const url = server.resolvedUrls?.local[0]
4949

50-
if (!address || typeof address === "string") throw new Error("Expected an HTTP port")
50+
if (!url) throw new Error("Expected a local URL")
5151

5252
await check(channel, async (path) => {
53-
const response = await fetch(`http://127.0.0.1:${address.port}${path}`)
53+
const response = await fetch(new URL(path, url))
5454
expect(response.status).toBe(200)
5555

5656
if (path.endsWith(".webmanifest")) expect(response.headers.get("content-type")).toBe("application/manifest+json")
@@ -67,7 +67,7 @@ test.each(["dev", "beta", "prod"])("serves %s app icons", async (channel) => {
6767
async function check(channel: string, read: (path: string) => Promise<Uint8Array>) {
6868
const html = new TextDecoder().decode(await read("/index.html"))
6969
const actual: typeof manifest = JSON.parse(new TextDecoder().decode(await read("/site.webmanifest")))
70-
expect(actual.icons.every((icon) => icon.purpose === "maskable")).toBe(true)
70+
expect(actual.icons.some((icon) => icon.purpose === "maskable")).toBe(true)
7171
expect(actual).toEqual({
7272
...manifest,
7373
icons: manifest.icons.map((icon) => ({ ...icon, src: `/icons/${channel}${icon.src}` })),

0 commit comments

Comments
 (0)