Skip to content

Conversation

dhh
Copy link
Member

@dhh dhh commented Dec 29, 2021

If you're using a custom asset host resolver, you may need to use separate caches for the preloaded module paths and json returns. Otherwise the first resolved asset host will be used to generate these values, which as then cached for all subsequent requests.

We use this in HEY as a consequence of the following asset host resolver:

 # Disable the CDN only for the iOS app to allow the app's local proxy to intercept network requests for assets config.action_controller.asset_host = Proc.new do |source, request| unless request&.user_agent.to_s.match?(/HEY iOS/) && source =~ /\.(css|js)$/ "https://production.haystack-assets.com" end end

That allows us to call javascript_inline_importmap_tag like so:

javascript_inline_importmap_tag(Rails.application.importmap.to_json( resolver: self, cache_key: "#{platform.ios_app? ? "ios_app" : "all"}_json" ))
@dhh dhh changed the title Allow for custom cache keys Allow for custom cache keys for Importmap::Map#preloaded_module_paths and Importmap::Map#to_json Dec 29, 2021
@dhh dhh merged commit 2ee1e26 into main Dec 29, 2021
@packagethief
Copy link

Driving by with a heads-up: custom cache keys won't be cleared by Importmap::Map#clear_cache.

@dhh
Copy link
Member Author

dhh commented Dec 29, 2021

Good catch. Will fix that.

@dhh
Copy link
Member Author

dhh commented Dec 29, 2021

Fixed in 459f108

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants