-
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
Offline plugin sometimes has a fetch error #8145
Comments
Note that this will cause a Best Practices warning in Lighthouse audits. In #7378, for next.gatsbyjs.org this error is triggered by using |
@m-allanson / @davidbailey00: I still see the first error (in Chrome) when it tries to fetch an external resource (like Analytics). Currently it does catch the TypeError, but (at least) Chrome also throws an error from the promise itself:
Would it be a good approach to combine #8183 and #8158 to only try to fetch internal resources? Something like this:
Where |
@haroldangenent actually we don't do this because some URLs (e.g. web fonts) allow CORS, which means they can be cached. If we only cache internal resources then we might miss out on caching stuff like external fonts and CSS. As you've stated, this does come with the downside of console errors for resources which don't allow CORS, but on the whole it's better to try to cache external resources rather than ignore all of them. |
@davidbailey00 Thanks for your response. That makes sense. Maybe it would still be an idea to use the |
@haroldangenent If I remember correctly, using |
@davidbailey00 I see. I also did some tests out of curiosity (just in Chrome for now), and noticed that all internal responses (using Based on another StackOverflow answer, it seems that
This would not only prevent the error from showing, but it would also cache the script (e.g. Google Analytics). Did some tests and it seems to be working as expected. As the answer states, there are some downsides:
If you think this approach would still be valid, let me know. I'll be happy to investigate some more and open up a PR for review. If not, we'll leave it as it is (and maybe document that these errors are expected). |
@haroldangenent Thanks for looking into this, your findings are super helpful 👍 I'll create a PR today since this is a fairly simple change and I'm not working on tons of other things at the moment, but I'll let you know when once I've done that so you can have a look! |
…use no-cors for external resources (#9679) - Fixes the fallback whitelist regex so that short pages match (e.g. `/`), so that these are served offline correctly - Uses `no-cors` mode when caching external resources, so that errors don't appear if the target doesn't allow CORS requests - Allow insecure external resources to be cached for testing on `localhost` Fixes #8145
@davidbailey00 PR is looking good, thanks for your work on this! |
…use no-cors for external resources (gatsbyjs#9679) - Fixes the fallback whitelist regex so that short pages match (e.g. `/`), so that these are served offline correctly - Uses `no-cors` mode when caching external resources, so that errors don't appear if the target doesn't allow CORS requests - Allow insecure external resources to be cached for testing on `localhost` Fixes gatsbyjs#8145
Description
Some combinations of plugins can cause console errors in the browser when first visiting a site.
Using
gatsby-plugin-offline
withgatsby-plugin-google-analytics
and a plugin that does something like:will trigger the following browser error when first visiting the site:
Reloading the page will load the page without errors. Removing the service worker and reloading the page will display the error again.
In short:
Steps to reproduce
Repo: https://github.com/m-allanson/offline-analytics
Demo video: https://www.youtube.com/watch?v=zPzEq7aMGfA
yarn
yarn run build
gatsby serve
http://localhost:9000
Expected result
No console error
Actual result
Console error on first visit
Environment
The text was updated successfully, but these errors were encountered: