-
Notifications
You must be signed in to change notification settings - Fork 26
Caching SVG doesn't cache CSS class name and Viewbox settings #67
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
base: main
Are you sure you want to change the base?
Conversation
Honestly, I'm pretty confident about this PR but it shouldn't be merged yet. I'm not sure how best to test it. The unit tests all pass but I failed to create new unit tests as I couldn't work out how to mock the AppCache correctly. Any ideas how to do this? Do you also have an existing method of testing this on a 'real' site, or should I work something out for that? |
There was a problem hiding this 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 caching bug where SVG content was cached with CSS classes and ViewBox settings, causing subsequent renders of the same SVG to incorrectly inherit attributes from previous usages. The solution separates the caching of file contents from attribute processing.
- Splits SVG caching into two levels: raw file content and processed content with attributes
- Extracts cache key calculation and configuration fetching into separate methods
- Refactors attribute processing into a dedicated method for better separation of concerns
Comments suppressed due to low confidence (1)
Our.Umbraco.TagHelpers/InlineSvgTagHelper.cs:244
- [nitpick] The method name 'ParseAndSetAttrs' uses an abbreviation. Consider renaming to 'ParseAndSetAttributes' for better readability and consistency with the rest of the codebase.
private string ParseAndSetAttrs (string cleanedFileContents) {
=> $"SvgWithAttributes ({CssClass?.GetHashCode()}_{cleanedFileContents.GetHashCode()})"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using GetHashCode() for cache keys can cause collisions since GetHashCode() is not guaranteed to be unique or consistent across application restarts. Consider using a more deterministic approach like combining the actual values or using a cryptographic hash.
=> $"SvgWithAttributes ({CssClass?.GetHashCode()}_{cleanedFileContents.GetHashCode()})"; | |
{ | |
// Use actual values or a deterministic hash for the cache key | |
var cssClassValue = CssClass ?? string.Empty; | |
var combined = cssClassValue + "|" + cleanedFileContents; | |
var hash = ComputeSHA256Hash(combined); | |
return $"SvgWithAttributes ({hash})"; | |
} | |
private static string ComputeSHA256Hash(string input) | |
{ | |
using (var sha256 = SHA256.Create()) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(input); | |
var hashBytes = sha256.ComputeHash(bytes); | |
var sb = new StringBuilder(); | |
foreach (var b in hashBytes) | |
{ | |
sb.Append(b.ToString("x2")); | |
} | |
return sb.ToString(); | |
} | |
} |
Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There is an issue where caching an SVG with a class name (or Viewbox setting) causes the class name to be contained in the cache. If outputting the same SVG later includes the class name from the first usage.
This PR splits the caching so that the file read is independently cached from the Css class name and Viewbox settings.