I have seen on various websites how developers version their css/javascripts files by specifying querystrings similar to:
<head>
<link rel="stylesheet" href="css/style.css?v=1">
<script src="js/helper.js?v=1">
</head>
How is that done? Is it a good practice? I've been searching around but apparently, I'm not looking for the right terms. If it matters, I'm using ASP.NET.
Edit:: I just noticed (via Firebug) that if I "version" my files (?v=1) they will always be loading and will always override the cache. Is there a way around that?
Thanks in advance.
They're not really versioned. We do that because certain browsers won't always request the stylesheets properly (they won't even check for a last modified) so to force them to make a new request, you can bump the number in your html file that references it. It's kind of a hack, but it works.
This helps with caching when you want it to and forcing to download when you don't. Files are cached based on their path. So if the path is the same then it can pull from cache. But if they are different, hence a new version, then it would not use the cache but should pull the new file. At least that is how I have used this.
They are doing this to make caching for the Browsers more reliable. You can add the version manually, and increment it every time you change the file. This way the Browser thinks it's got a new file and downloads it for sure.
I don't know the way how to do this automatically in ASP.NET, Ruby on Rails for example checks the last changed timestamp on the file and adds this as version number to the file. I'm sure you'll be able to do something similar in ASP.NET.
Related
I have a CSS file for my homepage, it's just a simple transparent background image.
When I change something in that file, it does not apply the changes, older version is compiled.
Even if delete that file completely from computer, it still loads it (I saw that within inspect element).
My view has just link to that CSS file and nothing else. It has defined Layout page also.
It's happening to me all the time. If I create some other as source, it works but I get the same problem for that new file also after the initial version.
What could itbe ? I'm fairly new in ASP .NET CORE but it looks like it's storing those files in a server. One time I accidentally opened console in inspect element and got some bad connection error and it worked from that point on.
Sounds like your browser is caching your css file. A common way to force it to use the updated version is to add the attribute asp-append-version="true" to all your link and script tags so that your browser always fetches the most up to date version.
Is it possible to add a url from another domain to the bundling in Microsoft.Web.Optimization?
I want to add a reference to replace the following link:
<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700' rel='stylesheet' type='text/css'>
My code for creating a css bundle which works with local files is as below:
Bundle cssCommon = new Bundle("~/cssCommon", typeof(CssMinify));
cssCommon.AddDirectory("~/content/", "aom.common.*", false);
BundleTable.Bundles.Add(cssCommon);
Actually leaving that content on the other host is probably a better thing to do.
You benefit from the Google's global distribution potentially this means that the font may be 'closer' on the internet and have less lag than your actual web server.
You can download the content in parallel with your main content (each host is a separate set of downloads threads so it won't block your existing content or add to the total transfer time of that content)
You benefit from Google's server resilience and up time.
The end user real world experience may actually be better as a result.
That doesn't really make sense because bundling reduces the number of requests from the website's server. Referencing goodleapis is obviously on another server.
There is the concept of CDNs though, and these can be changed depending on the release, this link has some useful information on using a CDN with Bundling and Minification.
Using ASP.NET 4.0, IIS 7.5.
I have a website engine, I have just implemented a way for this to tell if it's being loaded on mobile and instead of loading Controls\MyControl.ascx it loads Mobile\Controls\MyControl.ascx. This works well for my controls and also my MasterPage.Master file.
What I can't figure out however is how I can do the same with Default.aspx. This needs to be done on the fly programatically as I need to be able to check if it's mobile version. I was thinking of doing something on a pre-init event in globals but not sure if that's the best way.
Note: I don't want to use inline code on Default.aspx and just display different content base on my Mobile flag as my scenario goes one step further by basing the file on customer as well and this would mean having one huge Default.aspx for all customers which wouldn't be manageable.
Changing the default document on the fly is not possible in any practical sense.
Writing to the web.config on the fly to load a mobile version of a default page is quite frankly terrible and not an answer to the true context of your issue. I would feel irresponsible as a developer if I even proposed this as an answer to loading a mobile version of a default page.
I was trying to help you solve your problem and not just answer the base question in the title. As we all know, changing the web.config will restart your application and would not serve as a true solution, as you could not do this and achieve any kind of performance.
Here is the BEST alternative (IMHO) to dealing with mobile browsers.
http://51degrees.codeplex.com/
HTH!
For anyone else looking at this I have found a solution but I am not sure I will implement it as I don't like the idea of updating the web.config file at run time. Using the Microsoft.Web.Administration namespace you can update the server.webServer -> defaultDocument section programmatically. Doing this allows you to change the path to default.aspx and it will load based on the variables you set.
This link should provide more information: http://blogs.msdn.com/b/saurabh_singh/archive/2007/11/24/accessing-iis-7-0-features-programmatically-from-configuration-file-s-c.aspx
Also, the Microsoft.Web.Administration dll isn't available directly in VS so you need to add it from %windir%\syswow64\inetsrv (64bit version).
Can someone give me pointers on how I can minify and combine my CSS as the site I'm working on has performance issues. Also, are external JS and CSS libraries also minified and combined or left as is?
There are several convenient options for .NET, for example:
Chirpy
SquishIt
Cassette
And new in ASP.NET 4.5:
Bundling in ASP.NET 4.5
I'd recommend reading Google's Page Speed and their Closure Compiler Service.
I would also add RequestReduce for .net options. It may be the only runtime bundling option for minifying external resources. Its unique from the other .net options in that it grabs the unminified content via http instead of via the file system. It can also sprite your background images.
I know this is an old question but I thought I will give a better answer (trick).
In visual studio if you do format code (Ctrl + K + D) then css will be formatted nicely for you.
Is the best approach to loading javascript files to put them in an IFrame and then embed the same in asp.net pages? I have read somewhere that this will help to boost page-loading times.
Best is to put script references at the bottom of the page. This ensures that all content is loaded before the scripts. Don't use the iFrame unless needed.
When looking at page load times, it depends on where your code is stored. If it is in the html then it will be reloaded each time the page is loaded. If you move it out to a js file, it will be loaded the first time and cached after that so then you shouldnt have a problem with affecting the load times.
You can use jQuery's $(document).ready to make sure that all the elements are loaded before running any code.
Don't go for the iframe approach. Put the scripts as far away at the bottom of your html as possible and your css as high as possible. Also try to go for unobtrusive javascript if possible. jQuery's great at this so you surely want to take a look into this. The benefit of jQuery is that it's also put on CDN servers which also speeds up performance.
I found these guidelines to be a great help when I was upgrading the performance of one of my former projects at a client: Best Practices for Speeding Up Your Web Site.
Some great tools are also Firebug in combination with YSlow.