Skip to content

Commit 775e6f7

Browse files
clydinalan-agius4
authored andcommittedDec 3, 2024·
fix(@angular/build): avoid deploy URL usage on absolute preload links
The `deployUrl` option was unintentionally being prepended to preload links with absolute URLs within the generated index HTML for an appplication. This has now been corrected and absolute URLs will not be altered when a deploy URL is configured. (cherry picked from commit a8ea9cf)
1 parent f6f6c9b commit 775e6f7

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed
 

‎packages/angular/build/src/utils/index-file/augment-index-html.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,8 @@ export interface FileInfo {
6565
export async function augmentIndexHtml(
6666
params: AugmentIndexHtmlOptions,
6767
): Promise<{ content: string; warnings: string[]; errors: string[] }> {
68-
const {
69-
loadOutputFile,
70-
files,
71-
entrypoints,
72-
sri,
73-
deployUrl = '',
74-
lang,
75-
baseHref,
76-
html,
77-
imageDomains,
78-
} = params;
68+
const { loadOutputFile, files, entrypoints, sri, deployUrl, lang, baseHref, html, imageDomains } =
69+
params;
7970

8071
const warnings: string[] = [];
8172
const errors: string[] = [];
@@ -117,7 +108,7 @@ export async function augmentIndexHtml(
117108

118109
let scriptTags: string[] = [];
119110
for (const [src, isModule] of scripts) {
120-
const attrs = [`src="${deployUrl}${src}"`];
111+
const attrs = [`src="${generateUrl(src, deployUrl)}"`];
121112

122113
// This is also need for non entry-points as they may contain problematic code.
123114
if (isModule) {
@@ -141,7 +132,7 @@ export async function augmentIndexHtml(
141132
let headerLinkTags: string[] = [];
142133
let bodyLinkTags: string[] = [];
143134
for (const src of stylesheets) {
144-
const attrs = [`rel="stylesheet"`, `href="${deployUrl}${src}"`];
135+
const attrs = [`rel="stylesheet"`, `href="${generateUrl(src, deployUrl)}"`];
145136

146137
if (crossOrigin !== 'none') {
147138
attrs.push(`crossorigin="${crossOrigin}"`);
@@ -157,7 +148,7 @@ export async function augmentIndexHtml(
157148

158149
if (params.hints?.length) {
159150
for (const hint of params.hints) {
160-
const attrs = [`rel="${hint.mode}"`, `href="${deployUrl}${hint.url}"`];
151+
const attrs = [`rel="${hint.mode}"`, `href="${generateUrl(hint.url, deployUrl)}"`];
161152

162153
if (hint.mode !== 'modulepreload' && crossOrigin !== 'none') {
163154
// Value is considered anonymous by the browser when not present or empty
@@ -303,6 +294,19 @@ function generateSriAttributes(content: string): string {
303294
return `integrity="${algo}-${hash}"`;
304295
}
305296

297+
function generateUrl(value: string, deployUrl: string | undefined): string {
298+
if (!deployUrl) {
299+
return value;
300+
}
301+
302+
// Skip if root-relative, absolute or protocol relative url
303+
if (/^((?:\w+:)?\/\/|data:|chrome:|\/)/.test(value)) {
304+
return value;
305+
}
306+
307+
return `${deployUrl}${value}`;
308+
}
309+
306310
function updateAttribute(
307311
tag: { attrs: { name: string; value: string }[] },
308312
name: string,

‎packages/angular/build/src/utils/index-file/augment-index-html_spec.ts

+40
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,46 @@ describe('augment-index-html', () => {
398398
`);
399399
});
400400

401+
it(`should not add deploy URL to hints with an absolute URL`, async () => {
402+
const { content, warnings } = await augmentIndexHtml({
403+
...indexGeneratorOptions,
404+
deployUrl: 'https://localhost/',
405+
hints: [{ mode: 'preload', url: 'http://example.com/y?b=2' }],
406+
});
407+
408+
expect(warnings).toHaveSize(0);
409+
expect(content).toEqual(oneLineHtml`
410+
<html>
411+
<head>
412+
<base href="/">
413+
<link rel="preload" href="http://example.com/y?b=2">
414+
</head>
415+
<body>
416+
</body>
417+
</html>
418+
`);
419+
});
420+
421+
it(`should not add deploy URL to hints with a root-relative URL`, async () => {
422+
const { content, warnings } = await augmentIndexHtml({
423+
...indexGeneratorOptions,
424+
deployUrl: 'https://example.com/',
425+
hints: [{ mode: 'preload', url: '/y?b=2' }],
426+
});
427+
428+
expect(warnings).toHaveSize(0);
429+
expect(content).toEqual(oneLineHtml`
430+
<html>
431+
<head>
432+
<base href="/">
433+
<link rel="preload" href="/y?b=2">
434+
</head>
435+
<body>
436+
</body>
437+
</html>
438+
`);
439+
});
440+
401441
it('should add `.mjs` script tags', async () => {
402442
const { content } = await augmentIndexHtml({
403443
...indexGeneratorOptions,

0 commit comments

Comments
 (0)
Please sign in to comment.