Add content hashes to asset URLs#6749
Conversation
Greptile SummaryThis PR adds content-hash cache busting to asset URLs. The main changes are:
Confidence Score: 4/5The remaining cache race should be fixed before merging.
reflex/assets.py Important Files Changed
Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/asset-conte..." | Re-trigger Greptile |
Merging this PR will not alter performance
Comparing Footnotes
|
| Returns: | ||
| The first 8 hex characters of the file's SHA-256 hash. | ||
| """ | ||
| digest = hashlib.sha256() |
There was a problem hiding this comment.
seems like a useless extra initialization; just keep the one in the loop
| break | ||
| return digest.hexdigest()[:8] |
There was a problem hiding this comment.
| break | |
| return digest.hexdigest()[:8] | |
| return digest.hexdigest()[:8] | |
| console.warning(f"{path} was modified {_MAX_HASH_ATTEMPTS} times while calculating hash") | |
| return str(time.time()) |
we should consider doing something reasonable when we can't get a stable hash, like using the compile timestamp as the hash rather than always returning the last digest, which might cause the browser to load a previous cached version. at least if we take the timestamp, the browser will reload the asset once per compile, which is probably good enough for most uses.
Closes #6550.
Adds content-hash cache busting to
rx.assetURLs by appending an 8-character SHA-256 query parameter based on theasset file contents.
Example:
rx.asset("logo.svg")now returns/logo.svg?v=<hash>.The hash changes only when the file content changes, allowing browsers to cache assets safely across deploys.
Implementation notes:
AssetPathStr.importable_pathunversioned so existing build-time import behavior is preserved.