(frontend) add offline mode support

Add a offline mode support from a service worker.
The service worker will cache the assets and the
visited pages to be able to work offline.
Created a fallback page for when the user is offline
and tries to access a page that is not cached.
This commit is contained in:
Anthony LC
2024-06-11 14:23:15 +02:00
committed by Anthony LC
parent 5ba35dbc1d
commit 9f6dac53d4
11 changed files with 948 additions and 41 deletions

View File

@@ -1,3 +1,5 @@
const { InjectManifest } = require('workbox-webpack-plugin');
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export',
@@ -9,7 +11,7 @@ const nextConfig = {
// Enables the styled-components SWC transform
styledComponents: true,
},
webpack(config) {
webpack(config, { isServer, dev }) {
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) =>
rule.test?.test?.('.svg'),
@@ -31,6 +33,23 @@ const nextConfig = {
},
);
if (!isServer && !dev) {
config.plugins.push(
new InjectManifest({
swSrc: './src/core/service-worker.ts',
swDest: '../public/service-worker.js',
include: [
({ asset }) => {
if (asset.name.match(/.*(static).*/)) {
return true;
}
return false;
},
],
}),
);
}
// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i;