Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(workbox): offline analytics #55

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/modules/workbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ workbox: {

**importScripts** (Array) - Additional scripts to be imported in service worker script. (Relative to `/`. Can be placed in `assets/` directory)


**offlineAnalytics** - (Default: false) Enable offline Google Analytics tracking [through workbox](https://developers.google.com/web/tools/workbox/guides/enable-offline-analytics)

**offlineAssets** (String) - List of assets to precache, in case you need extra files precached other than the ones automatically processed (`_nuxt/*` etc) or you want to ensure assets used by your `offlinePage`. (Example: `['/offline.png']`)

**offlinePage** (String) - Enables routing all offline requests to the specified path. (Example: `/offline.html`)
Expand Down
8 changes: 6 additions & 2 deletions packages/workbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ function getOptions (moduleOptions) {
routingExtensions: null,
config: null,
cacheId: process.env.npm_package_name || 'nuxt',
clientsClaim: true,
skipWaiting: true,
globPatterns: ['**/*.{js,css}'],
globDirectory: undefined,
Expand All @@ -100,7 +99,8 @@ function getOptions (moduleOptions) {
handler: 'cacheFirst'
}
],
runtimeCaching: []
runtimeCaching: [],
offlineAnalytics: false
}

const options = defaultsDeep({}, this.options.workbox, moduleOptions, defaults)
Expand Down Expand Up @@ -156,6 +156,10 @@ function addTemplates (options) {
skipWaiting: options.skipWaiting,
wbOptions: {
cacheId: options.cacheId,
clientsClaim: options.clientsClaim,
directoryIndex: options.directoryIndex,
},
offlineAnalytics: options.offlineAnalytics
directoryIndex: options.directoryIndex,
cleanUrls: false
}
Expand Down
4 changes: 4 additions & 0 deletions packages/workbox/templates/sw.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ options.runtimeCaching.forEach(r => {
workbox.routing.registerRoute(new RegExp('<%= r.urlPattern %>'), workbox.strategies.<%= r.handler %>(<%= strategy %>), '<%= r.method %>')
<% }) %>

<% if(options.offlineAnalytics){
workboxSW.googleAnalytics.initialize()
} %>

<% if (options.offlinePage) { %>
// offlinePage support
const strategy = workbox.strategies.networkOnly()
Expand Down