Installations
npm install gatsby-plugin-offline-next
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=12.13.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
HTML (60.46%)
JavaScript (39.05%)
CSS (0.49%)
Developer
kije
Download Statistics
Total Downloads
11,990
Last Day
2
Last Week
37
Last Month
181
Last Year
2,082
GitHub Statistics
2 Stars
473 Commits
2 Forks
1 Watching
1 Branches
75 Contributors
Bundle Size
142.00 B
Minified
129.00 B
Minified + Gzipped
Package Meta Information
Latest Version
5.2.3
Package Id
gatsby-plugin-offline-next@5.2.3
Unpacked Size
51.27 kB
Size
15.35 kB
File Count
20
Total Downloads
Cumulative downloads
Total Downloads
11,990
Last day
-33.3%
2
Compared to previous day
Last week
5.7%
37
Compared to previous week
Last month
19.1%
181
Compared to previous month
Last year
-38.2%
2,082
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
gatsby-plugin-offline-next
Fork of the official gatsby-plugin-offline with updated workbox version. Based on this PR.
⚠️ Note: I created this fork of the above mentioned PR because the PR currently does not seem to get any feedback, and I wanted to provide the plugin with a more recent version of workbox to users who need/want it. If the Gatsby team decides to merge the above mentioned PR, this plugin will be obsolete and the official plugin should be used again.
Thus consider the status of this plugin currently as somewhat unstable (in the sense that if the above mentioned case happens, users will probably need to switch back to the official plugin, which however should not be a big deal, but nonetheless something you should keep in mind if you're using this fork).
Description
Adds drop-in support for making a Gatsby site work offline and more resistant to bad network connections. It uses workbox-webpack-plugin to create a service worker for the site and loads the service worker into the client.
If you're using this plugin with gatsby-plugin-manifest
(recommended) this
plugin should be listed after that plugin so the manifest file can be included
in the service worker.
Install
npm install gatsby-plugin-offline-next
How to use
1// In your gatsby-config.js 2plugins: [`gatsby-plugin-offline-next`]
Available options
In gatsby-plugin-offline-next
5.x, the following options are available:
-
precachePages
lets you specify pages whose resources should be precached by the service worker, using an array of globs. For example:1plugins: [ 2 { 3 resolve: `gatsby-plugin-offline-next`, 4 options: { 5 precachePages: [`/about-us/`, `/projects/*`], 6 }, 7 }, 8]
Note: while essential resources of specified pages will be precached, such as JavaScript and CSS, non-essential resources such as fonts and images will not be included. Instead, these will be cached at runtime when a user visits a given page that includes these resources.
-
swSrc
lets you specify a file that is used as the entry point of the service worker (sw.js
). For example:1plugins: [ 2 { 3 resolve: `gatsby-plugin-offline-next`, 4 options: { 5 swSrc: require.resolve(`src/custom-sw-code.js`), 6 }, 7 }, 8]
1// default workbox setup & logic from `gatsby-plugin-offline-next/serviceworker/index.js`: 2import { precache } from "gatsby-plugin-offline-next/serviceworker/precache.js" 3import { setup } from "gatsby-plugin-offline-next/serviceworker/setup.js" 4import { registerDefaultRoutes } from "gatsby-plugin-offline-next/serviceworker/default-routes.js" 5import { setupOfflineRouting } from "gatsby-plugin-offline-next/serviceworker/offline.js" 6import { googleAnalytics } from "gatsby-plugin-offline-next/serviceworker/google-analytics.js" 7import { cleanup } from "gatsby-plugin-offline-next/serviceworker/cleanup.js" 8import { NavigationRoute, registerRoute } from "workbox-routing" 9 10precache() 11setup() 12registerDefaultRoutes() 13setupOfflineRouting() 14googleAnalytics() 15cleanup() 16 17// custom code: 18 19// show a notification after 15 seconds (the notification 20// permission must be granted first) 21setTimeout(() => { 22 self.registration.showNotification("Hello, world!") 23}, 15000) 24 25// register a custom navigation route 26const customRoute = new NavigationRoute(({ event }) => { 27 // ... 28}) 29registerRoute(customRoute)
The specified file will be compiled/bundled with webpack, so as shown in the example above, other modules can be imported.
Note: if you provide the
swSrc
option, you'll need to make sure that the appropriate workbox routes get set up and also the custom offline logic this plugin provides gets executed. See files ingatsby-plugin-offline-next/serviceworker
for further information -
cacheId
lets you specify a custom cache prefix used by workbox. See Configure Workbox Documentation -
define
Object passed to webpacks DefinePlugin to define values that get replaced in the compiled service worker. See DefinePlugin -
webpackCompilationPlugins
Optional webpack plugins that will be used when compiling the swSrc input file. See InjectManifest options -
chunks
additional webpack chunk names that shall be precached. See InjectManifest for more information -
offlineAnalyticsConfig
If specified, these options get passed to the workbox-google-analytics plugin. You can also set this option to just enable this plugin with the default options -
deletePreviousCacheVersionsOnUpdate
If set to true, automatically attempts to delete previous caches on service worker update ifcacheId
has changed. Useful if you'rechacheId
might change, and you want to avoid old, unused caches form taking up space on the user's device. -
cleanupOutdatedCaches
If set to true, automatically cleans up outdated caches from older workbox versions. See workbox's documentation -
globPatterns
A set of optional glob patterns to include in precaching. -
lodashWebpackPluginFeatures
Configuration of enabled lodash feature sets. See lodash-webpack-plugin -
additionalManifestEntries
,manifestTransforms
,maximumFileSizeToCacheInBytes
,dontCacheBustURLsMatching
,modifyURLPrefix
Options passed to workbox's InjectManifest webpack plugin
Migrating from official gatsby-plugin-offline (v4.x)
Version 5.x of this plugin no longer uses the workbox-build
/generateSW
tool to generate the service worker.
Instead, it uses the InjectManifest
webpack plugin.
This means that some options are no longer supported (although it should be possible to implement the same features via a custom swSrc
-> see above).
To upgrade from a version prior to 5.x (3.x, 4.x) of the official gatsby-plugin-offline
, you'll need to perform the following steps:
-
Remove no longer supported options
importWorkboxFrom
andglobDirectory
-
Move supported options from
workboxConfig
to the root level option object
1// previous 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline`, 5 options: { 6 precachePages: ["about"], 7 workboxConfig: { 8 cacheId: "some-cache-id", 9 offlineGoogleAnalytics: true, 10 cleanupOutdatedCaches: true, 11 directoryIndex: "index.html", 12 importWorkboxFrom: "cdn", 13 }, 14 }, 15 }, 16]
1// v5 (gatsby-plugin-offline-next) 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline-next`, 5 options: { 6 precachePages: ["about"], 7 cacheId: "some-cache-id", 8 offlineGoogleAnalytics: true, 9 cleanupOutdatedCaches: true, 10 directoryIndex: "index.html", 11 }, 12 }, 13]
-
The
runtimeCaching
option is no longer supported in 5.x. If you previously used customruntimeCaching
handlers, you'll need to create a customswSrc
file to archive the same effect.
In case you just added some additional handlers without modifying the default handlers provided by
gatsby-plugin-offline
, this should be straight forward:
1// previous 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline`, 5 options: { 6 workboxConfig: { 7 runtimeCaching: [ 8 // Default handlers from gatsby-plugin-offline 9 { 10 // Use cacheFirst since these don't need to be revalidated (same RegExp 11 // and same reason as above) 12 urlPattern: /(\.js$|\.css$|static\/)/, 13 handler: `CacheFirst`, 14 }, 15 { 16 // page-data.json files, static query results and app-data.json 17 // are not content hashed 18 urlPattern: /^https?:.*\/page-data\/.*\.json/, 19 handler: `StaleWhileRevalidate`, 20 }, 21 { 22 // Add runtime caching of various other page resources 23 urlPattern: /^https?:.*\.(png|jpg|jpeg|webp|avif|svg|gif|tiff|js|woff|woff2|json|css)$/, 24 handler: `StaleWhileRevalidate`, 25 }, 26 { 27 // Google Fonts CSS (doesn't end in .css so we need to specify it) 28 urlPattern: /^https?:\/\/fonts\.googleapis\.com\/css/, 29 handler: `StaleWhileRevalidate`, 30 }, 31 32 // Your custom handler 33 { 34 urlPattern: /my-custom-pattern/, 35 handler: `NetworkFirst`, 36 }, 37 ], 38 }, 39 }, 40 }, 41]
1// 5.x (gatsby-plugin-offline-next) 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline-next`, 5 options: { 6 // ... 7 swSrc: path.resolve(__dirname, "src/custom-sw.js"), 8 }, 9 }, 10]
1// this includes the default behaviour & setup of the service worker from gatsby-plugin-offline 2import "gatsby-plugin-offline-next/serviceworker/index.js" 3 4import { registerRoute } from "workbox-routing" 5import { NetworkFirst } from "workbox-strategies" 6 7// your custom handler goes here 8registerRoute(/my-custom-pattern/, new NetworkFirst(), `GET`)
If you have previously overwritten or modified the default handlers from gatsby-plugin-offline
, you'll need a bit more code in your swSrc
:
1// previous 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline`, 5 options: { 6 workboxConfig: { 7 runtimeCaching: [ 8 // Default handlers from gatsby-plugin-offline 9 { 10 // *modified* 11 // Use cacheFirst since these don't need to be revalidated (same RegExp 12 // and same reason as above) 13 urlPattern: /(\.js$|\.css$|static\/|\.wasm$)/, 14 handler: `StaleWhileRevalidate`, 15 }, 16 { 17 // *not modified* 18 // page-data.json files, static query results and app-data.json 19 // are not content hashed 20 urlPattern: /^https?:.*\/page-data\/.*\.json/, 21 handler: `StaleWhileRevalidate`, 22 }, 23 ], 24 }, 25 }, 26 }, 27]
1// 5.x (gatsby-plugin-offline-next) 2plugins: [ 3 { 4 resolve: `gatsby-plugin-offline-next`, 5 options: { 6 // ... 7 swSrc: path.resolve(__dirname, "src/custom-sw.js"), 8 }, 9 }, 10]
1// code based on gatsby-plugin-offline-next/serviceworker/index.js (note: `registerDefaultRoutes()` is not used, as we will define all used route handlers below ourselfs) 2import { precache } from "gatsby-plugin-offline-next/serviceworker/precache.js" 3import { setup } from "gatsby-plugin-offline-next/serviceworker/setup.js" 4import { setupOfflineRouting } from "gatsby-plugin-offline-next/serviceworker/offline.js" 5import { googleAnalytics } from "gatsby-plugin-offline-next/serviceworker/google-analytics.js" 6import { cleanup } from "gatsby-plugin-offline-next/serviceworker/cleanup.js" 7import { NavigationRoute, registerRoute } from "workbox-routing" 8import { registerRoute } from "workbox-routing" 9import { StaleWhileRevalidate } from "workbox-strategies" 10 11precache() 12setup() 13setupOfflineRouting() 14googleAnalytics() 15cleanup() 16 17// your custom/modified handlers go here. Note: you'll need to specify all handlers manually, even those you didn't modify previously 18registerRoute( 19 /(\.js$|\.css$|static\/|\.wasm$)/, 20 new StaleWhileRevalidate(), 21 `GET` 22) // modified handler 23registerRoute( 24 /^https?:.*\/page-data\/.*\.json/, 25 new StaleWhileRevalidate(), 26 `GET` 27) // unmodified default handler
Remove
If you want to remove gatsby-plugin-offline-next
from your site at a later point,
substitute it with gatsby-plugin-remove-serviceworker
to safely remove the service worker. First, install the new package:
1npm install gatsby-plugin-remove-serviceworker 2npm uninstall gatsby-plugin-offline-next
Then, update your gatsby-config.js
:
1 plugins: [ 2- `gatsby-plugin-offline-next`, 3+ `gatsby-plugin-remove-serviceworker`, 4 ]
This will ensure that the worker is properly unregistered, instead of leaving an outdated version registered in users' browsers.
Notes
Empty View Source and SEO
Gatsby offers great SEO capabilities and that is no different with gatsby-plugin-offline-next
. However, you shouldn't think that Gatsby doesn't serve HTML tags anymore when looking at your source code in the browser (with Right click
=> View source
). View source
doesn't represent the actual HTML data since gatsby-plugin-offline-next
registers and loads a service worker that will cache and handle this differently. Your site is loaded from the service worker, not from its actual source (check your Network
tab in the DevTools for that).
To see the HTML data that crawlers will receive, run this in your terminal:
on Windows (using powershell):
1Invoke-WebRequest https://www.yourdomain.tld | Select -ExpandProperty Content
on Mac OS/Linux:
1curl https://www.yourdomain.tld
Alternatively you can have a look at the /public/index.html
file in your project folder.
App shell and server logs
Server logs (like from Netlify analytics) may show a large number of pageviews to a route like /offline-plugin-app-shell-fallback/index.html
, this is a result of gatsby-plugin-offline-next
adding an app shell to the page. The app shell is a minimal amount of user interface that can be cached offline for reliable performance loading on repeat visits. The shell can be loaded from the cache, and the content of the site loaded into the shell by the service worker.
Using with gatsby-plugin-manifest
If using this plugin with gatsby-plugin-manifest
you may find that your icons are not cached.
In order to solve this, update your gatsby-config.js
as follows:
1// gatsby-config.js 2{ 3 resolve: 'gatsby-plugin-manifest', 4 options: { 5 icon: 'icon.svg', 6 cache_busting_mode: 'none' 7 } 8}, 9{ 10 resolve: 'gatsby-plugin-offline-next', 11 options: { 12 workboxConfig: { 13 globPatterns: ['**/icon-path*'] 14 } 15 } 16}
Updating cache_busting_mode
is necessary. Otherwise, workbox will break while attempting to find the cached URLs.
Adding the globPatterns
makes sure that the offline plugin will cache everything.
Note that you have to prefix your icon with icon-path
or whatever you may call it
![Empty State](/_next/static/media/empty.e5fae2e5.png)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
23 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-c2jc-4fpr-4vhg
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-phwq-j96m-2c2q
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-mhxj-85r3-2x55
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More