-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also introduce placeholder temp offline page. TODO: impl bg sync etc
- Loading branch information
1 parent
a815177
commit ec273bf
Showing
8 changed files
with
350 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,8 @@ next-env.d.ts | |
|
||
.env* | ||
|
||
/postgres | ||
/postgres | ||
|
||
# Serwist | ||
public/sw* | ||
public/swe-worker* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { defaultCache } from "@serwist/next/worker"; | ||
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist"; | ||
import { Serwist } from "serwist"; | ||
|
||
declare global { | ||
interface WorkerGlobalScope extends SerwistGlobalConfig { | ||
// Change this attribute's name to your `injectionPoint`. | ||
// `injectionPoint` is an InjectManifest option. | ||
// See https://serwist.pages.dev/docs/build/configuring | ||
__SW_MANIFEST: (PrecacheEntry | string)[] | undefined; | ||
} | ||
} | ||
|
||
// @ts-expect-error | ||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
const serwist = new Serwist({ | ||
precacheEntries: self.__SW_MANIFEST, | ||
skipWaiting: true, | ||
clientsClaim: true, | ||
navigationPreload: true, | ||
runtimeCaching: defaultCache, | ||
fallbacks: { | ||
entries: [ | ||
{ | ||
url: "/~offline", | ||
matcher({ request }) { | ||
return request.destination === "document"; | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
serwist.addEventListeners(); | ||
|
||
/* Push Notifs */ | ||
self.addEventListener("push", function (event: any) { | ||
if (event.data) { | ||
let body; | ||
let icon; | ||
let title; | ||
try { | ||
const data = event.data.json(); | ||
body = data.body; | ||
icon = data.icon || "/icon.png"; | ||
title = data.title; | ||
} catch (e) { | ||
console.log("clearly not a JSON notif"); | ||
body = ""; | ||
icon = "/icon.png"; | ||
title = event.data; | ||
} | ||
|
||
const options = { | ||
body: body, | ||
icon: icon, | ||
badge: "/icon.png", | ||
vibrate: [100, 50, 100], | ||
data: { | ||
dateOfArrival: Date.now(), | ||
primaryKey: "2", | ||
}, | ||
}; | ||
event.waitUntil(self.registration.showNotification(title, options)); | ||
} | ||
}); | ||
|
||
self.addEventListener("notificationclick", function (event: any) { | ||
console.log("Notification click received."); | ||
event.notification.close(); | ||
event.waitUntil( | ||
//@ts-ignore | ||
clients.openWindow("https://schedulerv2.sccs.swarthmore.edu/") | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Metadata } from "next"; | ||
|
||
export const metadata: Metadata = { | ||
title: "Offline", | ||
}; | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<h1>This is offline fallback page</h1> | ||
<h2>When offline, any page route will fallback to this page</h2> | ||
</> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @ts-check | ||
import withSerwistInit from "@serwist/next"; | ||
|
||
// You may want to use a more robust revision to cache | ||
// files more efficiently. | ||
// A viable option is `git rev-parse HEAD`. | ||
const revision = crypto.randomUUID(); | ||
|
||
const withSerwist = withSerwistInit({ | ||
cacheOnNavigation: true, | ||
swSrc: "app/sw.ts", | ||
swDest: "public/sw.js", | ||
additionalPrecacheEntries: [{ url: "/~offline", revision }], | ||
}); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
images: { | ||
domains: ["www.swarthmore.edu", "cdn.vectorstock.com"], | ||
}, | ||
}; | ||
|
||
export default withSerwist(nextConfig); |
Oops, something went wrong.