Last active
September 16, 2022 23:15
-
-
Save JustDoItSascha/c5e05e7a03bfff896646a37015292a5b to your computer and use it in GitHub Desktop.
Splash Screen for Angular 8+ PWA
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
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class PwaService { | |
constructor( | |
private appRef: ApplicationRef, | |
private swUpdate: SwUpdate, | |
) { | |
if (this.swUpdate.isEnabled) { | |
this.appRef.isStable.pipe( | |
first(isStable => isStable === true), | |
switchMap(() => this.swUpdate.available), | |
).subscribe(() => { | |
this.swUpdate.activateUpdate().then(() => document.location.reload()); | |
}); | |
} | |
} | |
checkForUpdate(): Observable<boolean> { | |
const waitFor = 1000; | |
if (this.swUpdate.isEnabled) { | |
const available$ = this.swUpdate.available.pipe( | |
mapTo(true), | |
timeout(waitFor), | |
catchError(() => of(false)), | |
); | |
return fromPromise(this.swUpdate.checkForUpdate()).pipe( | |
switchMap(() => available$), | |
); | |
} | |
return timer(waitFor).pipe(mapTo(false)); | |
} | |
} |
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
@Component({ | |
selector: 'app-splash-screen', | |
template: ` | |
<div class="splash-screen" *ngIf="show" @fadeOut> | |
// Your custom splash screen design | |
</div> | |
`, | |
animations: [ | |
// the fade-in/fade-out animation. | |
trigger('fadeOut', [ | |
transition(':leave', [ | |
query(':leave', animateChild(), {optional: true}), | |
animate(300, style({opacity: 0})) | |
]), | |
]), | |
], | |
styles: [` | |
.splash-screen { | |
position: absolute; | |
top: 0; | |
right: 0; | |
bottom: 0; | |
left: 0; | |
z-index: 9999; | |
} | |
`], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class SplashScreenComponent implements OnInit { | |
show = true; | |
constructor( | |
private pwaService: PwaService, | |
private cdr: ChangeDetectorRef, | |
private appRef: ApplicationRef, | |
) { | |
} | |
ngOnInit() { | |
this.pwaService.checkForUpdate() | |
.subscribe(result => { | |
this.show = result; | |
this.cdr.detectChanges(); | |
}); | |
} | |
} |
I agree with @albinlang, a working demo would be nice.
I agree with @albinlang, a working demo would be nice.
I have tried to get this to work for me the last couple of hours but without any luck. Let me know if you get it to work @jneuhaus
I will try to help you. What kind of error do you get?
Albin Lang <notifications@github.com> schrieb am Fr., 22. Mai 2020, 23:26:
… ***@***.**** commented on this gist.
------------------------------
I agree with @albinlang <https://github.com/albinlang>, a working demo
would be nice.
I have tried to get this to work for me the last couple of hours but
without any luck. Let me know if you get it to work @jneuhaus
<https://github.com/jneuhaus>
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/c5e05e7a03bfff896646a37015292a5b#gistcomment-3314878>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACAADHWUPWCJCNWLLJQGVILRS3UYDANCNFSM4NGYVKMA>
.
I do not get any error at all – the splash screen doesn't show.
I also wonder why "private appRef: ApplicationRef" is in the constructor? I can't see it being used in splash-screen.component.ts (only ion the service where it's also in the constructor).
This sounds strange, you definitely should see it, when you just place the
component anywhere in your code.
Maybe you should create a gist from your code and I will have a look?
Albin Lang <notifications@github.com> schrieb am Fr., 22. Mai 2020, 23:42:
… ***@***.**** commented on this gist.
------------------------------
I also wonder why "private appRef: ApplicationRef" is in the constructor?
I can't see it being used in splash-screen.component.ts (only ion the
service where it's also in the constructor).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/c5e05e7a03bfff896646a37015292a5b#gistcomment-3314901>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACAADHWPPMSPZNNFGVV6LCDRS3WVVANCNFSM4NGYVKMA>
.
@JustDoItSascha I'm not that familiar with how gist works so I invited you to my repo.
Hey Albin, sorry, I was busy the whole week. Can you send me a new
invitation for the repo and I will have a look today.
Albin Lang <notifications@github.com> schrieb am Sa., 23. Mai 2020, 00:02:
… ***@***.**** commented on this gist.
------------------------------
@JustDoItSascha <https://github.com/JustDoItSascha> I'm not that familiar
with how gist works so I invited you to my repo.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/c5e05e7a03bfff896646a37015292a5b#gistcomment-3314917>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACAADHX5OXWCR25Z5LOPLV3RS3Y6FANCNFSM4NGYVKMA>
.
I have no idea what was going wrong in the first place but I created a new project and managed to get it to work.
Public repo here.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, yes it was an interesting topic. Is there a working demo available?
I noticed, but 'fromPromise' doesn't seem to be a part of RxJS so I tried 'from' instead.
Also... Should I place the component between the router-outlet tags in my app component?
Doesn't work currently (with above approach).