Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: strange behavior when I try to serve ajax and non-ajax request on same route (Caching)

I registered a GET route for laravel.dev/test. The corresponding controller for the route would distinguish whether the request is ajax or not.

When I type laravel.dev/test on the browser, Laravel detect that it's not an ajax request and uses return View::make() to generate the page. Then Backbone.js code on the page make an ajax request to laravel.dev/test and Laravel uses return Response::json to return a JSON.

It's all fine until when I try to navigate away from the page and then use the browser button to navigate back to laravel.dev/test that it print out the json as the response, which is not what I expect since I'm not making an ajax request.

like image 735
Chris Breslaw Avatar asked Jul 05 '13 11:07

Chris Breslaw


1 Answers

Definitely a caching issue. Just to try and get some results, add this to your controller (ajax and non-ajax) to force-disable caching:

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

And see if chrome still fetches from the cache on the back button.

like image 111
Alexandre Danault Avatar answered Sep 20 '22 02:09

Alexandre Danault