Skip to content

Commit

Permalink
feat: add headTagInsertCheck warning (#16555)
Browse files Browse the repository at this point in the history
Co-authored-by: pengbo43 <[email protected]>
  • Loading branch information
PengBoUESTC and pengbo43 authored May 29, 2024
1 parent 9853190 commit 9f02a9f
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
partialEncodeURIPath,
processSrcSet,
removeLeadingSlash,
unique,
urlCanParse,
} from '../utils'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -1265,6 +1266,42 @@ export function resolveHtmlTransforms(
return [preHooks, normalHooks, postHooks]
}

// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head#see_also
const elementsAllowedInHead = new Set([
'title',
'base',
'link',
'style',
'meta',
'script',
'noscript',
'template',
])

function headTagInsertCheck(
tags: HtmlTagDescriptor[],
ctx: IndexHtmlTransformContext,
) {
if (!tags.length) return
const { logger } = ctx.server?.config || {}
const disallowedTags = tags.filter(
(tagDescriptor) => !elementsAllowedInHead.has(tagDescriptor.tag),
)

if (disallowedTags.length) {
const dedupedTags = unique(
disallowedTags.map((tagDescriptor) => `<${tagDescriptor.tag}>`),
)
logger?.warn(
colors.yellow(
colors.bold(
`[${dedupedTags.join(',')}] can not be used inside the <head> Element, please check the 'injectTo' value`,
),
),
)
}
}

export async function applyHtmlTransforms(
html: string,
hooks: IndexHtmlTransformHook[],
Expand Down Expand Up @@ -1306,7 +1343,7 @@ export async function applyHtmlTransforms(
;(headPrependTags ??= []).push(tag)
}
}

headTagInsertCheck([...(headTags || []), ...(headPrependTags || [])], ctx)
if (headPrependTags) html = injectToHead(html, headPrependTags, true)
if (headTags) html = injectToHead(html, headTags)
if (bodyPrependTags) html = injectToBody(html, bodyPrependTags, true)
Expand Down

0 comments on commit 9f02a9f

Please sign in to comment.