-
Notifications
You must be signed in to change notification settings - Fork 26.2k
feat(aio): Convert AIO to use the new Service Worker 5.0.0. #19795
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
Conversation
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.
🎉 ✨ (Quite excited about the dynamic content caching 😄)
A couple of comments/questions:
- You can now remove the
--ignore-packages @angular/service-worker
part from here. - The
SwUpdatesService
spec needs to be updated too 😁 - Do we still need ngsw-manifest.json?
@@ -31,38 +30,27 @@ export class SwUpdatesService implements OnDestroy { | |||
private checkInterval = 1000 * 60 * 60 * 6; // 6 hours | |||
private onDestroy = new Subject(); | |||
private checkForUpdateSubj = new Subject(); | |||
updateActivated = this.sw.updates | |||
updateActivated = this.sw.activated | |||
.takeUntil(this.onDestroy) | |||
.do(evt => this.log(`Update event: ${JSON.stringify(evt)}`)) |
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.
Nit: Update event --> Activation event
@@ -8,11 +8,4 @@ if (environment.production) { | |||
enableProdMode(); | |||
} | |||
|
|||
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => { | |||
if (environment.production && 'serviceWorker' in (navigator as any)) { |
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.
Will the ServiceWorker still be registered on production only?
(How does it know which environment it is in?)
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.
The module is only added if environment.production
is set.
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.
Will that work fine if the SW support doesn't exist at runtime (e.g. ios)?
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.
Yes, the module itself does feature detection to decide whether to register the SW.
aio/src/ngsw.json
Outdated
], | ||
"urls": [ | ||
"https://fonts.googleapis.com/**", | ||
"http://fonts.gstatic.com/s/**", |
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.
Shouldn't this be https
too?
aio/src/ngsw.json
Outdated
@@ -0,0 +1,50 @@ | |||
{ | |||
"index": "/index.html", |
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.
How does the new ServiceWorker handle routing? (For reference this was our previous config.)
aio/src/ngsw.json
Outdated
"/generated/docs/api/api-list.json" | ||
] | ||
} | ||
}] |
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.
AFAICT, it is possible to follow the previous config more closely (if not exactly) wrt to what gets cached and when.
Have you intentionally deviated from it?
aio/package.json
Outdated
"sw-manifest": "ngu-sw-manifest --dist dist --in ngsw-manifest.json --out dist/ngsw-manifest.json", | ||
"sw-copy": "cp node_modules/@angular/service-worker/bundles/worker-basic.min.js dist/", | ||
"sw-manifest": "ngsw-config dist src/ngsw.json", | ||
"sw-copy": "cp node_modules/@angular/service-worker/ngsw-worker.js dist/", |
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.
Just out of curiosity, couldn't you use assets
of the CLI?
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.
The SW script has to be registered at the root of the page's origin, it can't come from a subdirectory.
aio/src/ngsw.json
Outdated
"index": "/index.html", | ||
"assetGroups": [{ | ||
"name": "site", | ||
"mode": "prefetch", |
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.
Do you mean installMode
?
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.
Fixed.
aio/src/ngsw.json
Outdated
"/pwa-manifest.json", | ||
"/app/search/search-worker.js" | ||
], | ||
"urls": [ |
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.
I'd move this block with fonts to a separate group, because it will not follow prefetch
rule anyway
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 conceptually part of the site.
@@ -31,38 +30,27 @@ export class SwUpdatesService implements OnDestroy { | |||
private checkInterval = 1000 * 60 * 60 * 6; // 6 hours | |||
private onDestroy = new Subject(); | |||
private checkForUpdateSubj = new Subject(); | |||
updateActivated = this.sw.updates | |||
updateActivated = this.sw.activated | |||
.takeUntil(this.onDestroy) | |||
.do(evt => this.log(`Update event: ${JSON.stringify(evt)}`)) |
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.
My IDE says we have to add import 'rxjs/add/operator/do';
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.
Do(ne).
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.
This will be a really useful 'real world' example once it gets to production!
aio/package.json
Outdated
@@ -77,7 +77,7 @@ | |||
"@angular/platform-browser-dynamic": "^5.0.0-beta.3", | |||
"@angular/platform-server": "^5.0.0-beta.3", | |||
"@angular/router": "^5.0.0-beta.3", | |||
"@angular/service-worker": "^1.0.0-beta.16", | |||
"@angular/service-worker": "5.0.0-rc.2", |
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.
Need to rebase to master to pick up 6b74883 and then update this to 5.0.0
. It probably also worth using the Angular CLI 1.6.0-beta.0
release that supports Angular Service Worker.
@alxhub can you please resurrect this PR and get it into a mergable state? thanks! |
fb0cbb6
to
35707ab
Compare
@IgorMinar PTAL. |
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.
I'm a bit concerned about the impact on ergonomics, let's discuss how we can resolve that - I'm afraid that this will require an API change/addition unless you have other ideas. Otherwise this looks good after the nitpicks are resolved.
aio/package.json
Outdated
"sw-manifest": "ngu-sw-manifest --dist dist --in ngsw-manifest.json --out dist/ngsw-manifest.json", | ||
"sw-copy": "cp node_modules/@angular/service-worker/bundles/worker-basic.min.js dist/", | ||
"sw-manifest": "ngsw-config dist src/ngsw-config.json", | ||
"sw-copy": "cp node_modules/@angular/service-worker/ngsw-worker.js dist/", |
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.
can't you update to cli@next and use the new build integration?
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.
Yes, I could. But I don't want to make more changes to AIO's build system than I need to.
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.
FWIW, we should soon upgrade to the latest-ish cli in #18428. So, maybe after that is merged you can use the build integration.
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.
FWIW, we are now on cli@1.6.3.
aio/scripts/_payload-limits.json
Outdated
@@ -1,3 +1,3 @@ | |||
{ | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37068}}} | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37070}}} |
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.
odd.. why the change to polyfills size?
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.
No idea - it was failing.
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 couldn't be failing for a 2B diff 😕
(FWIW, I think the polyfills size has been 37070 for a while, but it wasn't updated in the limits, since it is a small changed - much lower than 1%).
aio/src/index.html
Outdated
@@ -2,7 +2,7 @@ | |||
<html> | |||
<head> | |||
<meta charset="utf-8"> | |||
<title>Angular Docs</title> | |||
<title>Angular Docs!!!</title> |
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.
remove
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.
Fixed.
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 not fixed. Did you forget to push the change?
aio/scripts/_payload-limits.json
Outdated
@@ -1,3 +1,3 @@ | |||
{ | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37068}}} | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37070}}} |
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.
Also, I'd expect the main size to decrease. Isn't that the case or is it less than 1% so the size check in CI doesn't fail?
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.
I'm not sure what you mean. It's probably less than 1%, this doesn't change much code.
aio/src/app/app.module.ts
Outdated
@@ -89,7 +92,8 @@ export const svgIconProviders = [ | |||
MatTabsModule, | |||
MatToolbarModule, | |||
SwUpdatesModule, | |||
SharedModule | |||
SharedModule, | |||
environment.production ? ServiceWorkerModule.register('/ngsw-worker.js') : [], |
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.
this is a bit odd, because it means that now you have to conditionally or optionally inject the ngsw services which makes the application code not very ergonomic.
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.
Good point. I will fix that.
this.updateActivated = Observable.never<string>().takeUntil(this.onDestroy); | ||
return; | ||
} | ||
this.sw = injector.get(SwUpdate); |
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.
ouch. that's ugly. can we do better? either inject it optionally, or even better always register the providers and inject them, but if we are on browser without SW support or in Dev mode, then make the services inert.. or something like that. We should really rethink this, because the current ergonomics are bad. I believe that this is what Stephen was pointing out as well...
@@ -8,11 +8,4 @@ if (environment.production) { | |||
enableProdMode(); | |||
} | |||
|
|||
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => { | |||
if (environment.production && 'serviceWorker' in (navigator as any)) { |
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.
Will that work fine if the SW support doesn't exist at runtime (e.g. ios)?
aio/src/ngsw-config.json
Outdated
{ | ||
"index": "/index.html", | ||
"assetGroups": [{ | ||
"name": "site", |
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.
please rename this to "app-shell"
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.
Done.
aio/src/ngsw-config.json
Outdated
] | ||
} | ||
}, { | ||
"name": "docs", |
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.
there are actually two docs content groups - the ones that are available offline, and those that are lazy downloaded.
The offline docs include all marketing pages and images under generated/marketing/ I believe - check what the current site is fetching: https://angular.io/ngsw-manifest.json
The the remaining docs and assets should be downloaded on as needed basis. Your proposal changes the update behavior (compared to current angular.io) - I'm fine experimenting with the prefetch mode. We'll see how well this user experience works.
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.
As discussed, the docs
group has been moved to prefetch
.
aio/scripts/_payload-limits.json
Outdated
@@ -1,3 +1,3 @@ | |||
{ | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37068}}} | |||
"aio":{"master":{"change":"application","gzip7":{"inline":925,"main":119519,"polyfills":11863},"gzip9":{"inline":925,"main":119301,"polyfills":11861},"uncompressed":{"inline":1533,"main":486493,"polyfills":37070}}} |
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 couldn't be failing for a 2B diff 😕
(FWIW, I think the polyfills size has been 37070 for a while, but it wasn't updated in the limits, since it is a small changed - much lower than 1%).
aio/src/app/app.module.ts
Outdated
@@ -53,6 +54,8 @@ import { WindowToken, windowProvider } from 'app/shared/window'; | |||
|
|||
import { SharedModule } from 'app/shared/shared.module'; | |||
|
|||
import {environment} from '../environments/environment'; |
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.
Inconsistent spacing inside {...}
.
I don't like the whitespace either, but... ¯\_(ツ)_/¯
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.
Fixed.
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.
Did you mean "Not fixed"? 😁
aio/src/main.ts
Outdated
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => { | ||
if (environment.production && 'serviceWorker' in (navigator as any)) { | ||
const appRef: ApplicationRef = ref.injector.get(ApplicationRef); | ||
appRef.isStable.first().subscribe(() => { |
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.
IIRC, we added this to improve TTI or something.
Is this now handled by the ServiceWorkerModule
internally?
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.
Exactly.
7e7d935
to
9d4d9d4
Compare
…19795) The is a bug in versions 0.6.8+ that breaks when trying to use Jasmine's mock clock. Related to angular/angular-cli#11626. PR Close #19795
This allows URLs to be passed through to the server (where they are properly redirected), instead of serving `index.html` from the SW. Known issue: `/docs/` will be passed through to the server. `/docs` (without the trailing slash) will be correctly treated as a navigation URL and handled by the SW. We don't link to `/docs/` from within the app, but if there are external links to `/docs/` they will require a round-trip to the server and will not work in offline mode. PR Close #19795
`%%DEPLOYMENT_HOST%%` has been assumed to be the host prefix for sitemap URLs since bf29936, but afaict this was never the case. PR Close angular#19795
`%%DEPLOYMENT_HOST%%` has been assumed to be the host prefix for sitemap URLs since bf29936, but afaict this was never the case. PR Close angular#19795
`%%DEPLOYMENT_HOST%%` has been assumed to be the host prefix for sitemap URLs since bf29936, but afaict this was never the case. PR Close angular#19795
…ngular#19795) AIO is currently using a beta version of @angular/service-worker. Since that was implemented, the SW has been rewritten and released as part of Angular 5.0.0. This commit updates AIO to use the latest implementation, with an appropriate configuration file that caches the various AIO assets in useful ways. PR Close angular#19795
…vkit/build-angular to 0.7.0-rc.3 (angular#19795) PR Close angular#19795
…ngular#19795) The is a bug in versions 0.6.8+ that breaks when trying to use Jasmine's mock clock. Related to angular/angular-cli#11626. PR Close angular#19795
…r#19795) This allows URLs to be passed through to the server (where they are properly redirected), instead of serving `index.html` from the SW. Known issue: `/docs/` will be passed through to the server. `/docs` (without the trailing slash) will be correctly treated as a navigation URL and handled by the SW. We don't link to `/docs/` from within the app, but if there are external links to `/docs/` they will require a round-trip to the server and will not work in offline mode. PR Close angular#19795
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
AIO is currently using a beta version of @angular/service-worker.
Since that was implemented, the SW has been rewritten and released
as part of Angular 5.0.0. This commit updates AIO to use the latest
implementation, with an appropriate configuration file that caches
the various AIO assets in useful ways.