vaughan
vaughan

Reputation: 7482

How do I prevent an asset from being cached by the Rails Asset Pipeline in development mode?

I added a custom engine for HAML in config/initializers.

When I visit http://127.0.0.1:3000/assets/page.html it renders the page successfully.

# page.html.haml

!!! 5
%html{:lang => "en"}
  %head
    = stylesheet_link_tag 'application'
    = javascript_include_tag 'application'

The problem is Rails seems to cache the html asset. When I add or remove a file from the application.js manifest it doesn't update until I stop the Rails server and run rake tmp:clear. An alternative is to touch the application.js file. I have config.assets.debug = true set because I prefer to see individual files/folders in Chrome DevTools rather than one big application.js file.

Is there a way to exclude certain assets from being cached during development.

Just to reiterate - its the HTML being cached which is the problem, changes to the manifest are reflected when visiting http://127.0.0.1:3000/assets/application.js.

Upvotes: 1

Views: 372

Answers (1)

David Miani
David Miani

Reputation: 14678

A brute force solution would be to add some depends_on attributes to the haml file:

# page.html.haml
#= depend_on application.js
#= depend_on application.css
!!! 5
%html{:lang => "en"}
  %head
    = stylesheet_link_tag 'application'
    = javascript_include_tag 'application'

This should force the asset to be regenerated when the application.js file is changed.

There are a few cavets with this. The file names should be the names in the source tree. It assumes you can use # for comments in your HAML renderer (I don't know HAML so I'm not sure).I'm also not really sure how this works with a custom engine. Also, a better solution would be to have your custom HAML engine track dependencies itself, but I don't know how to do this.

See the sprockets docs for more details

Upvotes: 2

Related Questions