Skip to content

Commit

Permalink
fix(@angular/build): avoid deploy URL usage on absolute preload links
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
clydin authored and alan-agius4 committed Dec 3, 2024
1 parent f6f6c9b commit 775e6f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/angular/build/src/utils/index-file/augment-index-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,8 @@ export interface FileInfo {
export async function augmentIndexHtml(
params: AugmentIndexHtmlOptions,
): Promise<{ content: string; warnings: string[]; errors: string[] }> {
const {
loadOutputFile,
files,
entrypoints,
sri,
deployUrl = '',
lang,
baseHref,
html,
imageDomains,
} = params;
const { loadOutputFile, files, entrypoints, sri, deployUrl, lang, baseHref, html, imageDomains } =
params;

const warnings: string[] = [];
const errors: string[] = [];
Expand Down Expand Up @@ -117,7 +108,7 @@ export async function augmentIndexHtml(

let scriptTags: string[] = [];
for (const [src, isModule] of scripts) {
const attrs = [`src="${deployUrl}${src}"`];
const attrs = [`src="${generateUrl(src, deployUrl)}"`];

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

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

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

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

function generateUrl(value: string, deployUrl: string | undefined): string {
if (!deployUrl) {
return value;
}

// Skip if root-relative, absolute or protocol relative url
if (/^((?:\w+:)?\/\/|data:|chrome:|\/)/.test(value)) {
return value;
}

return `${deployUrl}${value}`;
}

function updateAttribute(
tag: { attrs: { name: string; value: string }[] },
name: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,46 @@ describe('augment-index-html', () => {
`);
});

it(`should not add deploy URL to hints with an absolute URL`, async () => {
const { content, warnings } = await augmentIndexHtml({
...indexGeneratorOptions,
deployUrl: 'https://localhost/',
hints: [{ mode: 'preload', url: 'http://example.com/y?b=2' }],
});

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

it(`should not add deploy URL to hints with a root-relative URL`, async () => {
const { content, warnings } = await augmentIndexHtml({
...indexGeneratorOptions,
deployUrl: 'https://example.com/',
hints: [{ mode: 'preload', url: '/y?b=2' }],
});

expect(warnings).toHaveSize(0);
expect(content).toEqual(oneLineHtml`
<html>
<head>
<base href="/">
<link rel="preload" href="/y?b=2">
</head>
<body>
</body>
</html>
`);
});

it('should add `.mjs` script tags', async () => {
const { content } = await augmentIndexHtml({
...indexGeneratorOptions,
Expand Down

0 comments on commit 775e6f7

Please sign in to comment.