@@ -65,17 +65,8 @@ export interface FileInfo {
65
65
export async function augmentIndexHtml (
66
66
params : AugmentIndexHtmlOptions ,
67
67
) : 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 ;
79
70
80
71
const warnings : string [ ] = [ ] ;
81
72
const errors : string [ ] = [ ] ;
@@ -117,7 +108,7 @@ export async function augmentIndexHtml(
117
108
118
109
let scriptTags : string [ ] = [ ] ;
119
110
for ( const [ src , isModule ] of scripts ) {
120
- const attrs = [ `src="${ deployUrl } ${ src } "` ] ;
111
+ const attrs = [ `src="${ generateUrl ( src , deployUrl ) } "` ] ;
121
112
122
113
// This is also need for non entry-points as they may contain problematic code.
123
114
if ( isModule ) {
@@ -141,7 +132,7 @@ export async function augmentIndexHtml(
141
132
let headerLinkTags : string [ ] = [ ] ;
142
133
let bodyLinkTags : string [ ] = [ ] ;
143
134
for ( const src of stylesheets ) {
144
- const attrs = [ `rel="stylesheet"` , `href="${ deployUrl } ${ src } "` ] ;
135
+ const attrs = [ `rel="stylesheet"` , `href="${ generateUrl ( src , deployUrl ) } "` ] ;
145
136
146
137
if ( crossOrigin !== 'none' ) {
147
138
attrs . push ( `crossorigin="${ crossOrigin } "` ) ;
@@ -157,7 +148,7 @@ export async function augmentIndexHtml(
157
148
158
149
if ( params . hints ?. length ) {
159
150
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 ) } "` ] ;
161
152
162
153
if ( hint . mode !== 'modulepreload' && crossOrigin !== 'none' ) {
163
154
// Value is considered anonymous by the browser when not present or empty
@@ -303,6 +294,19 @@ function generateSriAttributes(content: string): string {
303
294
return `integrity="${ algo } -${ hash } "` ;
304
295
}
305
296
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 + : ) ? \/ \/ | d a t a : | c h r o m e : | \/ ) / . test ( value ) ) {
304
+ return value ;
305
+ }
306
+
307
+ return `${ deployUrl } ${ value } ` ;
308
+ }
309
+
306
310
function updateAttribute (
307
311
tag : { attrs : { name : string ; value : string } [ ] } ,
308
312
name : string ,
0 commit comments