How to add browser cache in Laravel 5?

I want to add browser caching to my Laravel application.

I used the Elixir version editing tool: https://laravel.com/docs/5.2/elixir#versioning-and-cache-busting

However, according to Google PageSpeed โ€‹โ€‹Insights, the files are still not cached, and I received this message:

Setting the expiration date or maximum age in the HTTP headers for static resources instruct the browser to download previously loaded resources from the local drive, and not over the network.

I think it could be because I need to manually add cache headers?

+6
source share
1 answer

Yes, you need to set Cache-Control and Expires in the HTTP header for static resources so that this Google PageSpeed โ€‹โ€‹message does not display.

Since you are already using the Elixir version control tool, you can safely install Expires from JS / CSS files for 1 week.

The way to do this depends on which web server you are using.

If you are using Apache , you can put the following code in .htaccess or the configuration file of your virtual website.

 <FilesMatch "\.(js|css)$"> ExpiresActive On ExpiresDefault "access plus 1 weeks" </FilesMatch> 

Be sure to enable the mod_expires Apache module!

With the same syntax, you can configure caching rules for .html, .jpg, .png, etc. files to speed up page loading.

If you are using nginx, there are similar ways to solve this problem, you can follow this tutorial

+7
source

All Articles