fix(app): make the web app installable as a PWA (#53905)

This commit is contained in:
Luke Parker
2026-10-09 07:54:07 +10:00
committed by GitHub
Unverified
parent 7d179e83f4
commit a2e3dd2884
4 changed files with 60 additions and 7 deletions
+42 -1
View File
@@ -1,4 +1,4 @@
import { expect, test, type Page } from "@playwright/test"
import { chromium, expect, test, type Page } from "@playwright/test"
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from "node:fs/promises"
import { createServer, type ServerResponse } from "node:http"
import type { AddressInfo } from "node:net"
@@ -453,3 +453,44 @@ test("the production build precaches every deployable file except on-demand Offi
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())))
}
})
test("the production build installs as an app behind an authenticating proxy", async () => {
const directory = new URL("../../dist/", import.meta.url)
const server = createServer(async (request, response) => {
// Model a reverse proxy that rejects every request without the signed-in user's cookie.
if (!request.headers.cookie?.includes("session=signed-in")) return void response.writeHead(401).end()
const path = new URL(request.url ?? "/", "http://localhost").pathname
const file = extname(path) ? path : "/index.html"
const bytes = await readFile(new URL(`.${file}`, directory)).catch(() => undefined)
if (!bytes) return void response.writeHead(404).end("Not found")
// Check the real HTML without starting the app or its service worker.
if (file === "/index.html")
response.writeHead(200, { "content-type": "text/html", "content-security-policy": "script-src 'none'" })
response.end(bytes)
})
server.listen(0, "127.0.0.1")
await once(server, "listening")
// SAFETY: a server listening on a host and port reports an AddressInfo; only pipe servers report a string.
const address = server.address() as AddressInfo
const url = `http://127.0.0.1:${address.port}`
// Chrome Headless Shell has no install checks and reports no errors for any page, and an incognito context always
// reports one, so check in a persistent Chrome profile.
const context = await chromium.launchPersistentContext("", { channel: "chromium" })
try {
await context.addCookies([{ name: "session", value: "signed-in", url }])
const page = await context.newPage()
await page.goto(url)
const session = await context.newCDPSession(page)
expect((await session.send("Page.getInstallabilityErrors")).installabilityErrors).toEqual([])
} finally {
await context.close()
server.closeAllConnections()
await new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve())))
}
})
+1 -1
View File
@@ -9,7 +9,7 @@
<title>OpenCode</title>
<link rel="icon" type="image/x-icon" href="%OPENCODE_FAVICON%" />
<link rel="apple-touch-icon" sizes="180x180" href="%OPENCODE_APPLE_TOUCH_ICON%" />
<link rel="manifest" href="/site.webmanifest" />
<link rel="manifest" href="/site.webmanifest" crossorigin="use-credentials" />
<meta name="theme-color" content="#fafafa" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
+12
View File
@@ -5,6 +5,18 @@
"start_url": "/",
"scope": "/",
"icons": [
{
"src": "/web-app-manifest-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "any"
},
{
"src": "/web-app-manifest-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "any"
},
{
"src": "/web-app-manifest-192x192.png",
"sizes": "192x192",
+5 -5
View File
@@ -29,7 +29,7 @@ test.each(["dev", "beta", "prod", "local"])("bundles %s app icons", async (chann
if (file?.type !== "asset") throw new Error(`Missing asset: ${path}`)
return typeof file.source === "string" ? new TextEncoder().encode(file.source) : file.source
return new Uint8Array(Buffer.from(file.source))
})
})
@@ -45,12 +45,12 @@ test.each(["dev", "beta", "prod"])("serves %s app icons", async (channel) => {
try {
await server.listen()
const address = server.httpServer?.address()
const url = server.resolvedUrls?.local[0]
if (!address || typeof address === "string") throw new Error("Expected an HTTP port")
if (!url) throw new Error("Expected a local URL")
await check(channel, async (path) => {
const response = await fetch(`http://127.0.0.1:${address.port}${path}`)
const response = await fetch(new URL(path, url))
expect(response.status).toBe(200)
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) => {
async function check(channel: string, read: (path: string) => Promise<Uint8Array>) {
const html = new TextDecoder().decode(await read("/index.html"))
const actual: typeof manifest = JSON.parse(new TextDecoder().decode(await read("/site.webmanifest")))
expect(actual.icons.every((icon) => icon.purpose === "maskable")).toBe(true)
expect(actual.icons.some((icon) => icon.purpose === "maskable")).toBe(true)
expect(actual).toEqual({
...manifest,
icons: manifest.icons.map((icon) => ({ ...icon, src: `/icons/${channel}${icon.src}` })),