BPm
BPm

Reputation: 2994

Rack refresh page / clear cache (all in config.ru)

Please change the title if you want...

I use toto as a blog engine for my website. So it uses Rack, Heroku and all the settings can be put nicely in the file config.ru, which is why I like it since it makes everything simple for me.

I use a Rack plugin: mobile detect to detect mobile devices and then redirect to a different site.

And that's where the problem comes. I tested the redirect request, for some time after the deployment, the redirect only works after I refresh the page, which means, I type in the URL for my website on an iPhone, first try will give me the original site, not the one for mobile, after I hit refresh it gets redirected.

I think it's because of the caching on the server (because even if I clear all cache on my phone, it still happens). So this leads to my question: what Rack function/plugin would let me automatically clear the cache on the server OR force the browser to refresh when a mobile is detected.

I have googled for this solution many times and most of it is for Rails app which is not really for me. How do I just make it work all in the config.ru ?

Upvotes: 2

Views: 1017

Answers (1)

Casper
Casper

Reputation: 34308

It might not be as simple as that. Toto forces caching of all its pages, and Heroku runs Varnish. This means it's not likely the first request will ever even hit your app.

But when you hit refresh on the browser the request probably includes no-cache or something similar, which makes Varnish hit your app, which triggers mobile detect, which then returns the redirect.

The code in Toto which sets the cache is here:
https://github.com/cloudhead/toto/blob/master/lib/toto.rb#L345

You can see that if you run your app in development mode the cache won't be used. That should be a simple test to see if caching is indeed where the problem is.

https://devcenter.heroku.com/articles/config-vars#production_and_development_modes

If that works then you have a simple solution, although not the most efficient one.

In any case I don't think you can cache the page and at the same time offer dynamic redirects. So the page needs to stay uncached in order for mobile detect to work.

Upvotes: 1

Related Questions