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

Use plain text in dataurl when possible #1843

Closed
mixtur opened this issue Dec 10, 2021 · 5 comments
Closed

Use plain text in dataurl when possible #1843

mixtur opened this issue Dec 10, 2021 · 5 comments
Labels

Comments

@mixtur
Copy link

mixtur commented Dec 10, 2021

When applying dataurl to a file that only contains valid utf-8 string it make sense, not to use base64.

For example

//main.js
import svg from "./1.svg";
console.log(svg);

//1.svg
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg"><path d="M10 10h100v100H10z"/></svg>

//CLI
npx esbuild main.js --bundle --minify --loader:.svg=dataurl

Now this produces these 197 bytes

(()=>{var g="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIwIiBoZWlnaHQ9IjEyMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJNMTAgMTBoMTAwdjEwMEgxMHoiLz48L3N2Zz4=";console.log(g);})();

But it could be 155:

(()=>{var g='data:image/svg+xml,<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg"><path d="M10 10h100v100H10z"/></svg>';console.log(g);})();
@evanw
Copy link
Owner

evanw commented Dec 12, 2021

This is likely a breaking change since there may be libraries that parse these URLs and only support base64 encoding.

@evanw evanw added the breaking label Dec 12, 2021
@hyrious
Copy link

hyrious commented Dec 12, 2021

To be added, better read this: https://css-tricks.com/probably-dont-base64-svg

Possible code to generate a safe dataurl string for svg (from antfu's blog, which is also from that css tricks post):

// https://bl.ocks.org/jennyknuth/222825e315d45a738ed9d6e04c7a88d0
function encodeSvg(svg: string) {
  return svg.replace('<svg', (~svg.indexOf('xmlns') ? '<svg' : '<svg xmlns="http://www.w3.org/2000/svg"'))
    .replace(/"/g, '\'')
    .replace(/%/g, '%25')
    .replace(/#/g, '%23')
    .replace(/{/g, '%7B')
    .replace(/}/g, '%7D')
    .replace(/</g, '%3C')
    .replace(/>/g, '%3E')
}

const dataUri = `data:image/svg+xml;utf8,${encodeSvg(svg)}`

@mixtur
Copy link
Author

mixtur commented Dec 15, 2021

This is likely a breaking change since there may be libraries that parse these URLs and only support base64 encoding.

There could be a separate loader for this then. Or a separate loader for current behaviour (still breaking, but with ability to fix easily).

@evanw evanw closed this as completed in 895f50c Dec 7, 2022
@evanw
Copy link
Owner

evanw commented Dec 7, 2022

This has now been released! It would be helpful if someone with a real app could try this out and report back if it works or not. With the new behavior, esbuild is very aggressive with not percent-escaping things in order to reduce code size. I have tried to test this in browsers to see what the real requirements for escapes are and it doesn’t seem like many things actually need to be escaped (only \t\n\r# and trailing control/space characters). But it would still be good to get confirmation from someone else that this is working as intended.

@johnnybenson
Copy link

johnnybenson commented Jan 3, 2023

The only issue that I have encountered with this change is with areas of my app using CSS-in-JS with SVG background images.

import SomeBg from 'common/assets/images/SomeBg.svg';
import { css } from 'styled-components';

// Parse Error
css`
  background: url(${SomeBg});
`;

// Works 
css`
  background: url('${SomeBg}');
`;

I don't think it's mandatory per CSS spec to include quotes around the text inside the url() function. Needing it here makes sense. A little tricky to find (mostly because CSS-in-JS is funky) and after adding quotes, it resolved my issues.

Thanks for making the change!

elbywan added a commit to LedgerHQ/ledger-live that referenced this issue Feb 3, 2023
elbywan added a commit to LedgerHQ/ledger-live that referenced this issue Feb 3, 2023
valpinkman pushed a commit to LedgerHQ/ledger-live that referenced this issue Feb 6, 2023
valpinkman pushed a commit to LedgerHQ/ledger-live that referenced this issue Feb 7, 2023
RamyEB pushed a commit to LedgerHQ/ledger-live that referenced this issue Feb 9, 2023
lambertkevin pushed a commit to LedgerHQ/ledger-live that referenced this issue Feb 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants