Skip to content

Commit c073309

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): allow component HMR with a service worker
When the service worker option is enabled, each code update will cause the `ngsw.json` configuration file to change due to the different hashes of the updated output. This is problematic for component HMR usage with the development server since the full reload fallback logic would be triggered due to the changed `ngsw.json` file. To avoid this problem, the `ngsw.json` configuration file is now special cased within the fallback logic. (cherry picked from commit c2bef38)
1 parent 4e4fb8a commit c073309

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/angular/build/src/builders/application/build-action.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { BuilderContext } from '@angular-devkit/architect';
1010
import { existsSync } from 'node:fs';
1111
import path from 'node:path';
12-
import { BuildOutputFileType } from '../../tools/esbuild/bundler-context';
12+
import { BuildOutputFile, BuildOutputFileType } from '../../tools/esbuild/bundler-context';
1313
import { ExecutionResult, RebuildState } from '../../tools/esbuild/bundler-execution-result';
1414
import { shutdownSassWorkerPool } from '../../tools/esbuild/stylesheets/sass-language';
1515
import { logMessages, withNoProgress, withSpinner } from '../../tools/esbuild/utils';
@@ -327,8 +327,7 @@ function* emitOutputResults(
327327
if (needFile) {
328328
if (file.path.endsWith('.css')) {
329329
hasCssUpdates = true;
330-
} else if (!/(?:\.m?js|\.map)$/.test(file.path)) {
331-
// Updates to non-JS files must signal an update with the dev server
330+
} else if (!canBackgroundUpdate(file)) {
332331
incrementalResult.background = false;
333332
}
334333

@@ -422,3 +421,15 @@ function* emitOutputResults(
422421
function isCssFilePath(filePath: string): boolean {
423422
return /\.css(?:\.map)?$/i.test(filePath);
424423
}
424+
425+
function canBackgroundUpdate(file: BuildOutputFile): boolean {
426+
// Files in the output root are not served and do not affect the
427+
// application available with the development server.
428+
if (file.type === BuildOutputFileType.Root) {
429+
return true;
430+
}
431+
432+
// Updates to non-JS files must signal an update with the dev server
433+
// except the service worker configuration which is special cased.
434+
return /(?:\.m?js|\.map)$/.test(file.path) || file.path === 'ngsw.json';
435+
}

0 commit comments

Comments
 (0)