Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

drpeck
Copy link
Contributor

@drpeck drpeck commented Sep 29, 2023

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.

@drpeck
Copy link
Contributor Author

drpeck commented Sep 29, 2023

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?

@warrenbuckley warrenbuckley linked an issue Aug 5, 2025 that may be closed by this pull request
@warrenbuckley warrenbuckley requested a review from Copilot August 6, 2025 08:13
Copy link

@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 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) {

Comment on lines +105 to +106
=> $"SvgWithAttributes ({CssClass?.GetHashCode()}_{cleanedFileContents.GetHashCode()})";

Copy link
Preview

Copilot AI Aug 6, 2025

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.

Suggested change
=> $"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.

warrenbuckley and others added 3 commits August 6, 2025 09:25
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>
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.

our-svg caches the CSS class
2 participants