Skip to content

Commit f89b3b8

Browse files
authored
Convert to PWA + add service worker (#301)
1 parent c7cfd2f commit f89b3b8

File tree

13 files changed

+124
-1
lines changed

13 files changed

+124
-1
lines changed

bun.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import communityRouter from "./routes/community.ts";
1010
import divisionRouter from "./routes/division.ts";
1111
import imageRouter from "./routes/image.ts";
1212
import languageRouter from "./routes/language.ts";
13+
import pushRouter from "./routes/push.ts";
1314
import universityRouter from "./routes/university.ts";
1415
import usersRouter from "./routes/users/index.ts";
1516

@@ -57,6 +58,7 @@ const app = new Hono()
5758
.route("/language", languageRouter)
5859
.route("/university", universityRouter)
5960
.route("/chat", chatRouter)
61+
.route("/push", pushRouter)
6062
.route("/image", imageRouter);
6163

6264
export default app;

server/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
"devalue": "^5.1.1",
2222
"firebase-admin": "^13.1.0",
2323
"hono": "^4.7.1",
24+
"web-push": "^3.6.7",
2425
"zod": "^3.24.2"
2526
},
2627
"devDependencies": {
2728
"@types/bun": "^1.2.4",
29+
"@types/web-push": "^3.6.4",
2830
"prisma": "^6.3.1",
2931
"typescript": "^5.7.3"
3032
},

server/routes/push.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Hono } from "hono";
2+
import webpush from "web-push";
3+
4+
const vapidKeys = webpush.generateVAPIDKeys();
5+
6+
export function publish() {
7+
"todo";
8+
}
9+
const route = new Hono().get("/pub_key", async (c) => {
10+
return c.json({ pubkey: vapidKeys.publicKey });
11+
});
12+
export default route;

web/public/icon-100x-to-192x.png

12.9 KB
Loading

web/public/icon-100x-to-512x.png

50.9 KB
Loading

web/public/icon.png

6.24 KB
Loading

web/public/manifest.webmanifest

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "UT-Bridge",
3+
"short_name": "UT-Bridge",
4+
"start_url": "/community",
5+
"display": "standalone",
6+
"background_color": "#ffffff",
7+
"theme_color": "#ffffff",
8+
"icons": [
9+
{
10+
"src": "/icon-100x-to-192x.png",
11+
"sizes": "192x192",
12+
"type": "image/png",
13+
"purpose": "any maskable"
14+
},
15+
{
16+
"src": "/icon-100x-to-512x.png",
17+
"sizes": "512x512",
18+
"type": "image/png",
19+
"purpose": "any maskable"
20+
}
21+
]
22+
}

web/public/service-worker.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @ts-check
2+
/// <reference lib="dom" />
3+
4+
self.addEventListener("activate", (ev) => {
5+
try {
6+
console.log("ServiceWorker activated");
7+
} catch {}
8+
9+
// @ts-expect-error
10+
ev.waitUntil(self.clients.claim());
11+
});

web/src/app/[locale]/layout.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { routing } from "@/i18n/routing";
22
import "../../tailwind.css";
3+
import { ServiceWorkerProvider } from "@/features/push/provider";
34
import { NextIntlClientProvider } from "next-intl";
45
import { getMessages } from "next-intl/server";
56
import { notFound } from "next/navigation";
7+
68
export const runtime = "edge";
9+
710
export default async function RootLayout({
811
children,
912
params,
@@ -15,6 +18,7 @@ export default async function RootLayout({
1518
if (!routing.locales.includes(locale as "ja" | "en")) {
1619
notFound();
1720
}
21+
1822
// Providing all messages to the client
1923
// side is the easiest way to get started
2024
const messages = await getMessages();
@@ -23,10 +27,13 @@ export default async function RootLayout({
2327
<head>
2428
<meta charSet="UTF-8" />
2529
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
30+
<link rel="manifest" href="/manifest.webmanifest" crossOrigin="use-credentials" />
2631
<title>ut-bridge</title>
2732
</head>
2833
<body className="flex h-screen flex-col overflow-y-auto">
29-
<NextIntlClientProvider messages={messages}>{children}</NextIntlClientProvider>
34+
<ServiceWorkerProvider>
35+
<NextIntlClientProvider messages={messages}>{children}</NextIntlClientProvider>
36+
</ServiceWorkerProvider>
3037
</body>
3138
</html>
3239
);

0 commit comments

Comments
 (0)