Skip to content

Commit

Permalink
Add caching assets
Browse files Browse the repository at this point in the history
  • Loading branch information
reynaldichernando committed Dec 3, 2024
1 parent 8e38dfa commit 788bce6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
18 changes: 15 additions & 3 deletions app/sw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defaultCache } from "@serwist/next/worker";
import type { PrecacheEntry, SerwistGlobalConfig } from "serwist";
import { Serwist } from "serwist";
import { StaleWhileRevalidate, Serwist } from "serwist";

declare global {
interface WorkerGlobalScope extends SerwistGlobalConfig {
Expand All @@ -15,7 +14,20 @@ const serwist = new Serwist({
skipWaiting: true,
clientsClaim: true,
navigationPreload: true,
runtimeCaching: defaultCache,
runtimeCaching: [
{
matcher: ({ request }) => request.destination === "style",
handler: new StaleWhileRevalidate(),
},
{
matcher: ({ request }) => request.destination === "script",
handler: new StaleWhileRevalidate(),
},
{
matcher: ({ request }) => request.destination === "document",
handler: new StaleWhileRevalidate(),
},
],
});

serwist.addEventListeners();
9 changes: 4 additions & 5 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ const withSerwist = withSerwistInit({
// use something else that works, such as "service-worker/index.ts".
swSrc: "app/sw.ts",
swDest: "public/sw.js",
cacheOnNavigation: true,
});

const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '*',
port: '',
pathname: '**',
protocol: "https",
hostname: "*",
port: "",
pathname: "**",
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"dev": "next dev --experimental-https",
"build": "next build",
"start": "next start",
"lint": "next lint"
Expand Down

0 comments on commit 788bce6

Please sign in to comment.