Configuring
Getting started
There are options you can use to customize the behavior of this plugin:
const withPWA = require("@ducanh2912/next-pwa").default({
dest: "public",
// disable: process.env.NODE_ENV === "development",
// register: true,
// scope: "/app",
// sw: "service-worker.js",
//...
});
// Your Next config is automatically typed!
module.exports = withPWA({
// Your Next.js config
});Available options
-
cacheOnFrontendNav— Enable additional route caching when users navigate through pages withnext/link.aggressiveFrontEndNavCaching— Cache every<link rel="stylesheet" />and<script />on frontend navigation. RequirescacheOnFrontEndNavto be enabled.
-
cacheStartUrl— Turn on caching for the start URL.-
dynamicStartUrl— If your start URL returns different HTML document under different states (such as logged in and not logged in), this should be set to true if you also usecacheStartUrl. Effective only whencacheStartUrlis set totrue. -
dynamicStartUrlRedirect— If your start URL redirects to another route such as/login, it's recommended to specify this redirected URL for the best user experience. Effective whendynamicStartUrlis set totrue.
-
-
customWorkerSrc— Change the directory in whichnext-pwalooks for a custom worker implementation to import into the service worker. -
customWorkerDest— The output directory of the custom worker. -
customWorkerPrefix— The custom worker's output filename prefix. -
disable— Whethernext-pwashould be disabled. -
dest— The output directory of the service worker. Relative to Next.js's root directory. -
extendDefaultRuntimeCaching— Extend the defaultruntimeCachingarray. Only effective whenruntimeCachingis specified. -
fallbacks— Configure routes to be precached so that they can be used as a fallback when fetching a resource from both the cache and the network fails. If you just need a fallback document, simply createpages/_offline.tsxorapp/~offline/page.tsx. -
publicExcludes— An array of glob pattern strings to exclude files in the public folder from being precached. -
scope— URL scope for PWA. Defaults tobasePathinnext.config.js. Set to/foo/so that paths under/foo/are PWA while others are not. -
sw— The service worker's output filename. -
register— Whethernext-pwashould automatically register the service worker. Set this tofalseif you want to register the service worker yourself. -
reloadOnOnline— Reload the app when it has gone back online.
Other options
next-pwa uses workbox-webpack-plugin under the hood. As such, its options are also available:
const withPWA = require("@ducanh2912/next-pwa").default({
// Your other options,
workboxOptions: {
// Workbox options go here...
},
});
module.exports = withPWA({
// Your Next.js config
});It is recommended to add // @ts-check to the top of your next.config.js so
as to ensure that you pass valid options to workbox-webpack-plugin.
Runtime caching
next-pwa provides a list of caching strategies out of the box.
You can also write your own list like so:
const withPWA = require("@ducanh2912/next-pwa").default({
// Your other options,
extendDefaultRuntimeCaching: true,
workboxOptions: {
runtimeCaching: [
// Your runtimeCaching array
],
},
});
module.exports = withPWA({
// Your Next.js config
});Also check out Workbox's documentation on
runtimeCaching.