Gathering detailed insights and metrics for gatsby-plugin-offline-next
Gathering detailed insights and metrics for gatsby-plugin-offline-next
npm install gatsby-plugin-offline-next
Typescript
Module System
Min. Node Version
HTML (60.46%)
JavaScript (39.05%)
CSS (0.49%)
Total Downloads
11,990
Last Day
2
Last Week
37
Last Month
181
Last Year
2,082
2 Stars
473 Commits
2 Forks
1 Watching
1 Branches
75 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
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).
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.
npm install gatsby-plugin-offline-next
1// In your gatsby-config.js 2plugins: [`gatsby-plugin-offline-next`]
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 in gatsby-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 if cacheId
has changed. Useful if you're chacheId
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
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
and globDirectory
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 custom runtimeCaching
handlers, you'll need to create a custom swSrc
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
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.
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.
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.
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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
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
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
23 existing vulnerabilities detected
Details
Score
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