Skip to content

Commit

Permalink
Merge pull request #212 from bocoup/service-worker-caching
Browse files Browse the repository at this point in the history
Service worker caching, skeletal "offline mode"
  • Loading branch information
stalgiag authored Apr 22, 2024
2 parents c750cbc + 87e1afb commit 64be27a
Show file tree
Hide file tree
Showing 3 changed files with 2,441 additions and 169 deletions.
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

0 comments on commit 64be27a

Please sign in to comment.