Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/angular/build/src/builders/application/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ export async function normalizeOptions(
? INDEX_HTML_CSR
: indexBaseName;

const preloadInitialDefault = !options.serviceWorker;

indexHtmlOptions = {
input: indexInput,
output: indexOutput,
Expand All @@ -395,8 +397,11 @@ export async function normalizeOptions(
// [name, esm]
] as [string, boolean][],
transformer: extensions?.indexHtmlTransformer,
// Preload initial defaults to true
preloadInitial: typeof options.index !== 'object' || (options.index.preloadInitial ?? true),
// Preload initial defaults to false when using a service worker, true otherwise
preloadInitial:
typeof options.index === 'object'
? (options.index?.preloadInitial ?? preloadInitialDefault)
: preloadInitialDefault,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,6 @@
},
"preloadInitial": {
"type": "boolean",
"default": true,
"description": "Generates 'preload', 'modulepreload', and 'preconnect' link elements for initial application files and resources."
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
expect(JSON.parse(config)).toEqual(jasmine.objectContaining({ index: '/index.csr.html' }));
});

it('should not generate initial modulepreload hints by default when service worker is enabled', async () => {
// Setup an initial chunk usage for JS
await harness.writeFile('src/a.ts', 'console.log("TEST");');
await harness.writeFile('src/b.ts', 'import "./a";');
await harness.writeFile('src/main.ts', 'import "./a";\n(() => import("./b"))();');

harness.useTarget('build', {
...BASE_OPTIONS,
serviceWorker: true,
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/browser/index.html').content.not.toContain('modulepreload');
});

it('should generate initial modulepreload hints when preloadInitial is explicitly true and service worker is enabled', async () => {
// Setup an initial chunk usage for JS
await harness.writeFile('src/a.ts', 'console.log("TEST");');
await harness.writeFile('src/b.ts', 'import "./a";');
await harness.writeFile('src/main.ts', 'import "./a";\n(() => import("./b"))();');

harness.useTarget('build', {
...BASE_OPTIONS,
serviceWorker: true,
index: {
input: 'src/index.html',
preloadInitial: true,
},
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
harness.expectFile('dist/browser/index.html').content.toContain('modulepreload');
});

it('should write JS-imported CSS chunk to browser dist when SSR is enabled', async () => {
await harness.modifyFile('src/tsconfig.app.json', (content) => {
const tsConfig = JSON.parse(content);
Expand Down
Loading