Skip to content

flask won't response 304 Not Modified due to Etag modificationΒ #65

@ITcarrot

Description

@ITcarrot

When browser try to get the same file again, due to the etag modification described in issue #15 , browser will send the modified etag to flask server. Therefore, flask server will return 200 OK with file contents instead of 304 because the etag is different. There will be a significant waste of network traffic, especially when serving static js and css files.

Expected behaviour, this happens when flask-compress is not involed during request:

GET /static/img/emoji1.png HTTP/1.1
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Host: 127.0.0.1:5000
If-Modified-Since: Wed, 17 Sep 2025 10:10:52 GMT
If-None-Match: "1758103852.0-22711-916066088"
Referer: http://127.0.0.1:5000/play/3
Sec-Fetch-Dest: image
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
sec-ch-ua: "Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
====================
HTTP/1.1 304 NOT MODIFIED
Server: Werkzeug/3.1.3 Python/3.12.11
Date: Sun, 28 Sep 2025 07:23:06 GMT
Content-Disposition: inline; filename=emoji1.png
Cache-Control: no-cache
ETag: "1758103852.0-22711-916066088"
Date: Sun, 28 Sep 2025 07:23:06 GMT
Vary: Accept-Encoding
Connection: close

Current behaviour, this happens when flask-compress is involed during request:

GET /static/js/play.js HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Host: 127.0.0.1:5000
If-Modified-Since: Sun, 28 Sep 2025 05:55:55 GMT
If-None-Match: "1759038955.6423435-29319-4233694673:zstd"
Referer: http://127.0.0.1:5000/play/3
Sec-Fetch-Dest: script
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
sec-ch-ua: "Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
=====================
HTTP/1.1 200 OK
Server: Werkzeug/3.1.3 Python/3.12.11
Date: Sun, 28 Sep 2025 07:23:06 GMT
Content-Disposition: inline; filename=play.js
Content-Type: text/javascript; charset=utf-8
Content-Length: 7996
Last-Modified: Sun, 28 Sep 2025 05:55:55 GMT
Cache-Control: no-cache
ETag: "1759038955.6423435-29319-4233694673:zstd"
Date: Sun, 28 Sep 2025 07:23:06 GMT
Vary: Accept-Encoding
Content-Encoding: zstd
Connection: close

I suggest three different ways to solve this issue:

  1. Do not modify the etag. The simplest way to fix this, it aligns with flask's logic on etag but will violate the RFC standard.
  2. Add an option to switch on/off etag modification.
  3. Checking whether the files is modified again in flask-compress. This is most recommanded but will require more effort to write some code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions