Skip to content

Rails 8: Considering adopting default config.public_file_server.headers value in production env #569

Open

Description

Current rails-template:

    # Ensure that Rails sets appropriate caching headers on static assets if
    # Rails is serving static assets in production e.g. on Heroku
    #
    # Overview of Cache-control values:
    #
    #     max-age=<seconds>
    #         The maximum amount of time a resource is considered fresh.
    #
    #     s-maxage=<seconds>
    #         Overrides max-age or the Expires header, but only for shared
    #         caches (e.g., proxies). Ignored by private caches.
    #
    #     More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
    #
    # Our Cache-Control header:
    #
    # * It tells all caches (both proxies like Cloudflare and the users web
    #   browser) that the asset can be cached.
    # * It tells shared caches (e.g. Cloudflare) that they can cache it for 365 days
    # * It tells browsers that they should cache for 365 days
    #
    # Cloudflare will respect s-maxage if it is set so change that value if you
    # want Cloudflare to cache differently than then browser.
    #
    config.public_file_server.headers = {
      "Cache-Control" => "public, s-maxage=#{365.days.seconds}, max-age=#{365.days.seconds}"
    }

Rails 8 default:

    config.public_file_server.headers = { "cache-control" => "public, max-age=#{1.year.to_i}" }

Above is a comparison of the config.public_file_server.headers directive we generate in config/initializers/production.rb. The default Rails 8 one is very close but does not set s-maxage as a separate thing. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control for context on the directives.

I vaguely like our current version because it calls out that s-maxage is a thing you can set separately but we default it to the same as maxage and I personally have never changed this in an app and I'm guessing that's the common case.

I'm in favour of just using the Rails 8 default here. Thoughts?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions