Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/Mvc/Mvc.TagHelpers/src/ScriptTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
output.CopyHtmlAttribute(SrcAttributeName, context);
}

if (Type != null)
{
output.CopyHtmlAttribute(TypeAttributeName, context);
}

// If there's no "src" attribute in output.Attributes this will noop.
ProcessUrlAttribute(SrcAttributeName, output);

Expand Down
38 changes: 38 additions & 0 deletions src/Mvc/Mvc.TagHelpers/test/ScriptTagHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,44 @@ public void RenderScriptTags_GlobbedSrc_WithFileVersion_WithStaticAssets()
Assert.Equal(expectedContent, content);
}

[Theory]
[InlineData("~/js/site.js")]
[InlineData("/approot/js/site.js")]
public void RenderScriptTags_PathBase_WithFileVersion_UsingResourceCollection_PreservesModule(string path)
{
// Arrange
var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList
{
new TagHelperAttribute("src", path),
new TagHelperAttribute("type", "module"),
new TagHelperAttribute("asp-append-version", "true")
});
var output = MakeTagHelperOutput("script", attributes: new TagHelperAttributeList());

var urlHelperFactory = MakeUrlHelperFactory(value =>
{
return value.StartsWith("~/", StringComparison.Ordinal) ?
value.Replace("~/", "/approot/") :
value;
});

var helper = GetHelper(urlHelperFactory: urlHelperFactory);
helper.ViewContext.HttpContext.SetEndpoint(CreateEndpoint());
helper.ViewContext.HttpContext.Request.PathBase = "/approot";
helper.Src = path;
helper.Type = "module";
helper.AppendVersion = true;

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

// Assert
Assert.Equal("script", output.TagName);
Assert.Equal("module", output.Attributes["type"].Value);
Assert.Equal("/approot/js/site.fingerprint.js", output.Attributes["src"].Value);
}

private static ScriptTagHelper GetHelper(
IWebHostEnvironment hostingEnvironment = null,
IUrlHelperFactory urlHelperFactory = null,
Expand Down