I know there are some similar questions like so unexpected token after angular 2 production build but it doesnt actually answer my question.
Basically I have an angular app and its a PWA, now everytime I do a new production build the first time I visit the site I get an error like so
and then as soon as I refresh the page, the site works as normal.
This happens on any browser/device
Now my suspicion is that every time the app is updated, the main bundled js file changes, and the PWA has cached the old one and the app is still trying to use the new one.
My Project structure is as follows
I have a root module that lazy loads the other modules now the first module that gets loaded is my account module so the user can log in
this is my root-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
const routes: Routes = [
{ path: '', redirectTo: '/account/login', pathMatch: 'full' },
{
path: 'account',
loadChildren: 'account/account.module#AccountModule',
data: { preload: true }
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })],
exports: [RouterModule],
providers: []
})
export class RootRoutingModule {
}
so technically the first module the user gets to is the account module, but obviously it has to be redirected from the root module.
So what I did was in my root component module I check to see if the Service Worker needs updating like so...
root.component.ts
@import { SwUpdate } from '@angular/service-worker'
...
export class...
constructor(
private _sw: SwUpdate
) {
if (this._sw.isEnabled) {
this._sw.available
.subscribe(() => {
this._sw.activateUpdate()
.then(() => {
window.location.reload(true);
});
});
}
}
now this should check to see if there is an update and then just refresh the page.. but it doesn't work.
this is my ngsw-config.json
{
"index": "/index.html",
"assetGroups": [
{
"name": "App",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/*.css",
"/*.js",
"!/main*.js"
]
}
}, {
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"
]
}
}
]
}
as you can see I am excluding the main.js
chunk file which should eliminate this issue... but it doesn't
Now when I check the network tab after a new build this is what I get
This image was before I excluded the main.js file
this is what my main.js
calls look like after I have excluded it from the ngsw.config
I then thought to just try and catch this error using my sentry error handler like so..
export class RavenErrorHandler implements ErrorHandler {
handleError(err: any): void {
console.log('error', err);
if (err.toLowerCase().includes('token <')) {
window.location.reload(true);
} else {
Raven.captureException(err);
}
}
}
but this isn't working, I am getting the error in sentry but the app is not reloading??
Now I have done some testing, If you visit the site after a fresh build on private mode you will never get the error, it only seems to happen if you have visited the site before, which is what makes me think its a caching issue
My Application is hosted on Azure and my application is using Angular 7.27
Any help would be appreciated!