3

I'm working on a large asp.net web project that has had a number of different developers/consultants making changes to it over the last few years. I've noticed that depending on the developer, paths to images and other static content may contain the correct casing, all lower case, or something completely random. The browser appears to be making multiple requests for the same asset due to the difference in casing. For example -

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <img src="http://cdn.sstatic.net/stackoverflow/img/sprites.png" />
    <img src="http://cdn.sstatic.net/stackoverflow/Img/sprites.png" />
</body>
</html>

Aside from searching for every image in the project and normalizing the casing, is there anything that can be done here? Perhaps something I can put in the page response headers to tell the browser to ignore casing, etc.

mld
  • 519
  • 6
  • 15
  • 2
    Cleanup your markup so links to the same resource are identical. – Oded Dec 12 '11 at 15:44
  • @Oded - I agree that's the correct thing to do, but the question I'm asking is if there's an alternative. My client would like this cleared up, but they're not going to agree to the amount of effort required to comb through the ~2,500 assets referenced throughout the solutions and the thousands of content pages in the CMS. – mld Dec 12 '11 at 16:05
  • How are these assets stored? Are they all HTML files? – Oded Dec 12 '11 at 16:05
  • Links to images/script/css/etc. exist within web form controls as well as manually entered fields in the CMS (added to the aspx/ascx via the API at runtime). – mld Dec 12 '11 at 16:19
  • Using the HTML Agility Pack you can cleanup the HTML fragments that appear in the CMS (assuming you have access to the backing database) - this can be automated. The remaining controls can't be that much work. – Oded Dec 12 '11 at 16:25
  • An alternative is to override the page `PreRender` of every page and canonicalize the URLs. – Oded Dec 12 '11 at 16:26
  • Isn't this behavior expected as this is a configuration that can be modified in webserver? – thiagoleite Dec 12 '11 at 18:22

1 Answers1

0

Well, the browser (it's not just Chrome that does it, any browser that doesn't is buggy) has to do this because there's no way for it to know that you happen to be using a case-insensitive mapping, so <http://cdn.sstatic.net/stackoverflow/img/sprites.png> and <http://cdn.sstatic.net/stackoverflow/Img/sprites.png> are completely different URIs.

There's a few things you can do.

First find-replace those that are:

  1. Particularly commonly used.
  2. Particularly heavy files.
  3. Particularly commonly mis-spelt.
  4. Not likely to result in you find-replacing something that ruins unrelated code.

Another thing you can do is to force canonicalisation of case in a handler that when invoked for a URI that doesn't match your case-canonicalisation rules, 301's to the form that does. This means that rather than grab 3 different 10kb images you'll grab 1 10kb image and have 2 or 3 redirects of a couple-hundred bytes. That said, below a certain file size then cost of an extra request out-weighs the saving.

Finally you can use a filter (a stream object that Response.Filter is set to, that writes to the previous value of Response.Filter) or code in the PreRender step that scans for local URIs (if you change the case of URIs on other sites you could result in 404s) and outputs them correctly.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251