Leverage browser caching for 3rd party JS

Leverage browser caching for 3rd party JS

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Leverage Browser Caching for 3rd Party JS

You have correctly configured your Apache server to cache static assets like images, fonts, and your own CSS and JavaScript files for a month. Your httpd.conf settings are solid:

ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"

However, Pagespeed Insights is flagging your external dependencies—Facebook SDK, Google APIs, Twitter widgets, and Analytics—with a warning about missing expiry headers. These files are currently being re-downloaded on every visit because the browser receives no instruction to keep them locally. You cannot simply extend the generic ExpiresDefault to cover these; doing so would force your browser to cache third-party scripts indefinitely, even when the provider updates them silently.

The core conflict is that third-party providers change their code frequently to patch bugs or introduce new features. If you set a hard expiry of "1 month" on a script that hasn't changed, the browser serves the old version from disk. If the script has changed and your user loads it after a month, they get the outdated code without realizing it. This breaks functionality and can cause security issues if a vulnerability was patched in the new version but not received by your users.

The standard solution is to use cache-busting via query strings. You need to force the browser to treat these URLs as unique resources every time the underlying file changes, while still allowing them to be cached for a short window if they haven't changed.

In your HTML or Blade templates, append a version number or a timestamp to the src attribute of your external script tags:

<!-- Facebook SDK -->
<script src="https://connect.facebook.net/en_US/sdk.js?v=1234567890"></script>

<!-- Google Analytics -->
<script src="https://www.google-analytics.com/analytics.js?v=timestamp_{{ time() }}&jid=1234567890"></script>

This technique works because the URL changes. Even if the content of sdk.js hasn't changed, the browser sees a new URL and fetches it from the network. Once fetched, it caches it. If the provider updates the file tomorrow, they usually also change the version number in the script itself or you update your own version parameter.

For frameworks like Laravel, you can automate this by defining custom asset paths or using middleware to inject cache-busting tokens into specific third-party scripts during development and production builds. However, be aware that some providers, like Google Analytics, might invalidate their cache aggressively anyway, rendering the query string less critical for them specifically, but still good practice.

A more advanced approach involves leveraging ETag and Last-Modified headers. Modern browsers are smart enough to check these headers before downloading a resource if you send a Cache-Control: max-age=31536000, public header alongside the query string strategy. This tells the browser: "Keep this forever, but only if the file hasn't changed." If the server responds with a 304 Not Modified, the browser uses its local copy without hitting the network.

There is a trade-off here regarding control. By relying on the provider's change detection (via version numbers or ETags), you lose the ability to force a hard refresh of a third-party script simply by changing your server config. You must wait for the provider to update their assets or manually manage the version string in your codebase.

If you are using a content delivery network (CDN) that serves these scripts, check if they offer specific cache-control headers for their external domains. Some CDNs allow you to set long-term caching policies on third-party domains via DNS or HTTP header configuration, provided the source server supports it.

Ultimately, the goal is to balance performance with correctness. Your own assets can have a strict one-month expiry because you control the versioning. Third-party assets require a hybrid approach: cache aggressively for unchanged files but invalidate immediately when the content updates. The query string method remains the most reliable way to achieve this without requiring changes to the third-party provider's server configuration.

Our network