feat: integrate Pi backend and Pi Web

This commit is contained in:
luckyyzh
2026-07-30 19:37:53 +08:00
commit 7392ab9dd7
1390 changed files with 337197 additions and 0 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

+83
View File
@@ -0,0 +1,83 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<meta name="theme-color" content="#1a1a1a" />
<title>Pi Web is offline</title>
<style>
:root {
color-scheme: dark;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background: #1a1a1a;
color: #e8e8e8;
}
* {
box-sizing: border-box;
}
body {
min-height: 100dvh;
margin: 0;
display: grid;
place-items: center;
padding: 32px;
}
main {
width: min(100%, 420px);
text-align: center;
}
img {
width: 88px;
height: 88px;
margin-bottom: 24px;
}
h1 {
margin: 0 0 10px;
font-size: 22px;
font-weight: 650;
letter-spacing: 0;
}
p {
margin: 0;
color: #9ca3af;
font-size: 14px;
line-height: 1.6;
}
button {
margin-top: 24px;
min-height: 40px;
padding: 0 18px;
border: 1px solid #3a3a3a;
border-radius: 6px;
background: #242424;
color: #e8e8e8;
font: inherit;
cursor: pointer;
}
button:hover {
background: #2e2e2e;
}
button:focus-visible {
outline: 2px solid #60a5fa;
outline-offset: 3px;
}
</style>
</head>
<body>
<main>
<img src="/icons/icon-192.png" alt="" />
<h1>Pi Web is offline</h1>
<p>Reconnect to the local Pi Web server, then try again.</p>
<button type="button" onclick="location.reload()">Try again</button>
</main>
</body>
</html>
+76
View File
@@ -0,0 +1,76 @@
const CACHE_PREFIX = "pi-web";
const CACHE_VERSION = new URL(self.location.href).searchParams.get("v") || "dev";
const STATIC_CACHE = `${CACHE_PREFIX}-static-${CACHE_VERSION}`;
const OFFLINE_URL = "/offline.html";
const PRECACHE_URLS = [
OFFLINE_URL,
"/manifest.webmanifest",
"/icons/icon-192.png",
"/icons/icon-512.png",
"/icons/apple-touch-icon.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(STATIC_CACHE)
.then((cache) => cache.addAll(PRECACHE_URLS))
.then(() => self.skipWaiting()),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key.startsWith(`${CACHE_PREFIX}-`) && key !== STATIC_CACHE)
.map((key) => caches.delete(key)),
),
)
.then(() => self.clients.claim()),
);
});
self.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
// Session data and live agent traffic must always come from the local server.
if (url.pathname.startsWith("/api/") || url.pathname === "/sw.js") return;
if (request.mode === "navigate") {
event.respondWith(
fetch(request).catch(async () => {
const fallback = await caches.match(OFFLINE_URL);
return fallback ?? Response.error();
}),
);
return;
}
const isStaticAsset =
url.pathname.startsWith("/_next/static/") ||
PRECACHE_URLS.includes(url.pathname);
if (isStaticAsset) {
event.respondWith(cacheFirst(request));
}
});
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
if (response.ok && response.type === "basic") {
const cache = await caches.open(STATIC_CACHE);
await cache.put(request, response.clone());
}
return response;
}