# Extend cache (extend_cache)

Content-hashed URLs with a 1-year browser cache for CSS, images and scripts. The extend_cache filter: what it does, its risks and the directives.

Canonical URL: https://modpagespeed.com/docs/filters/extend_cache/

- Category: Caching
- CoreFilters: Yes
- OptimizeForBandwidth: No
- Risk: Generally safe

`extend_cache` rewrites the URLs of stylesheets, scripts, and images to carry a content hash, giving each resource a `.pagespeed.ce.` URL, and serves those resources with a one-year `Cache-Control: max-age`. When the original file changes, its hash changes too: once the module's cached copy of the original expires (origin lifetime, or `ImplicitCacheTtlMs` when none is set), the page points at a new URL. Live demo: [extend_cache](https://modpagespeed.com/examples/extend_cache/).

## What it switches on

`extend_cache` is a compound name. Enabling it enables these 3 filters; each can be disabled on its own.

- [`extend_cache_css`](https://modpagespeed.com/docs/filters/extend_cache_css/): Extend cache for CSS
- [`extend_cache_images`](https://modpagespeed.com/docs/filters/extend_cache_images/): Extend cache for images
- [`extend_cache_scripts`](https://modpagespeed.com/docs/filters/extend_cache_scripts/): Extend cache for scripts

## How it works

### When it helps and when it does not

It helps wherever the origin cannot set long cache lifetimes itself, which is common on shared hosting and legacy applications: first visits cache every static resource for a year, and repeat visits stop revalidating them. It does nothing for resources that already carry a fingerprint and a long max-age from the build pipeline, since those URLs are already immutable in practice. It also does not make HTML cacheable: the page itself keeps its own short lifetime, because the page is what hands out the new hashed URLs after a deploy.

### How it decides

Only resources on domains the module is authorized to rewrite are candidates, and resources already rewritten by another filter (a minified stylesheet, an optimized image) already carry a content hash, so there is nothing to extend. The one-year lifetime works because the URL is derived from the bytes: changed content gets a new URL rather than new bytes at the old one, so visitors pick it up once they load a page that references the new URL. The name is compound: it switches on `extend_cache_css`, `extend_cache_images`, and `extend_cache_scripts`, and each member can be disabled on its own.

## When to use it

- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Risk rating on these docs: **Generally safe**.

## Risks

- HTML and the resources it references must share one configuration; a setup where two virtual hosts disagree on the same file can serve a hash that does not match. See [Virtual hosts](https://modpagespeed.com/docs/configuration/#virtual-hosts).

- Verify with the `X-Mod-Pagespeed` response header and a `?PageSpeedFilters=-extend_cache` comparison; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

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

**Apache**

```apache
ModPagespeedEnableFilters extend_cache
```

**nginx**

```nginx
pagespeed EnableFilters extend_cache;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters extend_cache
```

It is on by default under CoreFilters; to turn it off:

```text
ModPagespeedDisableFilters extend_cache   # Apache
pagespeed DisableFilters extend_cache;   # nginx
pagespeed DisableFilters extend_cache   # IIS
```

```html
<!-- before -->
<link rel="stylesheet" href="/css/site.css" />

<!-- after: served with Cache-Control: max-age=31536000 -->
<link rel="stylesheet" href="/css/site.css.pagespeed.ce.HASH.css" />
```

The sub-filters `extend_cache_css`, `extend_cache_images`, and `extend_cache_scripts` are included when you enable `extend_cache`, which turns on all three. Each can also be enabled individually with `EnableFilters` (for example, `extend_cache_images` alone).

### On the worker

The worker runs its own pipeline, configured by flags. Its equivalent of this filter is the [cache extension](https://modpagespeed.com/docs/worker-configuration/#cache-extension) transform. Always-on under `pagespeed on;`.

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: [extend_cache example](https://modpagespeed.com/examples/extend_cache/).

## Frequently asked questions

**What does the extend_cache filter do?**

`extend_cache` rewrites the URLs of stylesheets, scripts, and images to carry a content hash, giving each resource a `.pagespeed.ce.` URL, and serves those resources with a one-year `Cache-Control: max-age`. When the original file changes, its hash changes too: once the module's cached copy of the original expires (origin lifetime, or `ImplicitCacheTtlMs` when none is set), the page points at a new URL. Live demo: extend_cache.

**Is extend_cache enabled by default?**

Yes. `extend_cache` is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with `DisableFilters`.

**How do I turn off extend_cache?**

Add `ModPagespeedDisableFilters extend_cache` on Apache or `pagespeed DisableFilters extend_cache;` on nginx. On IIS, add `pagespeed DisableFilters extend_cache` to pagespeed.config.

## Related filters

- [`extend_cache_css`](https://modpagespeed.com/docs/filters/extend_cache_css/): Extend cache for CSS
- [`extend_cache_images`](https://modpagespeed.com/docs/filters/extend_cache_images/): Extend cache for images
- [`extend_cache_scripts`](https://modpagespeed.com/docs/filters/extend_cache_scripts/): Extend cache for scripts
- [`trim_urls`](https://modpagespeed.com/docs/filters/trim_urls/): Trim URLs
- [`extend_cache_pdfs`](https://modpagespeed.com/docs/filters/extend_cache_pdfs/): Extend cache for PDFs
- [`local_storage_cache`](https://modpagespeed.com/docs/filters/local_storage_cache/): Local storage cache
- [`rewrite_domains`](https://modpagespeed.com/docs/filters/rewrite_domains/): Rewrite domains

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