FortAwesome / Font-Awesome

The iconic SVG, font, and CSS toolkit
https://fontawesome.com
Other
73.53k stars 12.2k forks source link

Strange/Weird characters on refresh (Self-hosted CSS compiled from SCSS) #17644

Open templeman15 opened 3 years ago

templeman15 commented 3 years ago

Describe the bug On random page loads or refreshing of page strange characters are displayed instead of the font awesome icons. Seems to very closely related to #15010 & #17611. However, it happens when using both fa classes and setting the content manually in css.

There is no trace of FA4 in the project.

To Reproduce

SCSS:

Screen Shot 2021-02-17 at 1 25 31 PM

Expected:

Screen Shot 2021-02-17 at 1 26 01 PM

Actual:

Screen Shot 2021-02-17 at 1 25 53 PM

Expected behavior Expected behavior is that the font awesome icons would display correctly.

Version and implementation Version: Font Awesome Pro 5.14.0 Browser and version: Google Chrome 88.0.4324.150 OS: Mac OS 10.15.7

Bug report checklist

tagliala commented 1 year ago

I did read the update, but with this "fix" does that mean the minified code results in the unicode string (ie. "\f26e")?

No, if you want a fix like that, it is a little bit more complicated at the moment and it will require a new fix after each update, please check the comments here: https://github.com/diowa/ruby3-rails6-bootstrap-heroku/pull/540

lindalinya commented 1 year ago

Hey guys, we don't use font-awesome but we came across your issue while trying to solve this problem with our own library so how you realized sass is decoding the Unicodes in build time and causing broken content to the classes

we work around this by importing the CSS icons files directly to js and skipping the sass-loader.

before: image after: image image

Actually, this case is not from sass-loader but dart-sass. I have looked up many, many issues about this case, which are all over the major open-source warehouses such as sass, sass-loader, and so on.

The source of the problem is not the sass-loader, the sass-loader just calls the sass command, so the source of the problem is the default encoding of dart-sass (you can get this result just with sass-loader and file-loader), which stubbornly encodes characters that do not need to be encoded. So "\xxx" in our code is translated into a garbled character after dart-sass compilation.

Unfortunately, dart-sass could have had an elegant solution to this problem, however, their developers were so stubborn that they believed their default coding could not be changed. They think this effect is so great that they act like bandits and demand that person who asked the question provide stable replication steps for an occasional bug.

Guess the ultimate source of this problem can actually be attributed to the browser, the browser for the CSS encoding guess error, resulting in even if you have added html encoding and CSS charset, the font icon parsing still occurs problems.

The final solution, as this man says, is to Change your sass file to a CSS file. Extract all CSS styles that contain Unicode characters into a CSS file, not a Sass file. The CSS file does not need to go through the sass-loader, so the stubborn dart-sass cannot corrupt it

nsunga commented 1 year ago

Update on my end:

I ended up using a postcss plugin - https://github.com/lcoronelp/postcss-sass-unicode

I will say that its not great, though because this is what looks like is happening:

  1. dart-sass is taking the unicode characters and encoding them to the square things []
  2. then, the postcss plugin takes the square things and then decodes them back to the unicode characters in the final css

So now when the browser loads in the css, it doesnt have to do its decoding for whatever the square [] is - its just reading the value.

Until theres a popular solution, ive just been using the above plugin for now

lindalinya commented 1 year ago

Update on my end:

I ended up using a postcss plugin - https://github.com/lcoronelp/postcss-sass-unicode

I will say that its not great, though because this is what looks like is happening:

  1. dart-sass is taking the unicode characters and encoding them to the square things []
  2. then, the postcss plugin takes the square things and then decodes them back to the unicode characters in the final css

So now when the browser loads in the css, it doesnt have to do its decoding for whatever the square [] is - its just reading the value.

Until theres a popular solution, ive just been using the above plugin for now

Maybe you can try my idea if your font icon related styles are all in a sass file. Just change this sass file to a CSS file to make it escape dart-sass

damirbogdanov commented 1 year ago

Update on my end:

I ended up using a postcss plugin - https://github.com/lcoronelp/postcss-sass-unicode

I will say that its not great, though because this is what looks like is happening:

  1. dart-sass is taking the unicode characters and encoding them to the square things []
  2. then, the postcss plugin takes the square things and then decodes them back to the unicode characters in the final css

So now when the browser loads in the css, it doesnt have to do its decoding for whatever the square [] is - its just reading the value.

Until theres a popular solution, ive just been using the above plugin for now

thank you @nsunga, this approach seemed to help in our case and helped to resolve an issue

vineeth-ktp commented 1 year ago

Issue is often(50% of the time) occurring with Mac Chrome. I have tried adding charset, but it still fails. The icons gets added by CMS and we don't have a way to determine which of them to preload. We are using sass and not with dart as it is not supported by CMS. Is there any other solution suitable here?

tagliala commented 1 year ago

We are using sass and not with dart as it is not supported by CMS

This is supposed to happen with self-hosted css files compiled from Sass, so it should mean that user have the ability to make changes to the toolchain used to compile the css output.

You can:

viniciusdeliz commented 1 year ago

For fellows using webpack and facing this issue, I've realized that while on localhost my stylesheet always had the icons in place. So I found that in the stylesheet of my built application I had the following output:

webpack-fa

So testing a bit of options, I figured that the culprit of my setup was the sass-loader set for .scss files. So on my webpack.config.js for my /\.scss$/ rules, I included the following sassOptions entry in the options object for the sass-loader:

{
    loader: 'sass-loader',
    options: {
        sassOptions: {
            outputStyle: 'expanded',
        },
    },
},

Now the stylesheet output for my build is the following:

webpack-fa-sol (1)

Hopefully this can help someone!