we would like to build a fully offline capable react web application. We usually work with NextJS.
The current problem that we are facing is that we are not able to precache all routes of the application, even though they are not making use of SSR.
For example:
pages
- index.js
- projects
- index.js
- [id.js]
The service worker should precache all pages (HTML) upfront, so that the application is immediately fully offline capable:
We tried to use next-offline and next-pwa, but we were only able to precache the static assets.
Has anybody had a similar requirement and found a solution?
js Progressive Web App (PWA)? Yes!
Redux is being used in many legacy projects, but Redux Toolkit is recommended, as it reduces a lot of boilerplate code and has improved performance.
Next. js also uses a virtual DOM, but it also builds on React to provide additional features that can make Next.
You can make you custom service worker or simply use the package https://github.com/hanford/next-offline with the easy configuration for PWA offline support
Edit: Attached Screenshot for external script
There is an open enhancement request for next-pwa
to enable precaching all pages: How to precache ALL pages
Sylvie Fiquet found a workaround to enable caching all pages with next-pwa
and wrote a blog post: Precaching pages with next-pwa
The short version of the solution follows.
<a>
links: precache HTML files only<Link>
client-side navigation: precache JSON files and at minimum the HTML file for the start URL. Decide whether to precache all HTML files or just have a fallback page.generateBuildId
pwa.additionalManifestEntries
revision
for HTML entriesrevision
set to nullpublic
folder, you have to do it yourselfpwa.dynamicStartUrl
to false
(default true
puts it in the runtime cache instead). Note that this doesn't precache the JSON.OUEmUvoIwu1Azj0i9Vad1
HTML | JSON |
---|---|
/ | /_next/data/OUEmUvoIwu1Azj0i9Vad1/index.json |
/about | /_next/data/OUEmUvoIwu1Azj0i9Vad1/about.json |
/posts/myfirstpost | /_next/data/OUEmUvoIwu1Azj0i9Vad1/posts/myfirstpost.json |
ManifestEntry
objects for /posts/myfirstpost
:{
url: '/posts/myfirstpost',
revision: 'OUEmUvoIwu1Azj0i9Vad1',
}
{
url: '/_next/data/OUEmUvoIwu1Azj0i9Vad1/posts/myfirstpost.json',
revision: null,
}
Caveat: There is potential for trouble if you precache too many pages. Keep an eye on the size of your cache. Remember that a single JS file for a dynamic page could translate into thousands of pages, depending on what happens in getStaticPaths
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With