General page cache for different queries #119
Replies: 2 comments 18 replies
-
Well it depends on whether you use InjectManifest or GenerateSW. But, looking at the question, I'm assuming you are using GenerateSW, in which case, you can do something like this: const withPWA = require("@ducanh2912/next-pwa").default({
dest: "public",
extendDefaultRuntimeCaching: true,
workboxOptions: {
runtimeCaching: [
{
urlPattern: ({ request, url: { pathname }, sameOrigin }) =>
request.headers.get("RSC") === "1" &&
request.headers.get("Next-Router-Prefetch") === "1" &&
sameOrigin &&
pathname === "/notes",
handler: "NetworkFirst",
options: {
cacheName: "notes-pages-rsc-prefetch",
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60,
},
matchOptions: {
ignoreSearch: true,
},
},
},
{
urlPattern: ({ request, url: { pathname }, sameOrigin }) =>
request.headers.get("RSC") === "1" &&
sameOrigin &&
pathname === "/notes",
handler: "NetworkFirst",
options: {
cacheName: "notes-pages-rsc",
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60,
},
matchOptions: {
ignoreSearch: true,
},
},
},
{
urlPattern: ({ url: { pathname }, sameOrigin }) =>
sameOrigin && pathname === "/notes",
handler: "NetworkFirst",
options: {
cacheName: "notes-pages",
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60,
},
matchOptions: {
ignoreSearch: true,
},
},
},
],
},
}); Note that this only matches the page '/notes', so you'd probably want to change Sorry for not replying, but this is because I'm not notified of the creation of a new discussion, and I've been busy with Serwist (the successor of this fork). |
Beta Was this translation helpful? Give feedback.
-
Unfortunately, this is not working :( |
Beta Was this translation helpful? Give feedback.
-
Let's assume that I am building a note taking application and I cached a '/notes' page which has 2 filters 'all' and 'todo'. The 2 urls formed will be '/notes?filter=all' ('/notes' also shows all notes by default) and '/notes?filter=todo' respectively.
Currently the problem with next-pwa or even with this fork is that if '/notes' page is cached only that particular page is shown when offline and if you go to '/notes?filter=todo' and reload the page, it throws the offline fallback page instead to linking it with the cached '/notes' page.
Now here if you have static queries, you can cache '/notes', '/notes?filter=all', '/notes?filter=todo' pages but it will take 3 times the necessary cache. And if you have dynamic queries, you can't even cache the pages.
So I was wondering if it is possible that all the '/notes?query=something' requests can be linked with the cached '/notes' page?
Note that I am taking about a CSR page and thus the page will be same irrespective of the filters and then the data can be fetched, or taken from localStorage (if offline) based on the filter.
Beta Was this translation helpful? Give feedback.
All reactions