Skip to content
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

preprocess-inline-svg does not decode & when inlining SVGs #322

Closed
SizableShrimp opened this issue Aug 18, 2024 · 2 comments · Fixed by #324
Closed

preprocess-inline-svg does not decode & when inlining SVGs #322

SizableShrimp opened this issue Aug 18, 2024 · 2 comments · Fixed by #324
Assignees
Labels
Milestone

Comments

@SizableShrimp
Copy link

When inlining SVGs, preprocess-inline-svg does not decode the HTML entity & into &, which .svelte files support. Svelte files do not decode & into &, so it must be done by this inlining process. This is especially apparent when using a URL in an SVG that needs an ampersand as a query parameter. When inlining, the ampersand is not decoded, so the query is ruined because the URL is resolved including &. Here is a demo SVG:

<?xml version="1.0"?>
<svg width="1" height="1" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <style type="text/css">
      @import url('https://fonts.googleapis.com/css2?family=Zen+Maru+Gothic&amp;display=swap');
    </style>
  </defs>
</svg>

When used in combination with preprocess-inline-svg, you can look in the Network tab of your browser's dev tools to see that the queried URL does not properly have &display=swap, meaning the queried URL & returned data is incorrect.

Here is a simple workaround I found in the time being to circumvent this issue (placed in svelte.config.js):

import * as preprocess_svg from '@svelte-put/preprocess-inline-svg'

function customInlineSvg(sources, inlineConfig) {
  const rSources = preprocess_svg.resolveSources(sources)
  const rConfig = preprocess_svg.resolveInlineSvgConfig(inlineConfig)

  return {
    markup({ content, filename }) {
      const transformed = preprocess_svg.transform(
        content,
        filename,
        rSources,
        rConfig
      )
      // Replace &amp; with & because .svelte files will not decode it for us
      transformed.code = transformed.code.replace(/&amp;/g, '&')
      return transformed
    }
  }
}

Then, use customInlineSvg in place of inlineSvg in the config, like so:

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://kit.svelte.dev/docs/integrations#preprocessors
  // for more information about preprocessors
  preprocess: [
    customInlineSvg([
      { directories: 'static' }
    ]),
    // ...
  ],
  // ...
@vnphanquang
Copy link
Owner

Thank you @SizableShrimp . Will look into this.

@vnphanquang vnphanquang self-assigned this Oct 4, 2024
@github-project-automation github-project-automation bot moved this to Backlog in svelte-put Oct 4, 2024
@vnphanquang vnphanquang added this to the Anytime milestone Oct 4, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in svelte-put Oct 4, 2024
@vnphanquang
Copy link
Owner

Should be fixed in @svelte-put/preprocess-inline-svg@2.1.4. Thanks again for reporting!

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

Successfully merging a pull request may close this issue.

2 participants