Safari caching pages too heavily, how to add expires header using rails - ruby-on-rails

I'm page caching the majority of my pages and using a dynamic js file to inject the user specific content.
So in my header file I have something like this:
<%= javascript_include_tag '/dynamic_header/current' %>
What this will do is execute the dynamic_header controller show.js.erb view which gets returned alongside the page and handles the injection of the dynamic content.
All well and good until I test under safari. (Firefox works fine)
It seems safari caches this file far too heavily and doesn't notice when it changes, which is every time there is a new flash messages or when a user logs in or out.
Is there an easy way in my controller to add an expires header to this file? Or do people have any other suggestions on how to make safari notice that the cached file has changed.
Thanks.

Normally static files have their modified date appended to the end of them to fix caching issues. Since your file is dynamic you should just add the current time to the end of the path.
This is not supported by javascript_include_tag, so you will need to write the script include tag by hand like so:
<script type="text/javascript" src="/dynamic_header/current?t=<%=Time.now.to_i%>"></script>
Good luck!

Maybe you can add this header:
Cache-Control: no-cache
The best way to do this is probably hooking into rack

Related

Does .html in the end of a URL mean that the webpage is static?

I know it's not a good question to ask, but sometimes I really need to know if a webpage or website is static or not.
Sometimes I see .html extension in many URL, Does that mean that those pages are static?
.html extension means that page contains only front-end code and does not have any server side language included in it (I'm not talking about URL rewriters that adds .html to the end of virtual path).
This does not prevent these things:
Page can load it's content via Ajax depending on inputs, URL params, time of day, etc.
Page can be generated as static HTML page, but still be re-generated from time-to-time.
You can have iFrame in static HTML page that leads to .php file.
Not really, .html does not mean webpage is static. Ajax can be used to load dynamic data in html page.
Also there is no proper method defined to find whether page is static or dynamic.
One way is, you can check requests in Developer Options of browser.
You can read more here.
No.
There is no guarantee of a direct relationship between a thing that looks like a file extension in a URL and how the server handles things behind the scenes.
It might be resolved using basic static file handling rules to a static file with that name.
It might use a tool like mod_write to map the URL onto a server side script with the same name but a different file extension (e.g. if the site used to be made of static files, but was changed to be dynamic with steps taken to keep the URLs unchanged).
It might use a tool like mod_write to map the URL onto a server side script that has no relation to the name of the file but implements the front controller pattern for the whole site.
It might map onto a server side script which looks at the end of the URL to determine what type of data to return the content in (e.g. cars.html and cards.json might both be be handled by the same script, which outputs a list of cars, but it might output it in JSON or HTML depending on the URL).
It might hit a 404 error or a 302 redirect.
It might do any number of other things.
Not always, sometimes it can be a generated page from a Servlet or a PHP script that generates them. you can have a .htaccess rule to add .html to all documents.

Performance in MVC web application

I am struggling to get some performance in my MVC application.I am loading a partial page (popup) which is taking hardly 500ms. But each time the popup loads it also downloads 2 jQuery files as well.
is it possible to use the jQuery from cache or from parent page?
I have attached the image in red which shows 2 additional request to server.
In order to improve the performance you can try with the following approaches:
see if your application server supports GZip and configure the application/server to return the responses always archived in Gzip
Use minified version of JQuery
there are also Packing libraries where you can pack all the imported resources, such as CSS files and JS files, and the browser will do only 1 request per resource type. For instance, in Java we have a library called packtag.
In general, I recommend you using Google Chrome browser and its performance analyzer. It will give you good hints.
In the Bundle config use this code
BundleTable.EnableOptimizations = true;
and also indclude both files in single bundle.
Does the popup use an iframe or does it's content just get added to the DOM of the current page?
If it gets added to the current page you could try just adding the script references to the parent page instead. It might not always be the best idea if the parent page has no need for those two files, but if the parent page also uses the jQuery validation then the popup will be able to use the parent's reference to the script file.
For an iframe I'd suggest looking at Gzip and minification to make the scripts load faster.

CSS include with MVC

So I know there are various ways of doing, however I'd like to know the "proper" way of including a specific CSS dynamically based on the page I am on. I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it, by either evaluating URL or passing a value through the controller for display flag, or just include it within the content page (outside of the head tags)... I'd like to keep it clean and link them all through my site master, but I'd like to be able to evaluate the page I'm on before I include that CSS..... thoughts??
No matter what its going to be something like this:
<% if( someCriteria ) { %>
<stylesheet type="text/css" href="mypath" />
<%} %>
You could wrap it in a helper or whatever but I don't think there can be a best practice or "cleaner" way of doing something this simple.
" I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it"
Another way to look at this is CSS files are cached by the browser so you may as well include it once and be done with. Your visitors may have a slightly longer initial load time but if you keep your CSS files small it will be barely noticeable. There is very little performance benefit by making it dynamic.
If you're using the Spark view engine, you can use the once attribute on your css include. I personally just put everything on the site master and let the browser handle caching.
http://sparkviewengine.com/documentation/expressions#Conditionalattributeonce
Good, bad, or indifferent, the thing I have been doing for years with master pages is include a ContentPlaceHolder in the <head> section of the master page. I can then inject page-specific CSS through that, instead of cluttering my master page with alot of processing logic. I am doing the same thing with my ASP.NET MVC solutions.
What I typically do is add a to the master in the to allow for pages to include things there. More often than not it is scripts rather than stylesheets, but it works for both.
I'd also vote for getting a Html helper in place to handle this so your developers don't need to care about exactly where the stylesheet is loaded from . . .

CSS not updated during debugging ASP.NET MVC application

I am working on a test application based on ASP.NET MVC. I am new to CSS stuff so I am learning that as well.
I have created a master page called "new.master" and created a new css "new.css". I am calling the view from controller as:
return View ("About", "new");
What I am doing is that I am frequently updating the css and master page. So I run the application in debug mode frequently. After sometime I stop seeing the effect of changes in css. When I "view source" in chrome and check the css, it shows an old version.
When I change the name of both master page and css, I start to receive updated views but that works for sometime and then again I stop getting the CSS updates. Have you faced this problem? How can I fix it? (Its terribly annoying!)
Your browser is caching the css. If you force a refresh with F5 that should display any changes.
I think this may be a browser cache issue. In in IE and FF I usually do Ctrl + F5 or Ctrl + Refresh button. Also you can manually clear the cache. For IE you can use the IE Dev Toolbar and for Firefox there is Firebug with both you can clear the cache easily. Not sure about chrome, sorry.
Sayed Ibrahim Hashimi
My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build
I found another case in which this can happen.
We started our application with just plain links to the CSS files, then switched to using bundling. We updated the old <link href="#Url.Content("~/somePath/someStyle.css")" rel="stylesheet" type="text/css" /> to #Styles.Render("~/Styles/bundles/someStyle").
We also use a few stylesheets which are only needed for some pages, and they have their own bundle. Now, for one of them, we forgot to change the link tag to the #Styles.Render statement.
The result was that this CSS got updated now and then (so we thought everything is OK; the style wasn't missing), but we could not influence when the update happens. When I started making changes to the file, I realized that nothing I do (emptying the cache, rebuilding the project, restarting Cassini) will trigger an update. The browser always got a 304 Not modified.
So, if you have these symptoms, check your links.
Please use Ctrl+F5 to refresh your browser.
F5 may give you the same page even if the content is changed, because it may load the page from cache. But Ctrl - F5 forces a cache refresh, and will guarantee that if the content is changed, you will get the new content.
hope this will help you
Thank you
go to chroom developer panel by right click inspect -> then go to Network tap -> tick the Disable cache checkbox (note: this should always be checked) -> Refresh the page by pressing F5.
this work for me, hope this help others who have this issue! :)
It's very possible that css is being cached in your browser, so simply clearch browser cache, it should help
This happened to me when I had a master view that would load partial views depending on which partial view the user wanted to see, but the answer from this page helped, https://forums.asp.net/t/1763494.aspx?applying+CSS+layouts+to+a+partial+view
CSS must be referenced in the of the Html document. As a
consequence you cannot include them in a partial view...because in
such case the css is included in the Html body. Please plce the style
sheet in head of the page that hosts the partial view.
I would have css links in my partial views and any modifications to the css files would never happen, even if I pressed Ctrl-F5 in the browser. I also tried clearing cache contents in the developer tools. I noticed by viewing the source stylesheets that the css files linked by the partial views were not changing, so the fix for me was to move the css links from the partial view to the main view where these partial views were to be held.
I had something similar happening to me.
Then suddenly noticed the bundle was added to the page:
#Styles.Render("/bundles/classifiedDetailCss")
instead of:
#Styles.Render("~/bundles/classifiedDetailCss")
Note the missing ~ character in the first example.
(The first example only worked after building the project, otherwise it would return a cached version even in debug mode.)
This can help solve this issues in both dev and production.
use asp-append-version="true"

Rails and javascript cache

I'll start to develop a new app that uses a lot of heavy js librarys (prototype, scriptaculous, tinymce and so on).
Someone told me to make all the app using ajax, so all the js files will be loaded only once.
My question is, I really must do everything on ajax?
Lets say if I call myapp.com/projects and projects use all those js files, then I click on a "show" link and I'm redirected to myapp.com/projects/1 on this redirect, all js will be reloaded again?
No, your Javascript files will not be loaded again, they will be cached on the client.
But yes, your application will need to check with the server at every page load, the server often responding that the scripts have not changed. By using AJAX, you reduce the number of connections to the server. You can reduce the number of connections by concating all you Javascript files into one.
Note however that AJAX will add some new problems, like forcing you to track memory leaks as your application will never unload its objects if you never reload a new page.
If you are not at ease with Javascript, I strongly suggest sticking with the "old" model of reloading the page everytime. If you have performance issues, you can deal with them later.
Reloading Images, Scripts, etc...
Short answer: yes
Long answer: depends
When you view a page, your browser will request the HTML, once it has the HTML it will start to load external references (images, scripts, etc...). When it goes to request an image or a script, your browser may send a header which says when it got it last and stored it in its cache. The web server may respond with a 304 Not Modified code which tells the browser to use the cached version saving it from downloading it again.
Even if the browser doesn't use these headers, it will still be caching, it just won't know when the cache should expire. When you use the rails helpers to include images and scripts, it will append a number onto the end of the url which is unique to scripts contents. So if you change the contents, a new url will be used forcing the browser to get the updated version.
Use Google's Ajax Libraries API! Google now hosts the most popular js libraries including Prototype, Scriptaculous and jQuery. Once they host a specific version they are committed to hosting that version indefinitely.
There is a small Rails plugin by Ryan Heath at github:
script/plugin install git://github.com/rpheath/google_ajax_libraries_api.git
Then in your views instead of using the default
<%= javascript_include_tag :defaults =>
use this instead:
<%= google_jquery =>
<%= google_prototype =>
<%= google_scriptaculous =>
You can specify versions if you want. Check out Ryan's readme at github for more information.
This way you don't have to bother setting up an asset host (at least not for your standard javascript) and save yourself a truckload of bandwidth!

Resources