@@ -767,11 +767,6 @@ export async function applyHtmlTransforms(
767
767
hooks : IndexHtmlTransformHook [ ] ,
768
768
ctx : IndexHtmlTransformContext
769
769
) : Promise < string > {
770
- const headTags : HtmlTagDescriptor [ ] = [ ]
771
- const headPrependTags : HtmlTagDescriptor [ ] = [ ]
772
- const bodyTags : HtmlTagDescriptor [ ] = [ ]
773
- const bodyPrependTags : HtmlTagDescriptor [ ] = [ ]
774
-
775
770
for ( const hook of hooks ) {
776
771
const res = await hook ( html , ctx )
777
772
if ( ! res ) {
@@ -787,6 +782,12 @@ export async function applyHtmlTransforms(
787
782
html = res . html || html
788
783
tags = res . tags
789
784
}
785
+
786
+ const headTags : HtmlTagDescriptor [ ] = [ ]
787
+ const headPrependTags : HtmlTagDescriptor [ ] = [ ]
788
+ const bodyTags : HtmlTagDescriptor [ ] = [ ]
789
+ const bodyPrependTags : HtmlTagDescriptor [ ] = [ ]
790
+
790
791
for ( const tag of tags ) {
791
792
if ( tag . injectTo === 'body' ) {
792
793
bodyTags . push ( tag )
@@ -798,21 +799,12 @@ export async function applyHtmlTransforms(
798
799
headPrependTags . push ( tag )
799
800
}
800
801
}
801
- }
802
- }
803
802
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
+ }
816
808
}
817
809
818
810
return html
@@ -859,6 +851,8 @@ function injectToHead(
859
851
tags : HtmlTagDescriptor [ ] ,
860
852
prepend = false
861
853
) {
854
+ if ( tags . length === 0 ) return html
855
+
862
856
if ( prepend ) {
863
857
// inject as the first element of head
864
858
if ( headPrependInjectRE . test ( html ) ) {
@@ -893,6 +887,8 @@ function injectToBody(
893
887
tags : HtmlTagDescriptor [ ] ,
894
888
prepend = false
895
889
) {
890
+ if ( tags . length === 0 ) return html
891
+
896
892
if ( prepend ) {
897
893
// inject after body open
898
894
if ( bodyPrependInjectRE . test ( html ) ) {
0 commit comments