FIX: add cache breaker for fonts

This is a temporary fix since fonts may be cached without CORS

A larger scale change needed.

Our long term solution here is to have the SCSS compilation pipeline take
care of this by introducing some SCSS var that can be consumed in the various
stylesheets.

We do not want to write a file on boot
This commit is contained in:
Sam Saffron 2020-09-02 12:17:28 +10:00
parent d49e96c6a3
commit 7b7357147e
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5

View File

@ -311,6 +311,17 @@ module Discourse
# Use discourse-fonts gem to symlink fonts and generate .scss file # Use discourse-fonts gem to symlink fonts and generate .scss file
fonts_path = File.join(config.root, 'public/fonts') fonts_path = File.join(config.root, 'public/fonts')
# TODO cache breaker should be supplied from both the gem
# and the app
# we may want to keep a cache breaker in Discourse in case somehow
# Discourse changes rules for the particular asset
#
# TODO this code should move to SCSS compilation pipeline, we can have the
# font gem inject some variables into the SCSS that it can consume, instead of writing a file
# on boot
font_cache_breaker = "1"
Discourse::Utils.atomic_ln_s(DiscourseFonts.path_for_fonts, fonts_path) Discourse::Utils.atomic_ln_s(DiscourseFonts.path_for_fonts, fonts_path)
File.open(File.join(config.root, 'app/assets/stylesheets/common/fonts.scss'), 'w') do |file| File.open(File.join(config.root, 'app/assets/stylesheets/common/fonts.scss'), 'w') do |file|
DiscourseFonts.fonts.each do |font| DiscourseFonts.fonts.each do |font|
@ -326,7 +337,7 @@ module Discourse
file.write <<~EOF file.write <<~EOF
@font-face { @font-face {
font-family: #{font[:name]}; font-family: #{font[:name]};
src: asset-url("/fonts/#{variant[:filename]}") format("#{variant[:format]}"); src: asset-url("/fonts/#{variant[:filename]}?#{font_cache_breaker}") format("#{variant[:format]}");
font-weight: #{variant[:weight]}; font-weight: #{variant[:weight]};
} }
EOF EOF