Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<meta name="theme-color" content="#131010" media="(prefers-color-scheme: dark)" />
<meta property="og:image" content="/social-share.png" />
<meta property="twitter:image" content="/social-share.png" />
<link rel="preload" href="/assets/JetBrainsMonoNerdFontMono-Regular.woff2" as="font" type="font/woff2" crossorigin />
<script id="oc-theme-preload-script" src="/oc-theme-preload.js"></script>
</head>
<body class="antialiased overscroll-none text-12-regular overflow-hidden">
Expand Down
90 changes: 90 additions & 0 deletions packages/app/public/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/// <reference lib="webworker" />
const CACHE_NAME = "opencode-v1"

/** @param {string} url */
function isHashedAsset(url) {
return /\/assets\/[^/]+[-.][\da-f]{8,}\.\w+$/.test(url)
}

/** @param {string} url */
function isStaticAsset(url) {
return /\.(?:js|css|woff2?|png|svg|ico|webmanifest)(?:\?|$)/.test(url)
}

/** @param {string} url */
function isAPIorEvent(url) {
const path = new URL(url).pathname
return path.startsWith("/api/") || path.endsWith("/event") || path.endsWith("/prompt_async")
}

self.addEventListener("install", () => {
self.skipWaiting()
})

self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))),
),
)
self.clients.claim()
})

self.addEventListener("fetch", (event) => {
const { request } = event
if (request.method !== "GET") return
if (isAPIorEvent(request.url)) return

// Navigation requests: stale-while-revalidate for instant page loads
if (request.mode === "navigate") {
event.respondWith(
caches.open(CACHE_NAME).then((cache) =>
cache.match("/index.html").then((cached) => {
const fresh = fetch(request)
.then((response) => {
if (response.ok) cache.put("/index.html", response.clone())
return response
})
.catch(() => cached)
return cached || fresh
}),
),
)
return
}

// Hashed assets: cache-first (immutable filenames)
if (isHashedAsset(request.url)) {
event.respondWith(
caches.open(CACHE_NAME).then((cache) =>
cache.match(request).then(
(cached) =>
cached ||
fetch(request).then((response) => {
if (response.ok) cache.put(request, response.clone())
return response
}),
),
),
)
return
}

// Other static assets: stale-while-revalidate
if (isStaticAsset(request.url)) {
event.respondWith(
caches.open(CACHE_NAME).then((cache) =>
cache.match(request).then((cached) => {
const fresh = fetch(request)
.then((response) => {
if (response.ok) cache.put(request, response.clone())
return response
})
.catch(() => cached)
return cached || fresh
}),
),
)
return
}
})
4 changes: 4 additions & 0 deletions packages/app/src/entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ if (import.meta.env.VITE_SENTRY_DSN) {
})
}

if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/sw.js").catch(() => {})
}

if (root instanceof HTMLElement) {
const auth = authFromToken(new URLSearchParams(location.search).get("auth_token"))
clearAuthToken()
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
src: url("/assets/JetBrainsMonoNerdFontMono-Regular.woff2") format("woff2");
font-weight: normal;
font-style: normal;
font-display: swap;
}

@layer components {
Expand Down
Loading