Skip to content

Fix ContentTypeProvider to respect custom content-type mappings precedence #50153

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 9, 2025

This PR fixes an issue where custom content-type mappings were being ignored in favor of built-in mappings in the Static Web Assets ContentTypeProvider.

Problem

In .NET SDK versions 9.0.300+, the ContentTypeProvider was prioritizing built-in mappings over custom mappings when both existed for the same file pattern. This prevented users from overriding default cache behavior for common file types like *.html and *.js.

The issue occurred in the TryGetMapping method where built-in mappings were checked first:

if (_builtInMappings.TryGetValue(match.Pattern, out mapping) || _customMappings.TryGetValue(match.Pattern, out mapping))

Since built-in mappings for *.html and *.js have Cache = null, they would always win over custom mappings that might specify cache headers like "no-cache" or "max-age=3600".

Solution

The fix swaps the lookup order to check custom mappings first:

if (_customMappings.TryGetValue(match.Pattern, out mapping) || _builtInMappings.TryGetValue(match.Pattern, out mapping))

This minimal one-line change ensures that:

  • Custom mappings take precedence over built-in mappings when both exist
  • User configuration properly overrides defaults as expected
  • Built-in mappings still serve as fallbacks when no custom mapping exists
  • No breaking changes to existing functionality

Testing

Added comprehensive regression tests to verify:

  • Custom *.html mappings with cache settings override built-in mappings
  • Custom *.js mappings with cache settings override built-in mappings
  • Custom mappings work correctly for compressed files (.gz)
  • All existing functionality continues to work as before

This restores the expected behavior from SDK versions 9.0.101–9.0.205 where user configuration properly overrode built-in defaults.

Fixes #50152.


💡 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.

@Copilot Copilot AI changed the title [WIP] [Static Web Assets] Respect custom content-type mappings Fix ContentTypeProvider to respect custom content-type mappings precedence Aug 9, 2025
@Copilot Copilot AI requested a review from javiercn August 9, 2025 15:00
Copilot finished work on behalf of javiercn August 9, 2025 15:00
@javiercn javiercn marked this pull request as ready for review August 10, 2025 00:22
@Copilot Copilot AI review requested due to automatic review settings August 10, 2025 00:22
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a precedence issue in the Static Web Assets ContentTypeProvider where built-in content-type mappings were overriding custom user-defined mappings. The fix ensures custom mappings take priority over built-in defaults, allowing users to properly override cache behavior for file types like HTML and JavaScript.

  • Swapped lookup order in TryGetMapping to check custom mappings before built-in mappings
  • Added comprehensive regression tests for HTML, JavaScript, and compressed file scenarios
  • Restored expected behavior where user configuration overrides built-in defaults

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/StaticWebAssetsSdk/Tasks/Data/ContentTypeProvider.cs Fixed mapping precedence by checking custom mappings first in TryGetMapping method
test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/ContentTypeProviderTests.cs Added three test cases to verify custom mappings override built-in mappings for HTML, JS, and compressed files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Static Web Assets] Respect custom content-type mappings
2 participants