Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

sample to describe how to init sw outside#1

Closed
JiaLiPassion wants to merge 2 commits intoDevIntent:masterfrom
JiaLiPassion:initfactory
Closed

sample to describe how to init sw outside#1
JiaLiPassion wants to merge 2 commits intoDevIntent:masterfrom
JiaLiPassion:initfactory

Conversation

@JiaLiPassion
Copy link

just to describe an idea to init service-worker in app code.
the change in angular is here, angular/angular#21842

Copy link
Member

@Splaktar Splaktar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a couple of comments on small changes.

One other concern:
Would going down a path similar to how preloading lazy loaded routes works be better? I.e. define a swRegistrationStrategy parameter that then has a couple of Angular provided defaults (like registerImmediately and registerWhenStable). Then allow it to also take user defined strategies similar to what https://github.com/angular/angular/blob/master/aio/content/examples/router/src/app/selective-preloading-strategy.ts does for the PreloadingStrategy.

console.log(`The application is stable? ${status}`);
});
if (environment.production) {
// if (environment.production) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably doesn't need to be commented it out as it isn't related to service workers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I just comment it for local debug, I will add it back.


static getServiceWorkerInitFactory() {
return new Promise<any>((resolve, reject) => {
AppComponent.serviceWorkerResolve = resolve;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will resolve immediately and trigger service worker registration immediately right? It would be nice for the example here to do something like wait 3-5 seconds and then register the service worker.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will update the sample.

@Splaktar Splaktar self-assigned this Jan 28, 2018
@JiaLiPassion
Copy link
Author

@Splaktar , I have updated the sample according the the PR change angular/angular#21842, please review.

Copy link
Member

@Splaktar Splaktar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the direction that this is going 😸

import {GoogleAnalyticsService} from './google-analytics.service';
import {isPlatformBrowser} from '@angular/common';
import {filter} from 'rxjs/operators';
import {Observable} from 'rxjs/Observable';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this file can be reverted?

observer.next();
}, 10000);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be a better example to move this into a file like src/app/custom-registration-strategy.ts.

googleAnalyticsTrackingId: '',
mapsApiKey: 'AIzaSyCG5P0ZrbWcC212QZsEebyyGwj2KRgs700'
mapsApiKey: 'AIzaSyCG5P0ZrbWcC212QZsEebyyGwj2KRgs700',
serviceWorkerStrategy: 'registerImmediately'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably isn't needed as it will have enabled: false in this case and the strategy won't be considered.

serviceWorkerStrategy: 'registerDelay:5000'
// serviceWorkerStrategy: 'registerImmediately'
// serviceWorkerStrategy: 'registerWhenStable'
// serviceWorkerStrategy: getServiceWorkerObservable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see these commented out examples of using different strategies in the app.module.ts.

@Splaktar
Copy link
Member

Closing this, as thankfully we got PR angular/angular#21842 into Angular version 8.0.0 🎉

@Splaktar Splaktar closed this Jun 22, 2019
Splaktar added a commit that referenced this pull request Jun 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments