Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

carrick's avatar

PageSpeed - Leverage browser caching

I'm using Laravel and testing the project in pagespeed service and I'm getting a low score (64).

One of the issues that the pagespeed service shows is about "Leverage browser caching":

Leverage browser caching
Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.
Leverage browser caching for the following cacheable resources:
https://...ngrok.io/css/jquery-ui.css (expiration not specified)
https://....ngrok.io/img/categories/c1.svg (expiration not specified)
https://....ngrok.io/img/categories/c2.svg (expiration not specified)
...

Someone knows what is necessary to correct this?

0 likes
1 reply
Cronix's avatar
Cronix
Best Answer
Level 67

It's best to do that at the server level, so in your nginx or apache config. The answer depends on which one you're using.

For instance, with nginx you could do it like

location ~* \.(?:css|svg)$ {
  expires 1M;
  add_header Cache-Control "public";
}

where 1M is one month. You could use -1 for forever, etc.

for Apache, something like

<filesMatch ".(css|svg)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>

where 31536000 is the number of seconds until it expires.

Be careful on your cache times if you're not using laravel mix + versioning. Otherwise, if you change the css (for example), the browser won't reload it until the cache expires so new changes won't show up (unless the user clears their cache, or force-refreshes the browser - something you don't want to have to explain to your users)

1 like

Please or to participate in this conversation.