# Local storage cache (local_storage_cache)

Caches inlined resources in localStorage. The local_storage_cache filter: what it does, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/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](https://modpagespeed.com/examples/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](https://modpagespeed.com/docs/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](https://modpagespeed.com/docs/cache-control/#local_storage_cache) 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**

```apache
ModPagespeedEnableFilters local_storage_cache
```

**nginx**

```nginx
pagespeed EnableFilters local_storage_cache;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters local_storage_cache
```

```html
<!-- 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](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [local_storage_cache example](https://modpagespeed.com/examples/local_storage_cache/).

## 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.

## Related filters

- [`extend_cache_pdfs`](https://modpagespeed.com/docs/filters/extend_cache_pdfs/): Extend cache for PDFs
- [`rewrite_domains`](https://modpagespeed.com/docs/filters/rewrite_domains/): Rewrite domains
- [`extend_cache`](https://modpagespeed.com/docs/filters/extend_cache/): Extend cache

This page is drawn from the [local_storage_cache entry on Cache control](https://modpagespeed.com/docs/cache-control/#local_storage_cache). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
