Skip to content

Encapsulate stale_when_importmap_changes method #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -226,14 +226,16 @@ Import your module on the specific page. Note: you'll likely want to use a `cont

## Include a digest of the import map in your ETag

If you're using [ETags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) generated by Rails helpers like `stale?` or `fresh_when`, you need to include the digest of the import map into this calculation. Otherwise your application will return [304](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304) cache responses even when your JavaScript assets have changed. You can avoid this with something like:
If you're using [ETags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) generated by Rails helpers like `stale?` or `fresh_when`, you need to include the digest of the import map into this calculation. Otherwise your application will return [304](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304) cache responses even when your JavaScript assets have changed. You can avoid this using the `stale_when_importmap_changes` method:

```ruby
class ApplicationController < ActionController::Base
etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
stale_when_importmap_changes
end
```

This will add the digest of the importmap to the etag calculation when the request format is HTML.


## Sweeping the cache in development and test

5 changes: 5 additions & 0 deletions app/controllers/importmap/freshness.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Importmap::Freshness
def stale_when_importmap_changes
etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
end
end
8 changes: 7 additions & 1 deletion lib/importmap/engine.rb
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ class Engine < ::Rails::Engine
config.importmap.cache_sweepers = []
config.importmap.rescuable_asset_errors = []

config.autoload_once_paths = %W( #{root}/app/helpers )
config.autoload_once_paths = %W( #{root}/app/helpers #{root}/app/controllers )

initializer "importmap" do |app|
app.importmap = Importmap::Map.new
@@ -48,6 +48,12 @@ class Engine < ::Rails::Engine
end
end

initializer "importmap.concerns" do
ActiveSupport.on_load(:action_controller_base) do
extend Importmap::Freshness
end
end

initializer "importmap.helpers" do
ActiveSupport.on_load(:action_controller_base) do
helper Importmap::ImportmapTagsHelper