From 1aaf30bba174ca45c1cd61eff22f91b4925b785b Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Mon, 16 Nov 2026 23:04:14 +0600 Subject: [PATCH] fix(@angular/pwa): support projects with the pre-v18 src/assets layout `ng add @angular/pwa` decides between the `public` and legacy `src/assets` layouts with `host.exists(assetsDir)`, but `Tree.exists()` only matches files, so the legacy branch never runs and pre-v18 layout projects get the manifest and icons in a `public` directory their build does not serve. The manifest link injected into index.html then 404s and the application is not installable, while the schematic reports success. Detect the directory through its entries instead and restore the layout that worked before the `public` directory support was introduced: the manifest is written to the source root, where the asset entry added to the workspace configuration and the injected manifest link already point, and the icons are written to the existing `assets` directory with the manifest referencing them as `assets/icons/*`. The workspace file is now written after the asset entry additions, which previously happened after `writeWorkspace()` and were never persisted. Projects using the `public` directory layout are unaffected. --- packages/angular/pwa/pwa/index.ts | 43 +++++++++++++++++++------- packages/angular/pwa/pwa/index_spec.ts | 39 +++++++++++++++++++++++ 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/packages/angular/pwa/pwa/index.ts b/packages/angular/pwa/pwa/index.ts index 1aaea15da4a3..11402c465ddc 100644 --- a/packages/angular/pwa/pwa/index.ts +++ b/packages/angular/pwa/pwa/index.ts @@ -13,6 +13,7 @@ import { apply, chain, externalSchematic, + filter, mergeWith, move, template, @@ -151,15 +152,20 @@ export default function (options: PwaOptions): Rule { // Setup service worker schematic options const { title, ...swOptions } = options; - await writeWorkspace(host, workspace); - let assetsDir = posix.join(sourcePath, 'assets'); + const assetsDir = posix.join(sourcePath, 'assets'); + // `Tree.exists()` only matches files, so probe the directory for contained entries. + const assetsDirEntry = host.getDir(assetsDir); + const hasAssetsDir = + !host.exists(assetsDir) && + (assetsDirEntry.subfiles.length > 0 || assetsDirEntry.subdirs.length > 0); + let iconsPath: string; - if (host.exists(assetsDir)) { - // Add manifest to asset configuration - const assetEntry = posix.join( - project.sourceRoot ?? posix.join(project.root, 'src'), - 'manifest.webmanifest', - ); + let manifestDir: string; + let iconsDir: string; + if (hasAssetsDir) { + // Pre-v18 project layout: the manifest is served from the application root and the + // icons from the existing `assets` directory, which the build already copies. + const assetEntry = posix.join(sourcePath, 'manifest.webmanifest'); for (const target of [...buildTargets, ...testTargets]) { if (target.options) { if (Array.isArray(target.options.assets)) { @@ -171,16 +177,31 @@ export default function (options: PwaOptions): Rule { target.options = { assets: [assetEntry] }; } } - iconsPath = 'assets'; + iconsPath = 'assets/icons'; + manifestDir = sourcePath; + iconsDir = assetsDir; } else { - assetsDir = posix.join(project.root, 'public'); iconsPath = 'icons'; + manifestDir = iconsDir = posix.join(project.root, 'public'); } + // Written after the asset additions above so that they are persisted. + await writeWorkspace(host, workspace); + return chain([ externalSchematic('@schematics/angular', 'service-worker', swOptions), mergeWith( - apply(url('./files/assets'), [template({ ...options, iconsPath }), move(assetsDir)]), + apply(url('./files/assets'), [ + filter((path) => !path.startsWith('/icons/')), + template({ ...options, iconsPath }), + move(manifestDir), + ]), + ), + mergeWith( + apply(url('./files/assets'), [ + filter((path) => path.startsWith('/icons/')), + move(iconsDir), + ]), ), ...[...indexFiles].map((path) => updateIndexFile(path)), ]); diff --git a/packages/angular/pwa/pwa/index_spec.ts b/packages/angular/pwa/pwa/index_spec.ts index d8393d405623..3fc580ce61e1 100644 --- a/packages/angular/pwa/pwa/index_spec.ts +++ b/packages/angular/pwa/pwa/index_spec.ts @@ -222,4 +222,43 @@ describe('PWA Schematic', () => { expect(swFlag).toBe('projects/bar/ngsw-config.json'); }); }); + + describe('legacy src/assets project layout', () => { + beforeEach(() => { + appTree.create('/projects/bar/src/assets/.gitkeep', ''); + appTree.delete('/projects/bar/public/favicon.ico'); + + const config = JSON.parse(appTree.readContent('/angular.json')); + const build = config.projects.bar.architect.build; + build.options.assets = ['projects/bar/src/favicon.ico', 'projects/bar/src/assets']; + appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2)); + }); + + it('should place the manifest at the source root and the icons in the assets directory', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + expect(tree.exists('/projects/bar/src/manifest.webmanifest')).toBeTrue(); + expect(tree.exists('/projects/bar/src/assets/icons/icon-72x72.png')).toBeTrue(); + expect(tree.exists('/projects/bar/public/manifest.webmanifest')).toBeFalse(); + expect(tree.exists('/projects/bar/public/icons/icon-72x72.png')).toBeFalse(); + }); + + it('should reference the icons relative to the application root in the manifest', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + const manifest = JSON.parse(tree.readContent('/projects/bar/src/manifest.webmanifest')); + for (const icon of manifest.icons) { + expect(icon.src).toMatch(/^assets\/icons\/icon-\d+x\d+\.png/); + } + }); + + it('should add the manifest to the build assets in the workspace configuration', async () => { + const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree); + + const config = JSON.parse(tree.readContent('/angular.json')); + expect(config.projects.bar.architect.build.options.assets).toContain( + 'projects/bar/src/manifest.webmanifest', + ); + }); + }); });