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
3 changes: 2 additions & 1 deletion examples/react/start-basic/src/routes/users.$userId.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createFileRoute } from '@tanstack/react-router'
import { NotFound } from '../components/NotFound'
import { UserErrorComponent } from '../components/UserError'
import { DEPLOY_URL } from '../utils/users'
import type { User } from '../utils/users'

export const Route = createFileRoute('/users/$userId')({
loader: async ({ params: { userId } }) => {
try {
const res = await fetch('/api/users/' + userId)
const res = await fetch(DEPLOY_URL + '/api/users/' + userId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

cat -n examples/react/start-basic/src/routes/users.\$userId.tsx

Repository: TanStack/router

Length of output: 1612


Encode userId before building the request path.

Line 10 concatenates raw userId into the URL path. If it contains reserved characters like /, ?, or #, the request target will be malformed.

Proposed fix
-      const res = await fetch(DEPLOY_URL + '/api/users/' + userId)
+      const res = await fetch(DEPLOY_URL + '/api/users/' + encodeURIComponent(userId))
📝 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.

Suggested change
const res = await fetch(DEPLOY_URL + '/api/users/' + userId)
const res = await fetch(DEPLOY_URL + '/api/users/' + encodeURIComponent(userId))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/react/start-basic/src/routes/users`.$userId.tsx at line 10, The URL
is built by concatenating raw userId into DEPLOY_URL in users.$userId.tsx (the
line with const res = await fetch(DEPLOY_URL + '/api/users/' + userId)), which
breaks if userId contains reserved characters; fix it by encoding userId with
encodeURIComponent before appending to the path (replace the direct userId
concat with an encoded version or compute an encodedUserId variable and use
that) so the fetch target is always a valid URL.

if (!res.ok) {
throw new Error('Unexpected status code')
}
Expand Down
3 changes: 2 additions & 1 deletion examples/react/start-basic/src/routes/users.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Link, Outlet, createFileRoute } from '@tanstack/react-router'
import { DEPLOY_URL } from '../utils/users'
import type { User } from '../utils/users'

export const Route = createFileRoute('/users')({
loader: async () => {
const res = await fetch('/api/users')
const res = await fetch(DEPLOY_URL + '/api/users')

if (!res.ok) {
throw new Error('Unexpected status code')
Expand Down
2 changes: 2 additions & 0 deletions examples/react/start-basic/src/utils/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export type User = {
name: string
email: string
}

export const DEPLOY_URL = 'http://localhost:3000'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

cat -n examples/react/start-basic/src/utils/users.tsx

Repository: TanStack/router

Length of output: 225


🏁 Script executed:

rg -n "import.*DEPLOY_URL" examples/react/start-basic/src/

Repository: TanStack/router

Length of output: 254


🏁 Script executed:

rg -n "DEPLOY_URL" examples/react/start-basic/src/ -A 2 -B 2

Repository: TanStack/router

Length of output: 2000


🏁 Script executed:

cat -n examples/react/start-basic/src/routes/users.tsx | head -50

Repository: TanStack/router

Length of output: 1701


🏁 Script executed:

cat -n examples/react/start-basic/src/routes/users.\$userId.tsx | head -50

Repository: TanStack/router

Length of output: 1612


🏁 Script executed:

fd "vite.config" examples/react/start-basic/ || fd "\.env" examples/react/start-basic/

Repository: TanStack/router

Length of output: 100


🏁 Script executed:

cat -n examples/react/start-basic/vite.config.ts

Repository: TanStack/router

Length of output: 670


🏁 Script executed:

fd "\.env" examples/react/start-basic/ -type f

Repository: TanStack/router

Length of output: 229


🏁 Script executed:

cat -n examples/react/start-basic/.env 2>/dev/null || echo "No .env file found"

Repository: TanStack/router

Length of output: 77


Don't hardcode localhost as the shared API base URL.

Line 7 forces all SSR loaders to fetch from http://localhost:3000, which breaks in production deployments. When this app is deployed, the server-side loaders in users.tsx and users.$userId.tsx will fail because they'll still target localhost instead of the actual deployment domain.

Use an environment variable with a sensible development default:

Proposed fix
 export type User = {
   id: number
   name: string
   email: string
 }
 
-export const DEPLOY_URL = 'http://localhost:3000'
+const envDeployUrl = import.meta.env.VITE_DEPLOY_URL?.trim()
+
+export const DEPLOY_URL = (envDeployUrl || 'http://localhost:3000').replace(/\/+$/, '')
📝 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.

Suggested change
export const DEPLOY_URL = 'http://localhost:3000'
const envDeployUrl = import.meta.env.VITE_DEPLOY_URL?.trim()
export const DEPLOY_URL = (envDeployUrl || 'http://localhost:3000').replace(/\/+$/, '')
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/react/start-basic/src/utils/users.tsx` at line 7, Replace the
hardcoded DEPLOY_URL constant so server-side loaders don't always call
http://localhost:3000; update the DEPLOY_URL export in users.tsx to read a
runtime environment variable (e.g., process.env.API_BASE_URL or a
framework-appropriate public env like import.meta.env.VITE_API_BASE_URL) with a
sensible development default of http://localhost:3000, and ensure users.tsx and
users.$userId.tsx reference that DEPLOY_URL; make the change so production
deployments use the env value while local dev falls back to localhost.