A suite of libraries that enables you to have a Workbox, Next.js, and React based Progressive Web App with basic offline support.
@next-with-offline/next-plugin
: Next.js plugin for generating a Workbox.@next-with-offline/react-hook
: React hook for registering/using a Workbox.@next-with-offline/service-worker
: Code utilities for configuring a service worker.
yarn add @next-with-offline/next-plugin @next-with-offline/react-hook @next-with-offline/service-worker workbox-window
-
Update or create
next.config.js
with:const withOffline = require("@next-with-offline/next-plugin"); module.exports = withOffline({ offline: { path: "/offline", }, // . // .. // ... other Next.js config });
-
Add
public/sw.js
andpublic/sw.js.map
to your.gitignore
:public/sw.js public/sw.js.map
-
Create
worker.js
with:import withNext from "@next-with-offline/service-worker"; withNext({ offlinePath: "/offline", showReloadPrompt: true });
-
Update or create
pages/_app.js
with:import useWorkbox from "@next-with-offline/react-hook"; import { ThemeProvider } from "@material-ui/core/styles"; import { SnackbarProvider } from "notistack"; export default function App({ Component, pageProps }) { useWorkbox({ offlinePath: "/offline", showReloadPrompt() { // A function that returns a Promise // that resolves when user agrees to reload, // or rejects if the user dismisses. }, }); return <Component {...pageProps} />; }
-
Create a basic
pages/offline.js
page:import React from "react"; export default function OfflinePage() { return ( <p> Oops, you appear to be offline. This app requires an internet connection. </p> ); } export default Page;
- offlinePath: string or boolean - a string pathname to the offline page.
Or
false
if you want to disable.- defaults to
/offline
.
- defaults to
- showReloadPrompt: function or boolean - set to a function that returns
a Promise that resolves when user agrees to reload, or rejects if
the user dismisses. set to
true
if you simply want to rely on the default implementation that useswindow.confirm
, or tofalse
if you want to skip waiting and claim clients.- defaults to
false
.
- defaults to