@@ -22,6 +22,7 @@ import {
22
22
partialEncodeURIPath ,
23
23
processSrcSet ,
24
24
removeLeadingSlash ,
25
+ unique ,
25
26
urlCanParse ,
26
27
} from '../utils'
27
28
import type { ResolvedConfig } from '../config'
@@ -1265,6 +1266,42 @@ export function resolveHtmlTransforms(
1265
1266
return [ preHooks , normalHooks , postHooks ]
1266
1267
}
1267
1268
1269
+ // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
1270
+ const elementsAllowedInHead = new Set ( [
1271
+ 'title' ,
1272
+ 'base' ,
1273
+ 'link' ,
1274
+ 'style' ,
1275
+ 'meta' ,
1276
+ 'script' ,
1277
+ 'noscript' ,
1278
+ 'template' ,
1279
+ ] )
1280
+
1281
+ function headTagInsertCheck (
1282
+ tags : HtmlTagDescriptor [ ] ,
1283
+ ctx : IndexHtmlTransformContext ,
1284
+ ) {
1285
+ if ( ! tags . length ) return
1286
+ const { logger } = ctx . server ?. config || { }
1287
+ const disallowedTags = tags . filter (
1288
+ ( tagDescriptor ) => ! elementsAllowedInHead . has ( tagDescriptor . tag ) ,
1289
+ )
1290
+
1291
+ if ( disallowedTags . length ) {
1292
+ const dedupedTags = unique (
1293
+ disallowedTags . map ( ( tagDescriptor ) => `<${ tagDescriptor . tag } >` ) ,
1294
+ )
1295
+ logger ?. warn (
1296
+ colors . yellow (
1297
+ colors . bold (
1298
+ `[${ dedupedTags . join ( ',' ) } ] can not be used inside the <head> Element, please check the 'injectTo' value` ,
1299
+ ) ,
1300
+ ) ,
1301
+ )
1302
+ }
1303
+ }
1304
+
1268
1305
export async function applyHtmlTransforms (
1269
1306
html : string ,
1270
1307
hooks : IndexHtmlTransformHook [ ] ,
@@ -1306,7 +1343,7 @@ export async function applyHtmlTransforms(
1306
1343
; ( headPrependTags ??= [ ] ) . push ( tag )
1307
1344
}
1308
1345
}
1309
-
1346
+ headTagInsertCheck ( [ ... ( headTags || [ ] ) , ... ( headPrependTags || [ ] ) ] , ctx )
1310
1347
if ( headPrependTags ) html = injectToHead ( html , headPrependTags , true )
1311
1348
if ( headTags ) html = injectToHead ( html , headTags )
1312
1349
if ( bodyPrependTags ) html = injectToBody ( html , bodyPrependTags , true )
0 commit comments