How to filter Flask application's static files based on modification time?

Jul 28, 2025Leave a message

Hey there! As a Filtering Flask supplier, I often get asked about how to filter Flask application's static files based on modification time. It's a common issue, especially when you're dealing with a large number of static files and you want to make sure you're only serving the most up - to - date ones. In this blog, I'll share some tips and tricks on how to achieve this.

First off, let's understand why we might want to filter static files based on modification time. When you're developing a Flask application, static files like CSS, JavaScript, and images are crucial for the user experience. However, if you've made changes to these files and the browser is still serving the old versions, it can lead to a lot of headaches. By filtering based on modification time, you can ensure that the latest versions of these files are always served.

Laboratory Glass Conical Shape Erlenmeyer Filtering Flasks With Upper TubulationLaboratory Filtering Flask

Using Flask's Built - in Features

Flask has some built - in features that can help us with this. One of the simplest ways is to use the send_from_directory function. This function can be used to serve static files from a directory. To filter based on modification time, we can add a bit of logic around it.

from flask import Flask, send_from_directory
import os
import time

app = Flask(__name__)

@app.route('/static/<path:path>')
def send_static(path):
    static_dir = 'static'
    file_path = os.path.join(static_dir, path)
    if os.path.exists(file_path):
        # Get the modification time
        mod_time = os.path.getmtime(file_path)
        # You can set a threshold here, for example, files modified in the last 60 minutes
        threshold = time.time() - 60 * 60
        if mod_time > threshold:
            return send_from_directory(static_dir, path)
    return 'File not found or not recently modified', 404

if __name__ == '__main__':
    app.run(debug=True)

In this code, we first check if the file exists. Then we get its modification time using os.path.getmtime. We set a threshold (in this case, files modified in the last hour). If the file's modification time is greater than the threshold, we serve it using send_from_directory. Otherwise, we return a 404 error.

Caching and Modification Time

Another important aspect is caching. Browsers cache static files to improve performance. But this can be a problem when you've updated a file. You can use Flask's caching mechanisms to control this. One way is to use the add_headers decorator to add cache - control headers.

from flask import Flask, send_from_directory
import os
import time

app = Flask(__name__)

def add_headers(response):
    response.headers['Cache - Control'] = 'no - cache, no - store, must - revalidate'
    response.headers['Pragma'] = 'no - cache'
    response.headers['Expires'] = '0'
    return response

@app.route('/static/<path:path>')
def send_static(path):
    static_dir = 'static'
    file_path = os.path.join(static_dir, path)
    if os.path.exists(file_path):
        mod_time = os.path.getmtime(file_path)
        threshold = time.time() - 60 * 60
        if mod_time > threshold:
            response = send_from_directory(static_dir, path)
            return add_headers(response)
    return 'File not found or not recently modified', 404

if __name__ == '__main__':
    app.run(debug=True)

By adding these headers, we're telling the browser not to cache the file. So every time the user requests the file, the server will check the modification time again.

Third - Party Libraries

There are also some third - party libraries that can help with this. For example, Flask - Caching can be used to implement more advanced caching strategies. You can use it to cache files based on their modification time.

from flask import Flask, send_from_directory
import os
import time
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@app.route('/static/<path:path>')
@cache.cached(key_prefix=lambda: os.path.getmtime(os.path.join('static', path)))
def send_static(path):
    static_dir = 'static'
    file_path = os.path.join(static_dir, path)
    if os.path.exists(file_path):
        mod_time = os.path.getmtime(file_path)
        threshold = time.time() - 60 * 60
        if mod_time > threshold:
            return send_from_directory(static_dir, path)
    return 'File not found or not recently modified', 404

if __name__ == '__main__':
    app.run(debug=True)

In this example, we're using Flask - Caching to cache the response. The cache key is based on the file's modification time. So if the file is modified, the cache will be invalidated.

Our Filtering Flask Products

As a Filtering Flask supplier, we offer a wide range of high - quality filtering flasks. For example, you can check out our Laboratory Glass Conical Shape Erlenmeyer Filtering Flasks with Upper Tubulation and Laboratory Clear Glass Filtering Flasks with Upper Tubulature. These flasks are made of high - quality glass and are perfect for various laboratory filtering applications.

Conclusion

Filtering Flask application's static files based on modification time is an important task to ensure the best user experience. Whether you use Flask's built - in features, caching mechanisms, or third - party libraries, there are many ways to achieve this. And if you're in the market for high - quality filtering flasks, don't hesitate to reach out to us. We're here to help you with all your filtering needs. If you're interested in our products or have any questions, feel free to contact us for a purchase negotiation.

References

  • Flask Documentation
  • Python os module documentation
  • Flask - Caching documentation