Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions backend/chainlit/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,37 +275,71 @@ async def serve_public_file(
raise HTTPException(status_code=404, detail="File not found")


def get_precompressed_response(
file_path: Path, accept_encoding: str
) -> 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():
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
Comment on lines +283 to +301
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
+) -&gt; Optional[FileResponse]:
+    content_type = mimetypes.guess_type(str(file_path))[0] or &quot;application/octet-stream&quot;
+
+    if &quot;br&quot; in accept_encoding:
+        br_path = file_path.with_suffix(file_path.suffix + &quot;.br&quot;)
+        if br_path.is_file():
</file context>
Suggested change
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
Fix with Cubic



@router.get("/assets/{filename:path}")
async def serve_asset_file(
request: Request,
filename: str,
):
"""Serve a file from assets dir."""

base_path = Path(os.path.join(build_dir, "assets"))
file_path = (base_path / filename).resolve()

if not is_path_inside(file_path, base_path):
raise HTTPException(status_code=400, detail="Invalid filename")

if file_path.is_file():
accept_encoding = request.headers.get("accept-encoding", "")
precompressed = get_precompressed_response(file_path, accept_encoding)
if precompressed:
return precompressed
return FileResponse(file_path)
else:
raise HTTPException(status_code=404, detail="File not found")


@router.get("/copilot/{filename:path}")
async def serve_copilot_file(
request: Request,
filename: str,
):
"""Serve a file from assets dir."""

base_path = Path(copilot_build_dir)
file_path = (base_path / filename).resolve()

if not is_path_inside(file_path, base_path):
raise HTTPException(status_code=400, detail="Invalid filename")

if file_path.is_file():
accept_encoding = request.headers.get("accept-encoding", "")
precompressed = get_precompressed_response(file_path, accept_encoding)
if precompressed:
return precompressed
return FileResponse(file_path)
else:
raise HTTPException(status_code=404, detail="File not found")
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^5.4.14",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-svgr": "^4.2.0",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.34.4"
Expand Down
42 changes: 42 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 50 additions & 2 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
import react from '@vitejs/plugin-react-swc';
import path from 'path';
import { defineConfig } from 'vite';
import viteCompression from 'vite-plugin-compression';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';

// https://vitejs.dev/config/
export default defineConfig({
build: {
sourcemap: true
sourcemap: true,
rollupOptions: {
output: {
manualChunks: {
'vendor-react': ['react', 'react-dom', 'react-router-dom'],
'vendor-ui': [
'@radix-ui/react-dialog',
'@radix-ui/react-dropdown-menu',
'@radix-ui/react-popover',
'@radix-ui/react-select',
'@radix-ui/react-tabs',
'@radix-ui/react-tooltip',
'@radix-ui/react-accordion',
'@radix-ui/react-checkbox',
'@radix-ui/react-switch',
'@radix-ui/react-slider',
'@radix-ui/react-scroll-area',
'lucide-react'
],
'vendor-markdown': [
'react-markdown',
'rehype-katex',
'rehype-raw',
'remark-gfm',
'remark-math',
'remark-directive',
'highlight.js'
],
'vendor-utils': ['lodash', 'uuid', 'zod', 'i18next', 'react-i18next']
}
}
}
},
plugins: [react(), tsconfigPaths(), svgr()],
plugins: [
react(),
tsconfigPaths(),
svgr(),
viteCompression({
algorithm: 'gzip',
ext: '.gz',
threshold: 1024,
deleteOriginFile: false
}),
viteCompression({
algorithm: 'brotliCompress',
ext: '.br',
threshold: 1024,
deleteOriginFile: false
})
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
Expand Down
Loading