Munter / subfont

Command line tool to optimize your webfont loading. Aggressive subsetting based on your font use, self-hosting of Google fonts and preloading
MIT License
1.56k stars 29 forks source link

Option to disable JS preload feature #146

Closed ivoberger closed 3 years ago

ivoberger commented 3 years ago

I'd like an option to disable the JS preload fallback or at least have it changed so it only runs if the browser doesn't natively support preloading (don't know if that's possible, but I'd prefer turning it off entirely anyway). I tried the Netlify plugin for font subsetting and noticed that the JS fallback causes the font files to be loaded twice. You see it in the Dev Tools Network tab on this deploy when doing a hard refresh. image It eliminates most if not all size savings gained from subsetting while adding 2 network requests and at least for my uses the native preloading is well enough supported.

papandreou commented 3 years ago

Hmm, that sucks. We've been talking about removing it altogether, as it's also a lot of extra code to drag around.

For now, the ability to turn it off is exposed via the --browsers switch. If you specify a set of browsers that all support link-rel-preload according to caniuse, it won't be included. https://github.com/Munter/subfont/blob/22d803aa1a1b39ad06a79c80f3171ba6c7625974/lib/subfont.js#L70-L74

For example: subfont --browsers 'chrome 50, firefox 88' ...

ivoberger commented 3 years ago

Are you talking about the extra code that ends up in the website or the extra code in the subfont source? I'm guessing it's about the website code since the js fallback part of the subfont source doesn't look that complicated. My 2c on that would be to make it an opt-in flag (can still be the browsers flag, just use something that doesn't include it by default). So basically make the default browsers list the last 3 versions of Chrome, FF, MS Edge and Safari or something like that. After all the font loading still works without preload, it's just a bit slower and because subset already makes the fonts a lot smaller the impact wouldn't be that big.

2 points if you decide to keep it:

  1. More explicit docs on how to turn off the JS fallback. --browsers flag totally makes sense but I know of at least 1 person who didn't think of that by themselves ;)
  2. If possible change the JS fallback so it only kicks in if the browser doesn't support native preload to avoid the double loading issue since that defeats the purpose of subsetting your fonts in the first place.
papandreou commented 3 years ago

Are you talking about the extra code that ends up in the website or the extra code in the subfont source?

Both, actually. It's complicating both the subfont source code and the resulting web pages (as evidenced by this bug).

I think it's time to cut it out completely. Almost all browsers support proper preload now, and the ones that don't generally don't support font loading anyway.

According to caniusedb-lite, the intersection of supports font-loading and not supports link-rel-preload is:

... So I really don't think it's worth it at this point.

I've opened a PR here: https://github.com/Munter/subfont/pull/147

papandreou commented 3 years ago

Fixed in 6.1.0