fix(examples): API route calls failing on server#7578
Conversation
📝 WalkthroughWalkthroughDocumentation guides and example applications are updated to use router-provided origin for constructing absolute API URLs. Route loaders now call ChangesRouter Origin Adoption for Absolute URL Construction
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates Start example route loaders (Solid + React across multiple adapters) to build absolute API URLs using the router’s origin, and adjusts documentation examples to use fully-qualified URLs in fetch calls.
Changes:
- Updated various
/usersand/users/$userIdloaders to callgetRouterInstance()and usenew URL(path, router.origin)for server/client-compatible fetching. - Mirrored the same change across Solid Start adapters (basic/nitro/netlify/cloudflare) and React Start adapters (basic/cloudflare).
- Updated execution-model/code-execution docs to show
fetchcalls using an absolutehttps://api.example.com/...URL.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| examples/solid/start-basic/src/routes/users.tsx | Use router origin to build absolute /api/users URL in loader |
| examples/solid/start-basic/src/routes/users.$userId.tsx | Use router origin to build absolute /api/users/:id URL in loader |
| examples/solid/start-basic-nitro/src/routes/users.tsx | Same absolute URL fetch pattern for Nitro example |
| examples/solid/start-basic-nitro/src/routes/users.$userId.tsx | Same absolute URL fetch pattern for Nitro user detail route |
| examples/solid/start-basic-netlify/src/routes/users.tsx | Same absolute URL fetch pattern for Netlify example |
| examples/solid/start-basic-netlify/src/routes/users.$userId.tsx | Same absolute URL fetch pattern for Netlify user detail route |
| examples/solid/start-basic-cloudflare/src/routes/users.tsx | Same absolute URL fetch pattern for Cloudflare example |
| examples/solid/start-basic-cloudflare/src/routes/users.$userId.tsx | Same absolute URL fetch pattern for Cloudflare user detail route |
| examples/react/start-basic/src/routes/users.tsx | Use router origin to build absolute /api/users URL in loader |
| examples/react/start-basic/src/routes/users.$userId.tsx | Use router origin to build absolute /api/users/:id URL in loader |
| examples/react/start-basic-cloudflare/src/routes/users.tsx | Same absolute URL fetch pattern for React Cloudflare example |
| examples/react/start-basic-cloudflare/src/routes/users.$userId.tsx | Same absolute URL fetch pattern for React Cloudflare user detail route |
| docs/start/framework/solid/guide/execution-model.md | Replace relative /api/... fetch examples with absolute https://api.example.com/... |
| docs/start/framework/solid/guide/code-execution-patterns.md | Replace relative /api/... fetch examples with absolute https://api.example.com/... |
| docs/start/framework/react/guide/execution-model.md | Replace relative /api/... fetch examples with absolute https://api.example.com/... |
| docs/start/framework/react/guide/code-execution-patterns.md | Replace relative /api/... fetch examples with absolute https://api.example.com/... |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const router = await getRouterInstance() | ||
| const res = await fetch(new URL('/api/users/' + userId, router.origin)) |
| const router = await getRouterInstance() | ||
| const res = await fetch(new URL('/api/users', router.origin)) |
| loader: async () => { | ||
| // This runs on server during SSR AND on client during navigation | ||
| const response = await fetch('/api/products') | ||
| const response = await fetch('https://api.example.com/api/products') |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
examples/solid/start-basic/src/routes/users.$userId.tsx (1)
17-17: ⚡ Quick winConsider adding type assertion for consistency.
This file returns
datawithout a type assertion, while the equivalent files instart-basic-cloudflare,start-basic-netlify, andstart-basic-nitroall usereturn data as User. Adding the type assertion would improve consistency across examples and strengthen type safety.✨ Suggested change for consistency
- return data + return data as User🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/solid/start-basic/src/routes/users`.$userId.tsx at line 17, The route currently returns raw data without a type assertion; update the return in users.$userId.tsx to return the payload with an explicit User assertion (e.g., return data as User) so it matches the other examples and strengthens type safety—locate the loader/action/export that returns data in users.$userId.tsx and add the "as User" assertion referencing the User type used elsewhere in the project.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@examples/solid/start-basic/src/routes/users`.$userId.tsx:
- Line 17: The route currently returns raw data without a type assertion; update
the return in users.$userId.tsx to return the payload with an explicit User
assertion (e.g., return data as User) so it matches the other examples and
strengthens type safety—locate the loader/action/export that returns data in
users.$userId.tsx and add the "as User" assertion referencing the User type used
elsewhere in the project.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 2a5e55ff-5363-4cff-bfbb-54b68662d861
📒 Files selected for processing (16)
docs/start/framework/react/guide/code-execution-patterns.mddocs/start/framework/react/guide/execution-model.mddocs/start/framework/solid/guide/code-execution-patterns.mddocs/start/framework/solid/guide/execution-model.mdexamples/react/start-basic-cloudflare/src/routes/users.$userId.tsxexamples/react/start-basic-cloudflare/src/routes/users.tsxexamples/react/start-basic/src/routes/users.$userId.tsxexamples/react/start-basic/src/routes/users.tsxexamples/solid/start-basic-cloudflare/src/routes/users.$userId.tsxexamples/solid/start-basic-cloudflare/src/routes/users.tsxexamples/solid/start-basic-netlify/src/routes/users.$userId.tsxexamples/solid/start-basic-netlify/src/routes/users.tsxexamples/solid/start-basic-nitro/src/routes/users.$userId.tsxexamples/solid/start-basic-nitro/src/routes/users.tsxexamples/solid/start-basic/src/routes/users.$userId.tsxexamples/solid/start-basic/src/routes/users.tsx
Hey there,
I was exploring the start-basic example locally, and noticed the issue with API routes calls from the server.
The problem
Isomorphic calls to API routes fail on the server because of the relative URLs:
router/examples/react/start-basic/src/routes/users.tsx
Lines 4 to 6 in 4a93cff
Possible solutions
router.originfor this purpose:createServerFn()and call them via RPCThe solution
I took the 1st approach, because I assume that the purpose of the API routes is to showcase that it's possible to have an API route and use it in route loader.
I've also updated some snippets in the docs that were using relative URLs in isomorphic functions, and used absolute URLs (relative URLs won't work on the server anyway). I didn't want to bring
route.originsolution there to not distract from the main purpose of those snippets.Summary by CodeRabbit
Documentation
Bug Fixes