-
Notifications
You must be signed in to change notification settings - Fork 823
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
Add docs on how to handle pretty URLS. #903
Comments
Thinking on this a little more, this is messy for precaching. Let's say you have a precache of the following:
At the moment (Out of the box) we will do the following:
This means we'll handle '/'. However, we won't handle the pretty URL case of '/about' or '/something/nested'. @jeffposnick would you be happy to add this into precaching's cache matching? This isn't an issue on template matching or runtime routes. |
I'm okay with that additional "magic" being added in, and perhaps on by default, with the option to disable it via FWIW, the other option is to create a recipe that they used in their build process along the lines of: buildConfig.templatedUrls = glob.sync('**/*.html').map((htmlFile) => {
const url = path.basename(htmlFile, '.html');
return {[url]: htmlFile};
}); That's pretty gnarly, and wouldn't work well for developers who are just using the CLI or webpack plugin. So we can support this by default instead. |
Is there any workaround so far? I really want this feature so that my websites work offline as it is without rewriting any link URLs. |
A workaround is to use a config like the one in #903 (comment), where you explicitly set Have you had any luck with that? |
I forgot to mention that I use the CLI version. |
You could actually do this with the CLI version, too 😄 (I totally understand that this is ugly and not what we'd ideally want developers to have to do, but I want to help unblock you given what's currently possible.) // Inside workbox.config.js:
// Run `npm install -g glob` to make sure it's globally available:
const glob = require('glob');
const templatedUrls = glob.sync('**/*.html').map((htmlFile) => {
const url = path.basename(htmlFile, '.html');
return {[url]: htmlFile};
});
module.exports = {
templatedUrls,
// Your existing config:
globDirectory: '...',
globPatterns: ['...']
}; |
Thank you @jeffposnick! I ended up this script. Note that I use Jekyll here and compiled HTML files of my website is located in const glob = require('glob')
const path = require('path')
module.exports = {
templatedUrls: Object.assign(...glob.sync('_site/**/*.html').map((filename) => {
if (path.basename(filename) === 'index.html') {
return {}
}
return {[filename.slice(6, -5)]: filename}
})),
globDirectory: '_site/',
globPatterns: [
'**/*.{css,html,js,json,png}'
],
globIgnores: [
'register-service-worker.js',
'service-worker.js'
],
swDest: 'service-worker.js'
} |
This is actually documented under clean URLs here: https://developers.google.com/web/tools/workbox/modules/workbox-precaching |
For all users who reached to this issue, now it's no need for configuration of clean URLs. With the latest version of workbox-cli, the script I posted above is almost equivalent to: module.exports = {
globDirectory: '_site/',
globPatterns: [
'**/*.{css,html,js,json,png}'
],
globIgnores: [
'register-service-worker.js',
'service-worker.js'
],
swDest: 'service-worker.js'
} |
So that works for precaching but how would I use a normal route to cache them? I currently precache (using the html files) and the cache never refreshes. |
Taken from #640
The text was updated successfully, but these errors were encountered: