Skip to content

Commit 649c7f6

Browse files
Mencisapphi-red
andauthoredJun 14, 2022
fix: sequential injection of tags in transformIndexHtml (#5851) (#6901)
Co-authored-by: sapphi-red <green@sapphi.red>
1 parent d7beaeb commit 649c7f6

File tree

1 file changed

+15
-19
lines changed
  • packages/vite/src/node/plugins

1 file changed

+15
-19
lines changed
 

‎packages/vite/src/node/plugins/html.ts

+15-19
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,6 @@ export async function applyHtmlTransforms(
767767
hooks: IndexHtmlTransformHook[],
768768
ctx: IndexHtmlTransformContext
769769
): Promise<string> {
770-
const headTags: HtmlTagDescriptor[] = []
771-
const headPrependTags: HtmlTagDescriptor[] = []
772-
const bodyTags: HtmlTagDescriptor[] = []
773-
const bodyPrependTags: HtmlTagDescriptor[] = []
774-
775770
for (const hook of hooks) {
776771
const res = await hook(html, ctx)
777772
if (!res) {
@@ -787,6 +782,12 @@ export async function applyHtmlTransforms(
787782
html = res.html || html
788783
tags = res.tags
789784
}
785+
786+
const headTags: HtmlTagDescriptor[] = []
787+
const headPrependTags: HtmlTagDescriptor[] = []
788+
const bodyTags: HtmlTagDescriptor[] = []
789+
const bodyPrependTags: HtmlTagDescriptor[] = []
790+
790791
for (const tag of tags) {
791792
if (tag.injectTo === 'body') {
792793
bodyTags.push(tag)
@@ -798,21 +799,12 @@ export async function applyHtmlTransforms(
798799
headPrependTags.push(tag)
799800
}
800801
}
801-
}
802-
}
803802

804-
// inject tags
805-
if (headPrependTags.length) {
806-
html = injectToHead(html, headPrependTags, true)
807-
}
808-
if (headTags.length) {
809-
html = injectToHead(html, headTags)
810-
}
811-
if (bodyPrependTags.length) {
812-
html = injectToBody(html, bodyPrependTags, true)
813-
}
814-
if (bodyTags.length) {
815-
html = injectToBody(html, bodyTags)
803+
html = injectToHead(html, headPrependTags, true)
804+
html = injectToHead(html, headTags)
805+
html = injectToBody(html, bodyPrependTags, true)
806+
html = injectToBody(html, bodyTags)
807+
}
816808
}
817809

818810
return html
@@ -859,6 +851,8 @@ function injectToHead(
859851
tags: HtmlTagDescriptor[],
860852
prepend = false
861853
) {
854+
if (tags.length === 0) return html
855+
862856
if (prepend) {
863857
// inject as the first element of head
864858
if (headPrependInjectRE.test(html)) {
@@ -893,6 +887,8 @@ function injectToBody(
893887
tags: HtmlTagDescriptor[],
894888
prepend = false
895889
) {
890+
if (tags.length === 0) return html
891+
896892
if (prepend) {
897893
// inject after body open
898894
if (bodyPrependInjectRE.test(html)) {

0 commit comments

Comments
 (0)
Please sign in to comment.