feat: add pre-built compression and code splitting for frontend assets#2705
feat: add pre-built compression and code splitting for frontend assets#2705Sumeet213 wants to merge 2 commits intoChainlit:mainfrom
Conversation
There was a problem hiding this comment.
1 issue found across 4 files
Prompt for AI agents (all 1 issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="backend/chainlit/server.py">
<violation number="1" location="backend/chainlit/server.py:283">
P2: `Accept-Encoding` quality values are ignored, so a client that explicitly disallows gzip/Brotli (q=0) will still receive that encoding. Parse the header and only serve a precompressed file when the requested encoding’s q-value is greater than 0.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Ask questions if you need clarification on any suggestion
Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR
| if "br" in accept_encoding: | ||
| br_path = file_path.with_suffix(file_path.suffix + ".br") | ||
| if br_path.is_file(): | ||
| return FileResponse( | ||
| br_path, | ||
| media_type=content_type, | ||
| headers={"Content-Encoding": "br", "Vary": "Accept-Encoding"}, | ||
| ) | ||
|
|
||
| if "gzip" in accept_encoding: | ||
| gz_path = file_path.with_suffix(file_path.suffix + ".gz") | ||
| if gz_path.is_file(): | ||
| return FileResponse( | ||
| gz_path, | ||
| media_type=content_type, | ||
| headers={"Content-Encoding": "gzip", "Vary": "Accept-Encoding"}, | ||
| ) | ||
|
|
||
| return None |
There was a problem hiding this comment.
P2: Accept-Encoding quality values are ignored, so a client that explicitly disallows gzip/Brotli (q=0) will still receive that encoding. Parse the header and only serve a precompressed file when the requested encoding’s q-value is greater than 0.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/chainlit/server.py, line 283:
<comment>`Accept-Encoding` quality values are ignored, so a client that explicitly disallows gzip/Brotli (q=0) will still receive that encoding. Parse the header and only serve a precompressed file when the requested encoding’s q-value is greater than 0.</comment>
<file context>
@@ -275,37 +275,71 @@ async def serve_public_file(
+) -> Optional[FileResponse]:
+ content_type = mimetypes.guess_type(str(file_path))[0] or "application/octet-stream"
+
+ if "br" in accept_encoding:
+ br_path = file_path.with_suffix(file_path.suffix + ".br")
+ if br_path.is_file():
</file context>
| if "br" in accept_encoding: | |
| br_path = file_path.with_suffix(file_path.suffix + ".br") | |
| if br_path.is_file(): | |
| return FileResponse( | |
| br_path, | |
| media_type=content_type, | |
| headers={"Content-Encoding": "br", "Vary": "Accept-Encoding"}, | |
| ) | |
| if "gzip" in accept_encoding: | |
| gz_path = file_path.with_suffix(file_path.suffix + ".gz") | |
| if gz_path.is_file(): | |
| return FileResponse( | |
| gz_path, | |
| media_type=content_type, | |
| headers={"Content-Encoding": "gzip", "Vary": "Accept-Encoding"}, | |
| ) | |
| return None | |
| encodings: dict[str, float] = {} | |
| for value in accept_encoding.lower().split(","): | |
| token = value.strip() | |
| if not token: | |
| continue | |
| parts = token.split(";") | |
| encoding = parts[0] | |
| q = 1.0 | |
| for part in parts[1:]: | |
| if part.strip().startswith("q="): | |
| try: | |
| q = float(part.split("=", 1)[1]) | |
| except ValueError: | |
| q = 0.0 | |
| encodings[encoding] = q | |
| if encodings.get("br", 0) > 0: | |
| br_path = file_path.with_suffix(file_path.suffix + ".br") | |
| if br_path.is_file(): | |
| return FileResponse( | |
| br_path, | |
| media_type=content_type, | |
| headers={"Content-Encoding": "br", "Vary": "Accept-Encoding"}, | |
| ) | |
| if encodings.get("gzip", 0) > 0: | |
| gz_path = file_path.with_suffix(file_path.suffix + ".gz") | |
| if gz_path.is_file(): | |
| return FileResponse( | |
| gz_path, | |
| media_type=content_type, | |
| headers={"Content-Encoding": "gzip", "Vary": "Accept-Encoding"}, | |
| ) | |
| return None |
|
This PR is stale because it has been open for 14 days with no activity. |
|
@github-actions Leave it for now, I'll review in a few days |
|
This PR is stale because it has been open for 14 days with no activity. |
|
This PR was closed because it has been inactive for 7 days since being marked as stale. |
Description:
Addresses #2340
Initial load reduced from ~1.07MB to ~850KB (brotli).
Summary by cubic
Pre-built gzip and Brotli assets and vendor code splitting to reduce initial load and remove runtime compression overhead, addressing #2340. Initial load drops from ~1.07MB to ~850KB.
New Features
Dependencies
Written for commit 6f45e56. Summary will update automatically on new commits.