Skip to content

Commit 1aaf30b

Browse files
committed
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.
1 parent 63a1c6a commit 1aaf30b

2 files changed

Lines changed: 71 additions & 11 deletions

File tree

‎packages/angular/pwa/pwa/index.ts‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
apply,
1414
chain,
1515
externalSchematic,
16+
filter,
1617
mergeWith,
1718
move,
1819
template,
@@ -151,15 +152,20 @@ export default function (options: PwaOptions): Rule {
151152
// Setup service worker schematic options
152153
const { title, ...swOptions } = options;
153154

154-
await writeWorkspace(host, workspace);
155-
let assetsDir = posix.join(sourcePath, 'assets');
155+
const assetsDir = posix.join(sourcePath, 'assets');
156+
// `Tree.exists()` only matches files, so probe the directory for contained entries.
157+
const assetsDirEntry = host.getDir(assetsDir);
158+
const hasAssetsDir =
159+
!host.exists(assetsDir) &&
160+
(assetsDirEntry.subfiles.length > 0 || assetsDirEntry.subdirs.length > 0);
161+
156162
let iconsPath: string;
157-
if (host.exists(assetsDir)) {
158-
// Add manifest to asset configuration
159-
const assetEntry = posix.join(
160-
project.sourceRoot ?? posix.join(project.root, 'src'),
161-
'manifest.webmanifest',
162-
);
163+
let manifestDir: string;
164+
let iconsDir: string;
165+
if (hasAssetsDir) {
166+
// Pre-v18 project layout: the manifest is served from the application root and the
167+
// icons from the existing `assets` directory, which the build already copies.
168+
const assetEntry = posix.join(sourcePath, 'manifest.webmanifest');
163169
for (const target of [...buildTargets, ...testTargets]) {
164170
if (target.options) {
165171
if (Array.isArray(target.options.assets)) {
@@ -171,16 +177,31 @@ export default function (options: PwaOptions): Rule {
171177
target.options = { assets: [assetEntry] };
172178
}
173179
}
174-
iconsPath = 'assets';
180+
iconsPath = 'assets/icons';
181+
manifestDir = sourcePath;
182+
iconsDir = assetsDir;
175183
} else {
176-
assetsDir = posix.join(project.root, 'public');
177184
iconsPath = 'icons';
185+
manifestDir = iconsDir = posix.join(project.root, 'public');
178186
}
179187

188+
// Written after the asset additions above so that they are persisted.
189+
await writeWorkspace(host, workspace);
190+
180191
return chain([
181192
externalSchematic('@schematics/angular', 'service-worker', swOptions),
182193
mergeWith(
183-
apply(url('./files/assets'), [template({ ...options, iconsPath }), move(assetsDir)]),
194+
apply(url('./files/assets'), [
195+
filter((path) => !path.startsWith('/icons/')),
196+
template({ ...options, iconsPath }),
197+
move(manifestDir),
198+
]),
199+
),
200+
mergeWith(
201+
apply(url('./files/assets'), [
202+
filter((path) => path.startsWith('/icons/')),
203+
move(iconsDir),
204+
]),
184205
),
185206
...[...indexFiles].map((path) => updateIndexFile(path)),
186207
]);

‎packages/angular/pwa/pwa/index_spec.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,43 @@ describe('PWA Schematic', () => {
222222
expect(swFlag).toBe('projects/bar/ngsw-config.json');
223223
});
224224
});
225+
226+
describe('legacy src/assets project layout', () => {
227+
beforeEach(() => {
228+
appTree.create('/projects/bar/src/assets/.gitkeep', '');
229+
appTree.delete('/projects/bar/public/favicon.ico');
230+
231+
const config = JSON.parse(appTree.readContent('/angular.json'));
232+
const build = config.projects.bar.architect.build;
233+
build.options.assets = ['projects/bar/src/favicon.ico', 'projects/bar/src/assets'];
234+
appTree.overwrite('/angular.json', JSON.stringify(config, undefined, 2));
235+
});
236+
237+
it('should place the manifest at the source root and the icons in the assets directory', async () => {
238+
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
239+
240+
expect(tree.exists('/projects/bar/src/manifest.webmanifest')).toBeTrue();
241+
expect(tree.exists('/projects/bar/src/assets/icons/icon-72x72.png')).toBeTrue();
242+
expect(tree.exists('/projects/bar/public/manifest.webmanifest')).toBeFalse();
243+
expect(tree.exists('/projects/bar/public/icons/icon-72x72.png')).toBeFalse();
244+
});
245+
246+
it('should reference the icons relative to the application root in the manifest', async () => {
247+
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
248+
249+
const manifest = JSON.parse(tree.readContent('/projects/bar/src/manifest.webmanifest'));
250+
for (const icon of manifest.icons) {
251+
expect(icon.src).toMatch(/^assets\/icons\/icon-\d+x\d+\.png/);
252+
}
253+
});
254+
255+
it('should add the manifest to the build assets in the workspace configuration', async () => {
256+
const tree = await schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
257+
258+
const config = JSON.parse(tree.readContent('/angular.json'));
259+
expect(config.projects.bar.architect.build.options.assets).toContain(
260+
'projects/bar/src/manifest.webmanifest',
261+
);
262+
});
263+
});
225264
});

0 commit comments

Comments
 (0)