-
-
Notifications
You must be signed in to change notification settings - Fork 78.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-cache key assets with Workboxjs.
- Loading branch information
1 parent
f5368ae
commit fa03833
Showing
6 changed files
with
2,186 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"globDirectory": "./", | ||
"globPatterns": [ | ||
"_gh_pages/**/*.{html,css,js,json,png,jpg}" | ||
], | ||
"swSrc": "./sw.js", | ||
"swDest": "./_gh_pages/sw.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const swBuild = require('workbox-build') | ||
const config = require('./workbox.config.json') | ||
const buildPrefix = '_gh_pages/' | ||
|
||
const workboxSWSrcPath = require.resolve('workbox-sw') | ||
const wbFileName = path.basename(workboxSWSrcPath) | ||
const workboxSWDestPath = buildPrefix + 'assets/js/vendor/' + wbFileName | ||
const workboxSWSrcMapPath = `${workboxSWSrcPath}.map` | ||
const workboxSWDestMapPath = `${workboxSWDestPath}.map` | ||
|
||
fs.createReadStream(workboxSWSrcPath).pipe(fs.createWriteStream(workboxSWDestPath)) | ||
fs.createReadStream(workboxSWSrcMapPath).pipe(fs.createWriteStream(workboxSWDestMapPath)) | ||
|
||
const updateUrl = (manifestEntries) => manifestEntries.map((entry) => { | ||
if (entry.url.startsWith(buildPrefix)) { | ||
const regex = new RegExp(buildPrefix, 'g') | ||
entry.url = entry.url.replace(regex, '') | ||
} | ||
return entry | ||
}) | ||
|
||
config.manifestTransforms = [updateUrl] | ||
|
||
swBuild.injectManifest(config).then(() => { | ||
const wbSwRegex = /{fileName}/g | ||
fs.readFile(config.swDest, 'utf8', (err, data) => { | ||
if (err) { | ||
throw err | ||
} | ||
const swFileContents = data.replace(wbSwRegex, wbFileName) | ||
fs.writeFile(config.swDest, swFileContents, () => { | ||
console.log('Pre-cache Manifest generated.') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.