-
Notifications
You must be signed in to change notification settings - Fork 1
Fix Copy Page Dropdown 404 errors in production #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Replace runtime file system access with pre-built docs.json approach - Follow same pattern as llms.txt route for Cloudflare Workers compatibility - Maintain same API interface for frontend component - Add path matching logic for both direct and index.mdx patterns Fixes #239 production 404 errors by using build-time generated JSON instead of Node.js fs operations that don't work in Cloudflare Workers. Co-Authored-By: srith@agentuity.com <rithsenghorn@gmail.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
WalkthroughThe GET handler in Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant APIHandler
participant PreloadedJSON
Client->>APIHandler: GET /api/page-content?path=...
APIHandler->>PreloadedJSON: Lookup document by path
alt Document found
PreloadedJSON-->>APIHandler: Document object
APIHandler-->>Client: 200 OK with content & metadata
else Not found
APIHandler-->>Client: 404 Not Found
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Deploying with
|
Status | Name | Latest Commit | Preview URL | Updated (UTC) |
---|---|---|---|---|
✅ Deployment successful! View logs |
docs | 7d96592 | Commit Preview URL Branch Preview URL |
Jul 31 2025, 02:52 PM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
app/api/page-content/route.ts (1)
36-39
: Response format maintains backward compatibility.The response structure correctly preserves the API contract, ensuring frontend compatibility as intended.
For better type safety, consider using optional chaining:
return Response.json({ content: doc.content, - title: doc.meta.title || '', - description: doc.meta.description || '', + title: (doc.meta.title as string) ?? '', + description: (doc.meta.description as string) ?? '', path });This makes the type casting explicit and uses nullish coalescing for cleaner fallback handling.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/api/page-content/route.ts
(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: applies to agent-docs/**/src/agents/**/index.ts : the file should export a default function...
Learnt from: CR
PR: agentuity/docs#0
File: agent-docs/.cursor/rules/agent.mdc:0-0
Timestamp: 2025-07-23T12:40:22.412Z
Learning: Applies to agent-docs/**/src/agents/**/index.ts : The file should export a default function
Applied to files:
app/api/page-content/route.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Workers Builds: docs
🔇 Additional comments (2)
app/api/page-content/route.ts (2)
31-33
: LGTM! Clean 404 handling.The null check and 404 response are appropriate and maintain the expected API contract.
25-29
: Unable to locatedocs.json
for ambiguity checkI ran a script to find and analyze the
docs.json
file but didn’t find it in the repository. Please:
- Confirm the exact filename and path of your docs metadata (e.g.
docs.json
,content/docs.json
, etc.).- Manually inspect that file for any cases where two entries collapse to the same base path (for example both
foo.mdx
andfoo/index.mdx
), which could lead to unpredictable matches.Once you’ve located and reviewed it, you can ensure there are no overlapping base paths so that the three-pattern lookup remains unambiguous.
import docsJson from '@/content/docs.json'; | ||
|
||
interface Doc { | ||
file: string; | ||
meta: Record<string, unknown>; | ||
content: string; | ||
} | ||
|
||
const docs = docsJson.docs as Doc[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add runtime validation for the imported JSON structure.
The type assertion as Doc[]
bypasses TypeScript's type checking, which could lead to runtime errors if docs.json
doesn't match the expected structure.
Consider adding runtime validation:
-const docs = docsJson.docs as Doc[];
+const docs = Array.isArray(docsJson.docs)
+ ? (docsJson.docs as Doc[])
+ : [];
+
+// Validate the structure exists
+if (docs.length === 0) {
+ console.warn('No docs found in docs.json - Copy Page feature may not work');
+}
Additionally, consider making the meta
property more specific if you know the expected fields:
interface Doc {
file: string;
- meta: Record<string, unknown>;
+ meta: {
+ title?: string;
+ description?: string;
+ [key: string]: unknown;
+ };
content: string;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
import docsJson from '@/content/docs.json'; | |
interface Doc { | |
file: string; | |
meta: Record<string, unknown>; | |
content: string; | |
} | |
const docs = docsJson.docs as Doc[]; | |
import docsJson from '@/content/docs.json'; | |
interface Doc { | |
file: string; | |
meta: { | |
title?: string; | |
description?: string; | |
[key: string]: unknown; | |
}; | |
content: string; | |
} | |
const docs = Array.isArray(docsJson.docs) | |
? (docsJson.docs as Doc[]) | |
: []; | |
// Validate the structure exists | |
if (docs.length === 0) { | |
console.warn('No docs found in docs.json - Copy Page feature may not work'); | |
} |
🤖 Prompt for AI Agents
In app/api/page-content/route.ts around lines 2 to 10, the imported JSON is cast
to Doc[] using a type assertion, which skips runtime validation and risks
runtime errors if the JSON structure is incorrect. To fix this, implement
runtime validation of the imported JSON data against the expected Doc interface,
for example by using a validation library or manual checks to confirm each
object has the required fields with correct types before assigning to docs.
Also, refine the meta property type from Record<string, unknown> to a more
specific interface if the expected fields are known, improving type safety.
Fix Copy Page Dropdown 404 errors in production
Summary
This PR fixes the 404 errors occurring in production for the Copy Page Dropdown functionality by replacing runtime file system access with a pre-built JSON approach, following the same pattern used by the existing
llms.txt
route.Root Cause: The original
/api/page-content
route used Node.js file system operations (readFile
,join
) and thegray-matter
dependency at runtime, which don't work in the Cloudflare Workers environment.Solution: Replace runtime file operations with imports from pre-built
docs.json
that's generated at build time byscripts/generate-docs-json.js
, using the same pattern asapp/llms.txt/route.ts
.Review & Testing Checklist for Human
npm run build
and verify it completes without errorscontent/docs.json
gets generated during the build processMedium Priority - Functionality Testing
Diagram
Notes
Key Changes Made:
import { readFile } from 'fs/promises'
andimport matter from 'gray-matter'
withimport docsJson from '@/content/docs.json'
docs/
prefix that wasn't needed)Testing Completed:
Potential Risks:
Link to Devin run: https://app.devin.ai/sessions/980312765f61444f9cb9d87e15fcf676
Requested by: @afterrburn
Summary by CodeRabbit