Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
harness
.expectFile('dist/index.html')
.content.toContain(
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap">',
'<link rel="preload" href="https://fonts.googleapis.com/css2?family=Roboto+Mono&family=Roboto:wght@300;400;500;700&display=swap" as="style">',
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function generateIndexHtml(
if (value.type === 'script') {
hints.push({ url: key, mode: 'modulepreload' as const });
} else if (value.type === 'style') {
hints.push({ url: key, mode: 'preload' as const });
// Provide an "as" value of "style" to ensure external URLs which may not have a
// file extension are treated as stylesheets.
hints.push({ url: key, mode: 'preload' as const, as: 'style' });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface AugmentIndexHtmlOptions {
entrypoints: Entrypoint[];
/** Used to set the document default locale */
lang?: string;
hints?: { url: string; mode: string }[];
hints?: { url: string; mode: string; as?: string }[];
}

export interface FileInfo {
Expand Down Expand Up @@ -152,6 +152,11 @@ export async function augmentIndexHtml(
case '.css':
attrs.push('as="style"');
break;
default:
if (hint.as) {
attrs.push(`as="${hint.as}"`);
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,29 @@ describe('augment-index-html', () => {
`);
});

it(`should add prefetch/preload hints with as=style when specified with a URL and an 'as' option`, async () => {
const { content, warnings } = await augmentIndexHtml({
...indexGeneratorOptions,
hints: [
{ mode: 'prefetch', url: 'https://example.com/x?a=1', as: 'style' },
{ mode: 'preload', url: 'http://example.com/y?b=2', as: 'style' },
],
});

expect(warnings).toHaveSize(0);
expect(content).toEqual(oneLineHtml`
<html>
<head>
<base href="/">
<link rel="prefetch" href="https://example.com/x?a=1" as="style">
<link rel="preload" href="http://example.com/y?b=2" as="style">
</head>
<body>
</body>
</html>
`);
});

it('should add `.mjs` script tags', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface IndexHtmlGeneratorProcessOptions {
baseHref: string | undefined;
outputPath: string;
files: FileInfo[];
hints?: { url: string; mode: HintMode }[];
hints?: { url: string; mode: HintMode; as?: string }[];
}

export interface IndexHtmlGeneratorOptions {
Expand Down