1

I am looking for a way to stop one view in particular from caching in Laravel. I have a website where it requires a special key on every page load to do something via ajax. What my users are doing is clicking 'back' on their browser and its displaying a cached version with the old special key? Meaning they are given a invalid special key error message and they aren't sure what this exactly means.

How can I force it to supply with a fresh copy, giving them a new special key?

VoiD HD
  • 699
  • 2
  • 8
  • 18
  • This is most likely a browser cache issue rather than a view cache issue. You'll need to do something like add `Cache-Control: no-cache, max-age=0, must-revalidate, no-store` headers if you want to prevent the browser from caching that page. – Joe Apr 10 '17 at 15:24

1 Answers1

1

There are multiple ways to do this in Laravel, however it comes down to implementing the Cache-Control headers as per @Joe's comment above.

Make sure the cache headers you use are appropriate for your clients' HTTP version.

You can add the headers to your response within your controller methods:

return response($content)
        ->header('Cache-Control', 'no-cache, no-store, must-revalidate')
        ->header('X-Header-One', 'Header Value')
        ->header('X-Header-Two', 'Header Value');

You can also implement this by registering custom middleware on the relevant route(s), or use a package such as laravel-httpcache

Community
  • 1
  • 1
Joe Niland
  • 885
  • 1
  • 14
  • 31