Skip to main content

filter · Cache control

Local storage cache (local_storage_cache)

Category:
Caching
CoreFilters:
No
OptimizeForBandwidth:
No
Risk:
Experimental

local_storage_cache moves the bytes of inlined images and CSS out of the HTML and into the browser’s localStorage. On the first visit the inlined resource is served as usual and a script stores its bytes under a pagespeed_lsc_url: key; on later visits the module sees its own _GPSLSC cookie saying the copy is there, and serves a one-line script in place of the data, which restores the resource from localStorage. The HTML shrinks on every repeat view by the size of everything stored.

How it works

Not a core filter; enable it by name, together with the filters that inline (inline_images, inline_css). Live demo: local_storage_cache.

The mechanism is per-resource and expiry-aware: each stored entry carries the resource’s cache expiration, expired entries are dropped from storage and from the cookie, and an element whose entry is missing or expired is restored by fetching the resource from the network. The filter needs JavaScript to run: since v1.15.0+r18, with HonorCsp (default on) it stands down on pages whose Content-Security-Policy disallows the inline script it relies on.

Not recommended for most deployments. Browser localStorage has size limits (typically 5-10 MB per origin) and can be cleared by the user at any time. Sites with many inlined resources may exceed these limits, and pages then pay the storing overhead without the repeat-view saving. A page whose HTML is already cached by the browser gains little: the inlined bytes ride inside a cached document either way. Compare it against extend_cache, which gets the same repeat-view saving by keeping resources in external, content-hashed URLs with a one-year lifetime and no scripts at all; that is the recommended path.

On IIS the same directives use the syntax described in IIS configuration.

When to use it

  • Not in CoreFilters: it runs only when you enable it by name.
  • Risk rating on these docs: Experimental.

Risks

The Cache control page lists no specific risks for this filter. If a page misbehaves with it, DisableFilters local_storage_cache turns it off for the scope you set it in.

Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

Apache

ModPagespeedEnableFilters local_storage_cache

nginx

pagespeed EnableFilters local_storage_cache;

IIS (pagespeed.config)

pagespeed EnableFilters local_storage_cache
<!-- first view: the stylesheet is inlined, marked for storage -->
<style data-pagespeed-lsc-url="https://example.com/css/site.css" ...>
  .site { ... }
</style>

<!-- repeat view: one script line instead of the whole stylesheet -->
<script>
  pagespeed.localStorageCache.inlineCss('https://example.com/css/site.css');
</script>

Scoping, ForbidFilters and the thresholds filters read: Choosing filters .

Live example

Stores inlined CSS and images in the browser's local storage for repeat visits.

Measured on the demo page, original against optimized:

  • HTTP requests

    3 → 2 −1

See the before and after, with the source diff →

Frequently asked questions

What does the local_storage_cache filter do?
local_storage_cache moves the bytes of inlined images and CSS out of the HTML and into the browser's localStorage. On the first visit the inlined resource is served as usual and a script stores its bytes under a pagespeed_lsc_url: key; on later visits the module sees its own _GPSLSC cookie saying the copy is there, and serves a one-line script in place of the data, which restores the resource from localStorage. The HTML shrinks on every repeat view by the size of everything stored.
Is local_storage_cache enabled by default?
No. local_storage_cache is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with EnableFilters.
How do I enable local_storage_cache on Apache and nginx?
Add ModPagespeedEnableFilters local_storage_cache on Apache or pagespeed EnableFilters local_storage_cache; on nginx. On IIS, add pagespeed EnableFilters local_storage_cache to pagespeed.config.

This page is drawn from the local_storage_cache entry on Cache control. Every filter in one table: PageSpeed filters.

Search