---
title: "How to implement cache control for different file types, such as images, CSS, and JavaScript files?"  
description: "How to implement cache control for different file types, such as images, CSS, and JavaScript files?"  
author: "Utpal Vishwas"  
published: 2023-05-23  
updated: 2023-05-24  
canonical: https://www.mindstick.com/forum/158456/how-to-implement-cache-control-for-different-file-types-such-as-images-css-and-javascript-files  
category: "cache"  
tags: ["cache", "browser-cache"]  
reading_time: 3 minutes  

---

# How to implement cache control for different file types, such as images, CSS, and JavaScript files?

How to [implement cache](https://www.mindstick.com/forum/158461/how-can-you-implement-cache-control-for-dynamically-generated-content) [control](https://www.mindstick.com/blog/197/asp-dot-net-repeater-control) for different [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) types, such as [images](https://www.mindstick.com/interview/1067/what-is-the-php-predefined-variable-that-tells-the-what-type-of-images-that-php-support), [CSS](https://www.mindstick.com/forum/514/background-gradient-ie7-css-problem), and [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud)?

## Replies

### Reply by Aryan Kumar

To implement [cache](https://www.mindstick.com/blog/222/clearing-cache-in-asp-dot-net) control for different file types like images, CSS, and JavaScript files, you can use various HTTP headers to provide instructions to the browser on how long to cache the resources. Here are some commonly used headers:

- **Cache-Control**: The Cache-Control header allows you to specify caching directives for a particular resource. Some commonly used directives are:

1. **public**: Indicates that the resource can be cached by both the browser and intermediary caches (CDNs, proxies, etc.).
2. **private**: Specifies that the resource is specific to a particular user and should not be cached by intermediary caches.
3. **max-age**: Specifies the maximum time in seconds that the resource can be cached. For example, **Cache-Control: max-age=3600** indicates the resource can be cached for one hour.
4. **no-cache**: Instructs the browser to revalidate the resource with the server before using a cached version. It does not prevent caching but ensures the cached version is validated before use.
5. **no-store**: Directs the browser not to cache the resource at all.

- **Expires**: The Expires header specifies an absolute date and time after which the resource is considered expired and should be revalidated. For example, **Expires: Fri, 31 Dec 2023 23:59:59 GMT** indicates that the resource is valid until the specified date.
- **Last-Modified**: The Last-Modified header indicates the date and time when the resource was last modified on the server. It is used by the browser to check if the resource has been modified since it was last cached. If the resource has not been modified, the cached version is used. For example, **Last-Modified: Wed, 23 May 2023 10:00:00 GMT**.
- **ETag**: The ETag header provides a unique identifier (usually a hash or version number) for a specific version of the resource. The browser sends this identifier in subsequent requests using the **If-None-Match** header to check if the resource has been modified. If the ETag matches, the cached version is used. For example, **ETag: "abc123"**.

To implement cache control for different file types:

1. Configure the web server: You can set cache control headers in your web server's configuration file. For example, using Apache's **.htaccess** file, you can specify cache control directives like **ExpiresByType**, **Header set Cache-Control**, and **FileETag**.
2. Set headers in server-side code: If you're using server-side code (e.g., PHP, Node.js), you can set the appropriate headers before sending the response. You can use functions like **header()** in PHP or set headers directly in the response object in Node.js.
3. Content Delivery Networks (CDNs): If you're using a CDN, check their documentation for cache control settings. CDNs often provide ways to set cache control headers or configure caching rules specific to different file types.

It's important to consider the specific caching requirements of your application and balance them with the need for freshness of content. Test caching behavior across different browsers and platforms to ensure the desired cache control is being applied correctly.


---

Original Source: https://www.mindstick.com/forum/158456/how-to-implement-cache-control-for-different-file-types-such-as-images-css-and-javascript-files

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
