Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly "config.assets.debug" setting does?

I have started development of simple rails application. After several hours work I have notices that somehow the deleted css is still applied to the web pages.

In order to fix the issue I executed the following actions several times:

  1. stop/start server
  2. use rails server
  3. use torquebox server
  4. delete browser cache

but nothing changes. It was very strange - the new css definitions were applied, but those that I have deleted were still there. So, I gave up and decided to create new project.

I have setup the new project (its scaffold is the same as the first one) and when I open one of the views, the styles from the old project were applied too. I have decided to look again into http://guides.rubyonrails.org/asset_pipeline.html and find out that setting

#Expands the lines which load the assets
config.assets.debug = false

solves the issue. But what is this option doing exactly? Why the old projects css were applied when this was true?

like image 372
gotqn Avatar asked May 03 '13 11:05

gotqn


2 Answers

This option's effect is well described in this post, but I'll summarize it here as well. The value of changing config.assets.debug lies in a compromise between page load time in development and ease of debugging.

Basically:

config.assets.debug = true: assets are served individually, organized just as you see them in development. Preprocessed languages like SASS or CoffeeScript will still show up as their target languages (i.e., CSS and JS, respectively).

config.assets.debug = false: assets are bundled into files like application.css and application.js. Error stack traces will likely not have the correct line number any more and it is harder to map those back to your original code.

like image 136
bosgood Avatar answered Oct 23 '22 04:10

bosgood


If you get to this web page, there is a possibility you are here because you are using the Rails Asset Pipeline and you made changes to one of the javascript files and reloaded the page and the change is not reflected when you search in the Sources tab in Chrome.

As stated above, config.assets.debug = false prompts the Sprockets gem to bundle all the individual javascript and css files into one application.js and application.css respectively. Also Sprockets runs the the SASS and CoffeeScript (if you did not use --skip-coffee) preprocessors on all associated files to generate css and javascript files that the browser can understand.

One important note is the following. Ruby Guides says this about debug = false:

Assets are cached on the first request after the server is started. If any of the files in the manifest have changed between requests, the server responds with a new compiled file.

This means if you do not change the css or javascript files between requests, then a cache will be used. As soon as you change a file, the cache is invalidated and a new cache is created for subsequent requests.

Consequently, if you made changes to a javascript file and the change is not reflected on page reload, it has nothing to do with this option config.assets.debug.

There is this other option called config.action_controller.perform_caching.

But by default this option defaults to false in development. That is, by default, caching is only enabled in your production environment. And in current versions, Rails only ships with Fragment Caching by default. You have to install separate gems for Page and Action caching.

Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in. But then again, cache fragments will also be expired when the view fragment changes (e.g., the HTML in the view changes).

So the question remains why is the change of your javascript not reflected? The answer is Google Chrome, the browser itself, is caching the page despite your Rails settings. To remove the cache, close the current tab, open a new tab, and visit the site again.

like image 38
Daniel Viglione Avatar answered Oct 23 '22 04:10

Daniel Viglione