Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker caching, skeletal "offline mode" #212

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
22 changes: 22 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import preact from "@astrojs/preact";
import mdx from "@astrojs/mdx";
import compress from "astro-compress";
import tailwind from "@astrojs/tailwind";
import serviceWorker from "astrojs-service-worker";

// Allow skipping compression step for faster test build times
// DO NOT SKIP COMPRESSION FOR DEPLOYMENT!
Expand All @@ -23,6 +24,27 @@ export default defineConfig({
mdx(),
tailwind(),
shouldSkipCompress ? null : compress(),
serviceWorker({
workbox: {
globPatterns: [
"**/*.{css,js,jpg,json,png,svg,ico,woff,woff2}", // Cache all assets accept HTML
],
clientsClaim: true,
runtimeCaching: [
{
urlPattern: ({ url }) => url.pathname.endsWith(".html"), // Caches HTML pages
handler: "CacheFirst", // Tries the cache first, then falls back to network if offline
options: {
cacheName: "html-pages-cache",
expiration: {
maxEntries: 50, // Limits the number of HTML pages cached
maxAgeSeconds: 24 * 60 * 60 * 7, // Cache for 1 week
},
},
},
],
},
}),
],
trailingSlash: "ignore",
build: {
Expand Down
Loading