-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
refractor service worker #4123
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
refractor service worker #4123
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
daae4a3
refractor service worker
chenxsan 5467bcd
exclude license
chenxsan 438f914
update dependencies to webpack5 compatible
chenxsan 134bcde
improve sw
chenxsan 34ec540
downgrade html-webpack-plugin
chenxsan 43e3dc3
precache manifest.json
chenxsan 24ac0f1
run e2e test against production
chenxsan 2302b09
set port
chenxsan 8afec3e
add e2e tests for offline
chenxsan 9cd85e6
clean wait
chenxsan 55fba52
Merge branch 'master' into bugfix/fix-service-worker
chenxsan 2083f4a
cleanup action
chenxsan 05e30e3
upgrade workbox-webpack-plugin to v6
chenxsan 6b64981
Merge branch 'master' into bugfix/fix-service-worker
chenxsan 325f391
revert async function
chenxsan 428a75c
clean for cypress v6
chenxsan c9c15a4
fix e2e test
chenxsan 5ae24cd
clean test
chenxsan ea4ef52
Merge branch 'master' into bugfix/fix-service-worker
chenxsan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"baseUrl": "http://localhost:3000", | ||
"baseUrl": "http://localhost:4200", | ||
"video": false | ||
} |
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,81 @@ | ||
// https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/server-communication__offline/cypress/integration/offline-spec.js | ||
|
||
const goOffline = () => { | ||
cy.log('**go offline**') | ||
.then(() => { | ||
return Cypress.automation('remote:debugger:protocol', { | ||
command: 'Network.enable', | ||
}); | ||
}) | ||
.then(() => { | ||
return Cypress.automation('remote:debugger:protocol', { | ||
command: 'Network.emulateNetworkConditions', | ||
params: { | ||
offline: true, | ||
latency: -1, | ||
downloadThroughput: -1, | ||
uploadThroughput: -1, | ||
}, | ||
}); | ||
}); | ||
}; | ||
|
||
const goOnline = () => { | ||
// disable offline mode, otherwise we will break our tests :) | ||
cy.log('**go online**') | ||
.then(() => { | ||
// https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-emulateNetworkConditions | ||
return Cypress.automation('remote:debugger:protocol', { | ||
command: 'Network.emulateNetworkConditions', | ||
params: { | ||
offline: false, | ||
latency: -1, | ||
downloadThroughput: -1, | ||
uploadThroughput: -1, | ||
}, | ||
}); | ||
}) | ||
.then(() => { | ||
return Cypress.automation('remote:debugger:protocol', { | ||
command: 'Network.disable', | ||
}); | ||
}); | ||
}; | ||
|
||
describe('offline', () => { | ||
describe('site', { browser: '!firefox' }, () => { | ||
// make sure we get back online, even if a test fails | ||
// otherwise the Cypress can lose the browser connection | ||
beforeEach(goOnline); | ||
afterEach(goOnline); | ||
|
||
it('shows /migrate/ page', () => { | ||
const url = '/migrate/'; | ||
const text = 'Migrate'; | ||
|
||
cy.visit(url); | ||
cy.get('h1').contains(text); | ||
|
||
goOffline(); | ||
|
||
cy.visit(url); | ||
cy.get('h1').contains(text); | ||
|
||
// click `guides` link | ||
cy.get('a[title="guides"]').click(); | ||
cy.get('h1').contains('Guides'); | ||
}); | ||
|
||
it('open print dialog when accessing /printable url', () => { | ||
const url = '/migrate/printable'; | ||
cy.visit(url, { | ||
onBeforeLoad: (win) => { | ||
cy.stub(win, 'print'); | ||
}, | ||
}); | ||
cy.window().then((win) => { | ||
expect(win.print).to.be.calledOnce; | ||
}); | ||
}); | ||
}); | ||
}); |
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,36 @@ | ||
// we need to precache some assets from ssg too | ||
// they're previously handled by require('./src/utilities/find-files-in-dist')(['.css', '.ico', '.svg']) | ||
|
||
const { Compilation, sources } = require('webpack'); | ||
const getManifestEntriesFromCompilation = require('workbox-webpack-plugin/build/lib/get-manifest-entries-from-compilation'); | ||
|
||
module.exports = class PrecacheSsgManifestPlugin { | ||
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. Please move this to an external dependency. |
||
apply(compiler) { | ||
compiler.hooks.thisCompilation.tap( | ||
'PrecacheSsgManifestPlugin', | ||
(compilation) => { | ||
compilation.hooks.processAssets.tapPromise( | ||
{ | ||
name: 'PrecacheSsgManifestPlugin', | ||
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER - 10, | ||
}, | ||
async () => { | ||
const { sortedEntries } = await getManifestEntriesFromCompilation( | ||
compilation, | ||
{ | ||
// we don't want to include all html pages | ||
// as that would take too many storages | ||
// svg excluded as it's already included with InjectManifest | ||
include: [/\.(ico|css)/i, /app-shell/i], | ||
} | ||
); | ||
compilation.emitAsset( | ||
'ssg-manifest.json', | ||
new sources.RawSource(JSON.stringify(sortedEntries)) | ||
); | ||
} | ||
); | ||
} | ||
); | ||
} | ||
}; |
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,49 @@ | ||
import { precacheAndRoute, matchPrecache } from 'workbox-precaching'; | ||
import { registerRoute } from 'workbox-routing'; | ||
import { CacheableResponsePlugin } from 'workbox-cacheable-response'; | ||
import { CacheFirst, NetworkOnly } from 'workbox-strategies'; | ||
import { ExpirationPlugin } from 'workbox-expiration'; | ||
import { setDefaultHandler, setCatchHandler } from 'workbox-routing'; | ||
import ssgManifest from '../dist/ssg-manifest.json'; | ||
|
||
// Precache assets built with webpack | ||
precacheAndRoute(self.__WB_MANIFEST); | ||
|
||
precacheAndRoute(ssgManifest); | ||
|
||
// Precache manifest.json as ssgManifest couldn't catch it | ||
precacheAndRoute([ | ||
{ | ||
url: '/manifest.json', | ||
revision: '1', // manually update needed when content changed | ||
}, | ||
]); | ||
|
||
// Cache Google Fonts | ||
registerRoute( | ||
/https:\/\/fonts\.gstatic\.com/, | ||
new CacheFirst({ | ||
cacheName: 'google-fonts-cache', | ||
plugins: [ | ||
// Ensure that only requests that result in a 200 status are cached | ||
new CacheableResponsePlugin({ | ||
statuses: [200], | ||
}), | ||
new ExpirationPlugin({ | ||
// Cache for one year | ||
maxAgeSeconds: 60 * 60 * 24 * 365, | ||
maxEntries: 30, | ||
}), | ||
], | ||
}) | ||
); | ||
|
||
setDefaultHandler(new NetworkOnly()); | ||
setCatchHandler(({ event }) => { | ||
switch (event.request.destination) { | ||
case 'document': | ||
return matchPrecache('/app-shell/index.html'); | ||
default: | ||
return Response.error(); | ||
} | ||
}); |
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
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.
We're testing against the production site now, I think it makes more sense than testing against development site.