JoshReedSchramm
JoshReedSchramm

Reputation: 2761

Javascript is cached in development mode with asset pipeline

I recently upgraded my application to rails 3.1 and generally everything seems to be working but one thing is driving me insane.

I have 2 main js files, we'll call them, application.js and main.js.

application.js has my manifest stuff in it and is loading in main.js. That's working fine. My problem is when i'm in development mode and i make a change to main.js, then refresh the page the site doesn't pick up the change. In order to pull in the change I have restart the rails server.

I have debug mode turned on in development, but I'm wondering if there's another setting i'm missing?

Anyone run into this before?

Upvotes: 16

Views: 8488

Answers (9)

Jimmy Truong
Jimmy Truong

Reputation: 81

We probably different root causes. But same symptom, and here's how I fixed my issue.

TLDR: If [main-application-file]-[hash-id].js is corrupt, then it won't update. So I had to do full removal of the /public/assets directory! Then did a rebuild of my app and restarted the rails server.

Details

The root cause for me was that my application-[hash-id].js was corrupted and wouldn't update! Even though when forced a rebuild I saw that app/assets/builds/application.js was picking up my changes. So I had to work backwards from the browser to see what files were being served from the Rails server and locate those. So when I saw the build code in application.js and public build code application-[hash-id].js were de-synced. I thought a good old deletion would do the trick!

Upvotes: 0

Shiva
Shiva

Reputation: 12514

If above mentioned answers fail in your context..

make sure the caching is done by rails.. some times it does not detect changes in asset and serve the older cached version..

use rails c >> Rails.cache.clear
or simply delete the caches in tmp dir

[project_dir/tmp/cache/assets"]

Upvotes: 0

curiouscode
curiouscode

Reputation: 409

I had the same issue and found that removing the asset digests in development fixed the issue. Make sure you set it to false in developmen.rb:

config.assets.digest = false

Upvotes: 4

CodeThug
CodeThug

Reputation: 3192

Also note that if you enable config.threadsafe, it will turn cache_classes on. So if your config/environments/development.rb file contains the following:

config.cache_classes = false
config.threadsafe!

Then you are turning off cache_classes and then turning it right back on. You will need to either comment out config.threadsafe (if you don't need it) like this:

config.cache_classes = false
# config.threadsafe!

Or, if you need threadsafe, reverse the order of these two configurations so that config_classes is really turned off:

config.threadsafe!
config.cache_classes = false

For more information, see http://tenderlovemaking.com/2012/06/18/removing-config-threadsafe.html

Upvotes: 2

Tom Bartel
Tom Bartel

Reputation: 56

I had the same issue, config.action_controller.perform_caching set properly. I also use Heroku and precompile assets for the Push to Heroku using: RAILS_ENV=production bundle exec rake assets:precompile After the push, when I started new work, I forgot to remove the precompiled assets using: sudo rm -r public/assets/*

So, no matter what I did to any of my .js files, they changes were not showing up.

Upvotes: 1

David D.
David D.

Reputation: 126

Opening Chrome in incognito mode worked best for me. No need to open and close a tab. In incognito mode, chrome doesn't cache javascript.

Upvotes: 2

Edward Anderson
Edward Anderson

Reputation: 13916

I had the same issue, but config.action_controller.perform_caching was already set to false.

For me and another guy I was working with, the problem was that Chrome was caching the page despite the settings in Rails.

To fix it, we just closed the tab, opened a new tab, and visited the site again.

Upvotes: 15

Matthew Clark
Matthew Clark

Reputation: 1965

I had the same problem, and I finally stumbled across something in my development.rb. I had config.action_controller.perform_caching set to true, and changing it to false solved the problem.

Upvotes: 2

Richard Hulse
Richard Hulse

Reputation: 10493

Have a look at your development log and see what it says when the application.js is served.

It should look something like this for a normal request (you browsed to a page):

Started GET "/assets/application.js" for 127.0.0.1 at Fri Sep 30 12:13:27 +1300 2011
Served asset /application.css - 304 Not Modified (2ms)

If not you may have not set the pipeline options correctly. One of the production settings might be in the wrong place. Section 9 of the asset pipeline guide has a checklist of correct settings for a migrated app.

Upvotes: 1

Related Questions