I am running an Angular PWA (version 11) on a NestJS (version 7) server. After every new deployment, the PWA crashes because the browser tries to load a JavaScript file which is not there anymore and the server redirects to the root site (html):
main.945bce5e14ef8a6c0362.js:1 Uncaught SyntaxError: Unexpected token '<'
The app module (app.module.ts
) is configured as follows:
import { join } from 'path';
import { Module } from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [
ServeStaticModule.forRoot({
serveStaticOptions: {
etag: false,
setHeaders: (res, path) => {
if (path.includes('ngsw.json')) {
res.set('Cache-Control', 'no-cache, no-store, must-revalidate');
}
}
},
rootPath: join(__dirname, '..', 'pwa'),
exclude: ['/api*']
})
],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}
Any suggestions? Thank you in advance.
I'm gonna show you how I use my service worker which is working, and you can test it and see if it does fix your problem.
This is how you import the service worker:
You should use this in your
app.component
to test that the browser has service worker implemented:This is what your service worker should look like: