-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
fix: service worker failed to install when pathPrefix
option was set
#1912
fix: service worker failed to install when pathPrefix
option was set
#1912
Conversation
Service worker failed to install page caches when `pathPrefix` option was set. There exists a [`replacePrefix` option in `sw-precache`](https://github.com/GoogleChrome/sw-precache#replaceprefix-string). If `pathPrefix` is configured by user, we should replace the `public` prefix with `pathPrefix`. We can also use the [`stripPrefixMulti` option](https://github.com/GoogleChrome/sw-precache#stripprefixmulti-object), but `replacePrefix` is enough here.
Deploy preview ready! Built with commit 13d49ff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the research and nice bug fix!
// the `public` prefix with `pathPrefix`. | ||
// See more at: | ||
// https://github.com/GoogleChrome/sw-precache#replaceprefix-string | ||
replacePrefix: args.pathPrefix || '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You also need to check if the user is actually building with paths prefixed. You can set a pathPrefix but build without them e.g. for local testing.
gatsby/packages/gatsby/src/utils/webpack.config.js
Lines 88 to 89 in cd68468
publicPath: program.prefixPaths | |
? `${store.getState().config.pathPrefix}/` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KyleAMathews I think pathPrefix
will only be set if program.prefixPaths === true
for plugin API(onPostBuild
here), otherwise it will be ''
.
gatsby/packages/gatsby/src/utils/api-runner-node.js
Lines 53 to 55 in 3f5c54d
if (store.getState().program.prefixPaths) { | |
pathPrefix = store.getState().config.pathPrefix | |
} |
P.S.
It seems no full documents for plugin API likeonPostBuild
now, I got thepathPrefix
argument by referring the source 😅. And can I get theprogram.prefixPaths
variable inonPostBuild
function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha so it is :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot I did that… :-)
Deploy preview ready! Built with commit 13d49ff |
Thanks! |
You rock, The Great Gatsby. 🤗 |
Hiya @vincentbel! 👋 This is definitely late, but on behalf of the entire Gatsby community, I wanted to say thank you for being here. Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If you have questions, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! 💪💜 |
Problem
Service worker failed to install page caches when
pathPrefix
option was set.demo:
What Causes the Problem
In the built
sw.js
ofVagr9K/gatsby-advanced-starter
, eachprecachedConfig
item is started with/
(source):and the assets url is generated by(source):
We want the
absoluteUrl
to behttps://vagr9k.github.io/gatsby-advanced-starter/app-7ab9dc184b7d3049a294.js
, but we gothttps://vagr9k.github.io/app-7ab9dc184b7d3049a294.js
.How to Fix the Problem
Let the generated
precachedConfig
array items to be prefixed(lol):There exists a
replacePrefix
option insw-precache
. IfpathPrefix
is configured by user, we should replace thepublic
prefix(rootDir
, used asstripPrefix
) withpathPrefix
. That's how this pull request fix it.How to Verify the Fixes
I have manually tested it, and it works. But you can quickly view the result of
replacePrefix
option by:service-worker.js
by:service-worker.js
.