Skip to content

Commit 0e1881a

Browse files
Copilotjaviercn
andcommitted
URL encode PackageId in scoped CSS asset paths to fix non-ASCII project names
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
1 parent 26c7de3 commit 0e1881a

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.ScopedCss.targets

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ Integration with static web assets:
233233

234234
<ItemGroup>
235235
<_ScopedCssAppBundleStaticWebAssetCandidate Include="$(_ScopedCssOutputPath)">
236-
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' == 'true'">$(PackageId)#[.{fingerprint}]?.styles.css</RelativePath>
237-
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' != 'true'">$(PackageId).styles.css</RelativePath>
236+
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' == 'true'">$([System.Uri]::EscapeDataString($(PackageId)))#[.{fingerprint}]?.styles.css</RelativePath>
237+
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' != 'true'">$([System.Uri]::EscapeDataString($(PackageId))).styles.css</RelativePath>
238238
</_ScopedCssAppBundleStaticWebAssetCandidate>
239239
<_ScopedCssProjectBundleStaticWebAssetCandidate Include="$(_ScopedCssProjectOutputPath)">
240-
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' == 'true'">$(PackageId)#[.{fingerprint}]!.bundle.scp.css</RelativePath>
241-
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' != 'true'">$(PackageId).bundle.scp.css</RelativePath>
240+
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' == 'true'">$([System.Uri]::EscapeDataString($(PackageId)))#[.{fingerprint}]!.bundle.scp.css</RelativePath>
241+
<RelativePath Condition="'$(StaticWebAssetFingerprintingEnabled)' != 'true'">$([System.Uri]::EscapeDataString($(PackageId))).bundle.scp.css</RelativePath>
242242
</_ScopedCssProjectBundleStaticWebAssetCandidate>
243243
<!-- Adds a Link: <bundle1>; rel="preload", <bundle2>; rel="preload", <bundle3>; rel="preload" -->
244244
<_AddLinkHeaderToAppBundle Include="Append" Condition="'@(_ResolvedScopedCssBundleEndpoints)' != ''">
245245
<UpdateTarget>Header</UpdateTarget>
246246
<Name>Link</Name>
247-
<Value>@(_ResolvedScopedCssBundleEndpoints->'&lt;%(Identity)&gt;; rel="preload"; as="style"', ', ')</Value>
247+
<Value>@(_ResolvedScopedCssBundleEndpoints->'&lt;$([System.Uri]::EscapeDataString(%(Identity)))&gt;; rel="preload"; as="style"', ', ')</Value>
248248
</_AddLinkHeaderToAppBundle>
249249
</ItemGroup>
250250

test/Microsoft.NET.Sdk.StaticWebAssets.Tests/ScopedCssIntegrationTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,5 +617,35 @@ public void RegeneratingScopedCss_ForProjectWithReferences()
617617
text.Should().Contain("background-color: orangered");
618618
text.Should().MatchRegex(""".*@import '_content/ClassLibrary/ClassLibrary\.[a-zA-Z0-9]+\.bundle\.scp\.css.*""");
619619
}
620+
621+
[Fact]
622+
public void Build_GeneratesUrlEncodedLinkHeaderForNonAsciiProjectName()
623+
{
624+
var testAsset = "RazorComponentApp";
625+
var projectDirectory = CreateAspNetSdkTestAsset(testAsset);
626+
627+
// Create a CSS file to trigger scoped CSS processing
628+
var cssFile = Path.Combine(projectDirectory.Path, "Components", "Pages", "Index.razor.css");
629+
Directory.CreateDirectory(Path.GetDirectoryName(cssFile));
630+
File.WriteAllText(cssFile, ".test { color: red; }");
631+
632+
var build = CreateBuildCommand(projectDirectory);
633+
// Set PackageId to contain non-ASCII characters (Chinese characters meaning "project")
634+
ExecuteCommand(build, "/p:PackageId=项目").Should().Pass();
635+
636+
var intermediateOutputPath = build.GetIntermediateDirectory(DefaultTfm, "Debug").ToString();
637+
638+
// Check that the staticwebassets.build.endpoints.json file contains URL-encoded characters
639+
var endpointsFile = Path.Combine(intermediateOutputPath, "staticwebassets.build.endpoints.json");
640+
new FileInfo(endpointsFile).Should().Exist();
641+
642+
var endpointsContent = File.ReadAllText(endpointsFile);
643+
644+
// Verify that the Link header contains URL-encoded characters (%E9%A1%B9%E7%9B%AE is "项目" encoded)
645+
endpointsContent.Should().Contain("%E9%A1%B9%E7%9B%AE");
646+
647+
// Verify it doesn't contain the unencoded characters
648+
endpointsContent.Should().NotContain("项目");
649+
}
620650
}
621651
}

0 commit comments

Comments
 (0)