# Static vs Dynamic Caching

### Static Content

Static content is any file that is stored in a server and is the same every time it is delivered to users. HTML files and images are examples of this kind of content.

#### Caching Static Content

The usual web caching process is for a cache to save a copy of the static file – e.g., an image, – when the content is served, so that it's closer to the user and delivered more quickly the next time. Browsers and [content delivery networks (CDNs)](https://www.cloudflare.com/learning/cdn/what-is-a-cdn/) can cache static content for a set time period and serve it to users as long as the content continues to be requested. This is possible because static content does not change over time; the same file can be delivered to users over and over.

### Dynamic Caching

Unlike static content, dynamic content is different for each user, meaning it cannot be served to multiple users and is difficult to cache.&#x20;

Dynamic content is content that changes based on factors specific to the user such as time of visit, location, and device. A dynamic webpage will not look the same for everybody, and it can change as users interact with it.

**Example**, NEWS Website, Social Media Pages

Dynamic webpages are not stored as static HTML files. Instead, [server-side](https://www.cloudflare.com/learning/serverless/glossary/client-side-vs-server-side/) scripts generate an HTML file in response to events, such as user interactions or user logins, and send the HTML file to the web browser. Because dynamic content is generated server-side, it is typically served from [origin servers](https://www.cloudflare.com/learning/cdn/glossary/origin-server/), not a cache.

#### Caching Dynamic Content

Dynamic content is generated by scripts that change the content on a page. By running scripts in a CDN cache instead of in a distant origin server, dynamic content can be generated and delivered from a cache. Dynamic content is thus essentially "cached" and does not have to be served all the way from the origin, reducing the response time to client requests and speeding up dynamic webpages.

[Cloudflare Workers](https://www.cloudflare.com/products/cloudflare-workers/), for example, are serverless JavaScript functions that run on the Cloudflare CDN. They can respond to a variety of events and inputs, including device type, time of day, user location, or data from third party APIs. Based on these parameters, dynamic content can be generated and served to client devices, or static content can be altered, cached, or purged from the cache. Additionally, Cloudflare Workers can be deployed and can propagate to CDN locations around the globe in seconds. As Cloudflare Workers run in a lightweight execution environment so that the code can spin up and execute within milliseconds.

|                                                                                            Dynamic Content Compression                                                                                           |
| :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|                      Another approach to speeding up dynamic webpages is to compress dynamic content generated by the origin server and deliver it as quickly and efficiently as possible.                       |
| With dynamic compression, the content still comes from the origin server instead of a cache, but the HTML files generated are made significantly smaller so that they can reach the client device more quickly.  |
|                              [Railgun](https://www.cloudflare.com/website-optimization/railgun/) is an example of a product that compresses dynamic content, improving performance.                              |
