Cache font assets in Rails in production

42 Views Asked by At

I'm running a Rails 7 application with some self-hosted fonts. Everything works well in development, the fonts are successfully cached and returned with 304 HTTP status in development on subsequent requests. However, in production, the fonts are being loaded (200 HTTP status) on every page load/navigation. This causes a noticeable flash of font family change after every page load or navigating to a new page.

(If you want to see this live, you can go here: https://foreverdiary.online/2023/12/26 but you'll have to create an account to see it properly, unfortunately.)

In case it matters, the application is hosted on fly.io, here is the (relevant part of the) fly.toml:

[processes]
  app = "./bin/rails server"
  clock = "bundle exec clockwork config/clock.rb"
  sidekiq = "bundle exec sidekiq"

[http_service]
  processes = ["app"]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 1

[[statics]]
  guest_path = "/rails/public"
  url_prefix = "/"

As I'm not very well versed with caching in Rails, I'm not sure which other files it's necessary to post, so do let me know in the comments if I can elaborate further with more details or more code.

0

There are 0 best solutions below