-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Switch to the Workbox InjectManifest plugin #9205
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
54d3740
WIP
jeffposnick e9fa48b
WIP
jeffposnick 5a3a6c7
Rename
jeffposnick 0eed437
Use publicPath
jeffposnick 3308fb6
Getting closer.
jeffposnick b0cab1b
Move off of NavigationRoute
jeffposnick 53b142a
Update the PWA guide
jeffposnick 8707cc7
Don't precache any LICENSEs.
jeffposnick d2545aa
Updated the ignore instructions
jeffposnick 3ee8b1f
skipWaiting message handler
jeffposnick a893129
Added a comment
jeffposnick 8ff1fe3
Merge branch 'master' into wb-inject-manifest
ianschmitz f96d1d2
Add back web vitals to typescript template
ianschmitz c674f01
Add web vitals back to javascript template
ianschmitz d22c7dc
Change references of CRA 3 to 4
ianschmitz 563589c
Add webworker lib ref
ianschmitz 49b27d5
Fix TypeScript errors in service-worker.ts
ianschmitz f8245c9
Add back eslint disable comment
ianschmitz f3adc73
Ignore restricted-globals across entire file
ianschmitz 94d6494
Runtime caching
jeffposnick d949faa
Explicitly depend on Workbox libs
jeffposnick b02ebb4
Remove SW and package.json deps
jeffposnick 7f768c9
WIP update to README.
jeffposnick 34a46dc
Updates to the docs.
jeffposnick 419a883
Explicitly require InjectManifest
jeffposnick 991e683
Switch back.
jeffposnick 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
packages/cra-template-typescript/template/src/service-worker.ts
This file contains hidden or 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,80 @@ | ||
/// <reference lib="webworker" /> | ||
/* eslint-disable no-restricted-globals */ | ||
|
||
// This service worker can be customized! | ||
// See https://developers.google.com/web/tools/workbox/modules | ||
// for the list of available Workbox modules, or add any other | ||
// code you'd like. | ||
// You can also remove this file if you'd prefer not to use a | ||
// service worker, and the Workbox build step will be skipped. | ||
|
||
import { clientsClaim } from 'workbox-core'; | ||
import { ExpirationPlugin } from 'workbox-expiration'; | ||
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; | ||
import { registerRoute } from 'workbox-routing'; | ||
import { StaleWhileRevalidate} from 'workbox-strategies'; | ||
|
||
declare const self: ServiceWorkerGlobalScope; | ||
|
||
clientsClaim(); | ||
|
||
// Precache all of the assets generated by your build process. | ||
// Their URLs are injected into the manifest variable below. | ||
// This variable must be present somewhere in your service worker file, | ||
// even if you decide not to use precaching. See https://cra.link/PWA | ||
precacheAndRoute(self.__WB_MANIFEST); | ||
|
||
// Set up App Shell-style routing, so that all navigation requests | ||
// are fulfilled with your index.html shell. Learn more at | ||
// https://developers.google.com/web/fundamentals/architecture/app-shell | ||
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); | ||
registerRoute( | ||
// Return false to exempt requests from being fulfilled by index.html. | ||
({ request, url }: { request: Request; url: URL }) => { | ||
// If this isn't a navigation, skip. | ||
if (request.mode !== 'navigate') { | ||
return false; | ||
} | ||
|
||
// If this is a URL that starts with /_, skip. | ||
if (url.pathname.startsWith('/_')) { | ||
return false; | ||
} | ||
|
||
// If this looks like a URL for a resource, because it contains | ||
// a file extension, skip. | ||
if (url.pathname.match(fileExtensionRegexp)) { | ||
return false; | ||
} | ||
|
||
// Return true to signal that we want to use the handler. | ||
return true; | ||
}, | ||
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') | ||
); | ||
|
||
// An example runtime caching route for requests that aren't handled by the | ||
// precache, in this case same-origin .png requests like those from in public/ | ||
registerRoute( | ||
// Add in any other file extensions or routing criteria as needed. | ||
({url}) => url.origin === self.location.origin && url.pathname.endsWith('.png'), | ||
// Customize this strategy as needed, e.g., by changing to CacheFirst. | ||
new StaleWhileRevalidate({ | ||
cacheName: 'images', | ||
plugins: [ | ||
// Ensure that once this runtime cache reaches a maximum size the | ||
// least-recently used images are removed. | ||
new ExpirationPlugin({maxEntries: 50}), | ||
], | ||
}), | ||
); | ||
|
||
// This allows the web app to trigger skipWaiting via | ||
// registration.waiting.postMessage({type: 'SKIP_WAITING'}) | ||
self.addEventListener('message', event => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
// Any other custom service worker logic can go here. |
File renamed without changes.
This file contains hidden or 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 hidden or 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,78 @@ | ||
/* eslint-disable no-restricted-globals */ | ||
|
||
// This service worker can be customized! | ||
// See https://developers.google.com/web/tools/workbox/modules | ||
// for the list of available Workbox modules, or add any other | ||
// code you'd like. | ||
// You can also remove this file if you'd prefer not to use a | ||
// service worker, and the Workbox build step will be skipped. | ||
|
||
import { clientsClaim } from 'workbox-core'; | ||
import { ExpirationPlugin } from 'workbox-expiration'; | ||
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; | ||
import { registerRoute } from 'workbox-routing'; | ||
import { StaleWhileRevalidate } from 'workbox-strategies'; | ||
|
||
clientsClaim(); | ||
|
||
// Precache all of the assets generated by your build process. | ||
// Their URLs are injected into the manifest variable below. | ||
// This variable must be present somewhere in your service worker file, | ||
// even if you decide not to use precaching. See https://cra.link/PWA | ||
precacheAndRoute(self.__WB_MANIFEST); | ||
|
||
// Set up App Shell-style routing, so that all navigation requests | ||
// are fulfilled with your index.html shell. Learn more at | ||
// https://developers.google.com/web/fundamentals/architecture/app-shell | ||
const fileExtensionRegexp = new RegExp('/[^/?]+\\.[^/]+$'); | ||
registerRoute( | ||
// Return false to exempt requests from being fulfilled by index.html. | ||
({ request, url }) => { | ||
// If this isn't a navigation, skip. | ||
if (request.mode !== 'navigate') { | ||
return false; | ||
} | ||
|
||
// If this is a URL that starts with /_, skip. | ||
if (url.pathname.startsWith('/_')) { | ||
return false; | ||
} | ||
|
||
// If this looks like a URL for a resource, because it contains | ||
// a file extension, skip. | ||
if (url.pathname.match(fileExtensionRegexp)) { | ||
return false; | ||
} | ||
|
||
// Return true to signal that we want to use the handler. | ||
return true; | ||
}, | ||
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') | ||
); | ||
|
||
// An example runtime caching route for requests that aren't handled by the | ||
// precache, in this case same-origin .png requests like those from in public/ | ||
registerRoute( | ||
// Add in any other file extensions or routing criteria as needed. | ||
({ url }) => | ||
url.origin === self.location.origin && url.pathname.endsWith('.png'), | ||
// Customize this strategy as needed, e.g., by changing to CacheFirst. | ||
new StaleWhileRevalidate({ | ||
cacheName: 'images', | ||
plugins: [ | ||
// Ensure that once this runtime cache reaches a maximum size the | ||
// least-recently used images are removed. | ||
new ExpirationPlugin({ maxEntries: 50 }), | ||
], | ||
}) | ||
); | ||
|
||
// This allows the web app to trigger skipWaiting via | ||
// registration.waiting.postMessage({type: 'SKIP_WAITING'}) | ||
self.addEventListener('message', event => { | ||
if (event.data && event.data.type === 'SKIP_WAITING') { | ||
self.skipWaiting(); | ||
} | ||
}); | ||
|
||
// Any other custom service worker logic can go here. |
File renamed without changes.
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.