Skip to content

Fix ScriptTagHelper regression with importmap scripts containing explicit content #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 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 6, 2025

The ScriptTagHelper was incorrectly suppressing <script type="importmap"> tags that contained explicit user-provided content, causing them to render as nothing in ASP.NET Core 9.0.

Problem

When users wrote importmap scripts with explicit content like:

<script type="importmap">
{
  "imports": {
    "jquery": "https://code.jquery.com/jquery.js"
  }
}
</script>

The ScriptTagHelper would completely suppress the output instead of preserving the user's content. This regression broke existing applications that relied on explicit importmap definitions.

Root Cause

The tag helper was being overly aggressive - it processed all script tags with type="importmap" and when no ImportMapDefinition was found in endpoint metadata, it would call output.SuppressOutput() regardless of whether the user intended to use the automatic importmap feature.

Solution

Modified the ScriptTagHelper to only suppress output when there's clear intent to use the automatic importmap functionality:

  • If asp-importmap attribute is present but no definition is found → suppress output (existing behavior preserved)
  • If ImportMap property is explicitly set but null → suppress output (existing behavior preserved)
  • If neither condition is met → let the tag render normally with user content

This allows users to continue using explicit importmap content while preserving all existing functionality for automatic importmap generation.

Testing

Added comprehensive tests to verify:

  • Explicit importmap content is preserved when no asp-importmap attribute is used
  • Output is still suppressed when asp-importmap is used but no definition exists
  • All existing functionality remains intact (51/51 existing tests pass)

Fixes #58973.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] Razor regression in ASP .NET Core 9.0 (script type=importmap and others) Fix ScriptTagHelper regression with importmap scripts containing explicit content Aug 6, 2025
@Copilot Copilot AI requested a review from MackinnonBuck August 6, 2025 17:45
Copilot finished work on behalf of MackinnonBuck August 6, 2025 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Razor regression in ASP .NET Core 9.0 (script type=importmap and others)
2 participants