var self = justin();

Software Developer Teammate

A place to remind my future self of what I've learned and experienced. That means both my successes and failures.


Prevent MVC From Serving Mobile Pages

My client brought in a mobile site developer to "mobilize" our project. So to start, he used the built-in features of MVC to switch between mobile ndain desktop. This means he went through and added .mobile. to the files he was making mobile friendly. For example, Index.cshtml became Index.mobile.cshtml.

When a request comes in, MVC will figure out if it is a mobile device and search for a "mobile" equivalent version of the requested view. That's pretty slick.

The problem appears as soon as you put this behind some CDN, like Akamai.

It's like Russian Roulette

Here's the problem: let's say your site goes live right now. The first person to access www.example.com/awsomePage is a mobile device. No one has requested that page before so Akamai doesn't have it in its cache. So it must pull the content from the origin. Since MVC can detect the mobile requester, it will find the mobile version of awesomePage.cshtml and serve it up.

Akamai then caches the response for that URL (remember, its the mobile version) and sends it to the original requester.

Now another person accesses your site, except this time its from a desktop. Well, Akamai doesn't know its from a desktop and says "oh, I've already cached the output of this URL. I'll just give that to them." Now the requester, which is a desktop, gets a mobile version of the site.

This is a big reason why you see many sites do m.example.com for mobile sites. Because its under a different URL, Akamai won't mix desktop and mobile versions of content.

So, I needed to allow the mobile developer to continue in his workflow (which was just putting the mobile version of the site next to the desktop version) but not let MVC try to serve it up on it's own.

Remove the DispayMode

To figure this out, it downloaded the MVC source code to see what they were doing. Turns out, I can remove the mobile display mode from it's list of available displays like this from the Global.asax:

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        var mobileModeProvider = DisplayModeProvider.Instance.Modes.First(mode =>     
            mode.DisplayModeId == DisplayModeProvider.MobileDisplayModeId);

        if (mobileModeProvider != null)
            DisplayModeProvider.Instance.Modes.Remove(mobileModeProvider);
    }
}
comments powered by Disqus