-
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
[gatsby-plugin-offline] [gatsby/cache-dir] Fix various offline and caching issues #7355
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7391f2a
Fix various bugs with offline support and caching
a4f477f
Merge branch 'master' of https://github.com/gatsbyjs/gatsby into fix-…
15ee192
Bump gatsby-plugin-offline version and update docs
51c2e10
Restore previous gatsby-plugin-offline version
c0578ca
Handle offline 404 pages properly
5ae987e
Avoid incorrectly showing 404 pages
55448c1
Tidy up get-resources-from-html.js
ef09930
Fix remaining issues with 404 pages
4eb02cd
404 page is no longer critical
9c5f24a
Throw an error if the sw.js patch fails
02e0d8b
Fix remaining 404 and caching issues
2c1cec9
Move 404 logic from loader.js to navigation.js
bf8c67c
Fix 404 page issues with gatsby dev
74ac8e5
Optimise UX and fix bugs
f4f76ab
url can be a constant
vtenfys 35eb86a
fix typo
a8ca6bc
don't cache all webpack chunks
86d7aca
Merge remote-tracking branch 'origin/master' into fix-offline-issues
KyleAMathews 63ca831
Update load-directly-or-404.js
KyleAMathews 3129a55
Ensure precached files are unique
KyleAMathews 8e1efb9
Prettier
KyleAMathews b280ffe
Fix test broken earlier
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,34 +29,39 @@ and AppCache setup by changing these options so tread carefully. | |
|
||
```javascript | ||
const options = { | ||
staticFileGlobs: [ | ||
`${rootDir}/**/*.{woff2}`, | ||
`${rootDir}/commons-*js`, | ||
`${rootDir}/app-*js`, | ||
staticFileGlobs: files.concat([ | ||
`${rootDir}/index.html`, | ||
`${rootDir}/manifest.json`, | ||
`${rootDir}/manifest.webmanifest`, | ||
`${rootDir}/offline-plugin-app-shell-fallback/index.html`, | ||
], | ||
...criticalFilePaths, | ||
]), | ||
stripPrefix: rootDir, | ||
// If `pathPrefix` is configured by user, we should replace | ||
// the `public` prefix with `pathPrefix`. | ||
// See more at: | ||
// https://github.com/GoogleChrome/sw-precache#replaceprefix-string | ||
replacePrefix: args.pathPrefix || ``, | ||
navigateFallback: `/offline-plugin-app-shell-fallback/index.html`, | ||
// Only match URLs without extensions. | ||
// Only match URLs without extensions or the query `no-cache=1`. | ||
// So example.com/about/ will pass but | ||
// example.com/about/?no-cache=1 and | ||
// example.com/cheeseburger.jpg will not. | ||
// We only want the service worker to handle our "clean" | ||
// URLs and not any files hosted on the site. | ||
navigateFallbackWhitelist: [/^.*(?!\.\w?$)/], | ||
// | ||
// Regex based on http://stackoverflow.com/a/18017805 | ||
navigateFallbackWhitelist: [/^.*([^.]{5}|.html)(?<!(\?|&)no-cache=1)$/], | ||
cacheId: `gatsby-plugin-offline`, | ||
// Do cache bust JS URLs until can figure out how to make Webpack's | ||
// URLs truely content-addressed. | ||
dontCacheBustUrlsMatching: /(.\w{8}.woff2)/, //|-\w{20}.js)/, | ||
// Don't cache-bust JS files and anything in the static directory | ||
dontCacheBustUrlsMatching: /(.*js$|\/static\/)/, | ||
runtimeCaching: [ | ||
{ | ||
// Add runtime caching of images. | ||
urlPattern: /\.(?:png|jpg|jpeg|webp|svg|gif|tiff)$/, | ||
// Add runtime caching of various page resources. | ||
urlPattern: /\.(?:png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ugh, we weren't caching json files (page data) 🤦♂️ |
||
handler: `fastest`, | ||
}, | ||
], | ||
skipWaiting: false, | ||
skipWaiting: true, | ||
} | ||
``` |
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
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
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,48 @@ | ||
/** | ||
* When other parts of the code can't find resources for a page, they load the 404 page's | ||
* resources (if it exists) and then pass them here. This module then does the following: | ||
* 1. Checks if 404 pages resources exist. If not, just navigate directly to the desired URL | ||
* to show whatever server 404 page exists. | ||
* 2. Try fetching the desired page to see if it exists on the server but we | ||
* were just prevented from seeing it due to loading the site from a SW. If this is the case, | ||
* trigger a hard reload to grab that page from the server. | ||
* 3. If the page doesn't exist, show the normal 404 page component. | ||
* 4. If the fetch failed (generally meaning we're offline), then navigate anyways to show | ||
* either the browser's offline page or whatever the server error is. | ||
*/ | ||
export default function(resources, path) { | ||
return new Promise(resolve => { | ||
const url = new URL(window.location.origin + path) | ||
|
||
// Append the appropriate query to the URL. | ||
if (url.search) { | ||
url.search += `&no-cache=1` | ||
} else { | ||
url.search = `?no-cache=1` | ||
} | ||
|
||
// Always navigate directly if a custom 404 page doesn't exist. | ||
if (!resources) { | ||
window.location = url | ||
} else { | ||
// Now test if the page is available directly | ||
fetch(url.href) | ||
.then(response => { | ||
if (response.status !== 404) { | ||
// Redirect there if there isn't a 404. If a different HTTP | ||
// error occurs, the appropriate error message will be | ||
// displayed after loading the page directly. | ||
window.location.replace(url) | ||
} else { | ||
// If a 404 occurs, show the custom 404 page. | ||
resolve() | ||
} | ||
}) | ||
.catch(() => { | ||
// If an error occurs (usually when offline), navigate to the | ||
// page anyway to show the browser's proper offline error page | ||
window.location = url | ||
}) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
where's this "no-cache=1" coming from?
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.
oh, I see I think. You're using it as a way to to ensure the sw does catch the reload?
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.
It's for when Gatsby detects a page not found after loading from the offline shell - usually it's correct, but in some cases the website could've been updated, or it's a static file which isn't handled by Gatsby (e.g. Netlify CMS's /admin/ page). We need to check if these files actually exist, which means they need to be loaded from the server directly, not from the cache - that's the purpose of this query, to prevent the cache handling them.