Skip to content

Fix ScriptTagHelper regression by checking for existing content before processing importmaps #63155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/Mvc/Mvc.TagHelpers/src/ScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,25 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
if (importMap == null)
{
// No importmap found, nothing to do.
output.SuppressOutput();
// No importmap found. Only suppress output if this was intended to be
// an automatically generated importmap (i.e., when asp-importmap was used).
// If the user provided explicit content without asp-importmap, let it render as-is.
if (ImportMap != null || context.AllAttributes.ContainsName(ImportMapAttributeName))
{
output.SuppressOutput();
return;
}
// Let the tag render as-is by continuing with normal processing
// Don't return here, let normal attribute copying happen
}
else
{
output.TagName = "script";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("type", "importmap");
output.Content.SetHtmlContent(importMap.ToString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right fix.

If the goal is to do nothing if there is already content on the script, then what should happen is:

  • Call var originalContent = await output.GetChildContentAsync().
  • Check for content.IsEmptyOrWhitespace
  • Set the import map only in that situation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we still want to process all <script type="importmap"></script> but the fix is only to avoid replacing existing content if there's already data the developer put it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 15776b3. I've implemented the approach you suggested:

  1. Call await output.GetChildContentAsync() to get existing content
  2. Check if content is empty or whitespace using IsEmptyOrWhiteSpace
  3. If content exists, preserve it and return early
  4. If no content exists, apply the normal importmap logic

The fix now properly processes all <script type="importmap"></script> tags but avoids replacing existing content when developers have provided their own importmap data. I've maintained backward compatibility by keeping the original Process method that calls the new ProcessAsync method.

return;
}

output.TagName = "script";
output.TagMode = TagMode.StartTagAndEndTag;
output.Attributes.SetAttribute("type", "importmap");
output.Content.SetHtmlContent(importMap.ToString());
return;
}

// Pass through attribute that is also a well-known HTML attribute.
Expand Down
53 changes: 53 additions & 0 deletions src/Mvc/Mvc.TagHelpers/test/ScriptTagHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,59 @@ public void ScriptTagHelper_RendersImportMap_FromEndpoint()
Assert.Equal(importMap.ToJson(), output.Content.GetContent());
}

[Fact]
public void ScriptTagHelper_PreservesExplicitImportMapContent_WhenNoImportMapDefinition()
{
// Arrange - this simulates the user's scenario where they provide explicit importmap content
// without using asp-importmap attribute
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList
{
new TagHelperAttribute("type", "importmap"),
});

var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

var helper = GetHelper();
helper.Type = "importmap";
// No endpoint with ImportMapDefinition and no asp-importmap attribute
// This should NOT suppress the output, allowing user content to render

// Act
helper.Process(context, output);

// Assert
Assert.Equal("script", output.TagName); // Tag should not be suppressed
Assert.Equal("importmap", output.Attributes["type"].Value);
// The output should not be suppressed, allowing user's explicit content to render
Assert.False(output.IsContentModified); // Content should remain as user provided
}

[Fact]
public void ScriptTagHelper_SuppressesOutput_WhenAspImportMapAttributeUsedButNoDefinition()
{
// Arrange - this simulates using asp-importmap attribute but having no ImportMapDefinition
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList
{
new TagHelperAttribute("type", "importmap"),
new TagHelperAttribute("asp-importmap", null), // asp-importmap used but no value
});

var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

var helper = GetHelper();
helper.Type = "importmap";
// No endpoint with ImportMapDefinition but asp-importmap attribute is present
// This should suppress the output since it was intended to be auto-generated

// Act
helper.Process(context, output);

// Assert - output should be suppressed when asp-importmap is used but no definition found
Assert.Null(output.TagName); // Tag should be suppressed
}

private Endpoint CreateEndpoint(ImportMapDefinition importMap = null)
{
return new Endpoint(
Expand Down
Loading