Refactor: Remove redundant logic and improve code clarity in worker.ts#22
Refactor: Remove redundant logic and improve code clarity in worker.ts#22
Conversation
Co-authored-by: nota9x <105396031+nota9x@users.noreply.github.com>
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
hapara-fail | e2b213e | Commit Preview URL Branch Preview URL |
Feb 11 2026, 02:18 AM |
Co-authored-by: nota9x <105396031+nota9x@users.noreply.github.com>
Co-authored-by: nota9x <105396031+nota9x@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors src/worker.ts to reduce redundant response-handling logic and improve readability around CSP definition and intermediate variable naming in the Cloudflare Worker entrypoint.
Changes:
- Refactors the Content Security Policy definition into a directive array joined into a single header value.
- Renames intermediate variables to better reflect what
handleAssetResponse()returns. - Simplifies 404 fallback handling by attempting to process the
/404.htmlresponse via the shared helper.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/worker.ts
Outdated
| } | ||
| // If the helper returns null, fall through to the 404 handling below. | ||
| const processedNotFoundResponse = handleAssetResponse(notFoundResponse); | ||
| if (processedNotFoundResponse) { |
There was a problem hiding this comment.
Bug: handleAssetResponse() returns a non-null response for any ok response. In the 404 fallback flow, this causes /404.html to be returned with its original status (typically 200) instead of being wrapped as a 404. Previously only 304s were short-circuited; successful 404.html responses were intentionally rewrapped with status: 404 below.
Fix: only short-circuit for notFoundResponse.status === 304 (or apply security headers without returning early for ok), so the notFoundResponse.ok branch can still wrap the body with a 404 status.
| if (processedNotFoundResponse) { | |
| // Only short-circuit 304 Not Modified responses; allow 200 OK to be rewrapped as 404 below. | |
| if (processedNotFoundResponse && processedNotFoundResponse.status === 304) { |
There was a problem hiding this comment.
Fixed in e2b213e. Now only short-circuits for 304 responses, allowing 200 OK responses from /404.html to be properly rewrapped with status 404.
| const cspDirectives = [ | ||
| "default-src 'self';", | ||
| "img-src 'self' data:;", | ||
| "style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;", | ||
| "font-src 'self' https://fonts.gstatic.com;", |
There was a problem hiding this comment.
Formatting/convention: .prettierrc sets singleQuote: true and trailingComma: "es5", but this new cspDirectives array uses double-quoted strings and omits the trailing comma on the last element. Please run Prettier (or update these literals) so this block matches the repo’s formatting rules and avoids churn in future diffs.
There was a problem hiding this comment.
Fixed in e2b213e. Applied Prettier formatting - added trailing comma to CSP directives array per repo conventions.
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: nota9x <105396031+nota9x@users.noreply.github.com>
Changes applied in e2b213e:
|
Pull Request (Website)
1. Type of Change:
What does this PR do? (Check one)
2. Related Issue:
Does this fix an existing issue? Please link it here (e.g., "Closes #24").
3. Summary of Changes:
Briefly explain what you changed and why. (e.g., "Fixed mobile navbar not opening on iOS," "Added a new blog post about Securly SSL decryption")
4. Verification & Testing:
How did you verify these changes?
npm run build) without errors.npm run dev).5. Screenshots (Optional):
If this is a visual change, please paste a screenshot or GIF below to help us review it faster.
Final Checklist:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.