Skip to content

Commit f42f101

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): ensure preload hints for external stylesheets are marked as styles
When a preload hint is added for a stylesheet that is referenced via an `@import` that has an URL that does not contain a file extension, an `as` attribute is now correctly added to the hint to ensure that the stylesheet is loaded properly. This case can happen when a font service URL is imported within a initial stylesheet.
1 parent f82bbca commit f42f101

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

Diff for: packages/angular_devkit/build_angular/src/builders/application/tests/behavior/index-preload-hints_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
3030
harness
3131
.expectFile('dist/index.html')
3232
.content.toContain(
33-
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap">',
33+
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap" as="style">',
3434
);
3535
});
3636
});

Diff for: packages/angular_devkit/build_angular/src/tools/esbuild/index-html-generator.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export function generateIndexHtml(
4545
if (value.type === 'script') {
4646
hints.push({ url: key, mode: 'modulepreload' as const });
4747
} else if (value.type === 'style') {
48-
hints.push({ url: key, mode: 'preload' as const });
48+
// Provide an "as" value of "style" to ensure external URLs which may not have a
49+
// file extension are treated as stylesheets.
50+
hints.push({ url: key, mode: 'preload' as const, as: 'style' });
4951
}
5052
}
5153
}

Diff for: packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface AugmentIndexHtmlOptions {
3939
entrypoints: Entrypoint[];
4040
/** Used to set the document default locale */
4141
lang?: string;
42-
hints?: { url: string; mode: string }[];
42+
hints?: { url: string; mode: string; as?: string }[];
4343
}
4444

4545
export interface FileInfo {
@@ -152,6 +152,11 @@ export async function augmentIndexHtml(
152152
case '.css':
153153
attrs.push('as="style"');
154154
break;
155+
default:
156+
if (hint.as) {
157+
attrs.push(`as="${hint.as}"`);
158+
}
159+
break;
155160
}
156161
}
157162

Diff for: packages/angular_devkit/build_angular/src/utils/index-file/augment-index-html_spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,29 @@ describe('augment-index-html', () => {
375375
`);
376376
});
377377

378+
it(`should add prefetch/preload hints with as=style when specified with a URL and an 'as' option`, async () => {
379+
const { content, warnings } = await augmentIndexHtml({
380+
...indexGeneratorOptions,
381+
hints: [
382+
{ mode: 'prefetch', url: 'https://example.com/x?a=1', as: 'style' },
383+
{ mode: 'preload', url: 'http://example.com/y?b=2', as: 'style' },
384+
],
385+
});
386+
387+
expect(warnings).toHaveSize(0);
388+
expect(content).toEqual(oneLineHtml`
389+
<html>
390+
<head>
391+
<base href="/">
392+
<link rel="prefetch" href="https://example.com/x?a=1" as="style">
393+
<link rel="preload" href="http://example.com/y?b=2" as="style">
394+
</head>
395+
<body>
396+
</body>
397+
</html>
398+
`);
399+
});
400+
378401
it('should add `.mjs` script tags', async () => {
379402
const { content } = await augmentIndexHtml({
380403
...indexGeneratorOptions,

Diff for: packages/angular_devkit/build_angular/src/utils/index-file/index-html-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface IndexHtmlGeneratorProcessOptions {
2828
baseHref: string | undefined;
2929
outputPath: string;
3030
files: FileInfo[];
31-
hints?: { url: string; mode: HintMode }[];
31+
hints?: { url: string; mode: HintMode; as?: string }[];
3232
}
3333

3434
export interface IndexHtmlGeneratorOptions {

0 commit comments

Comments
 (0)