Skip to content

Fix(start-basic): Add deploy url to fix SSR fetch bug#7098

Open
g3org3 wants to merge 1 commit intoTanStack:mainfrom
g3org3:fix/start_basic_example_fetch_in_ssr_bug
Open

Fix(start-basic): Add deploy url to fix SSR fetch bug#7098
g3org3 wants to merge 1 commit intoTanStack:mainfrom
g3org3:fix/start_basic_example_fetch_in_ssr_bug

Conversation

@g3org3
Copy link
Copy Markdown

@g3org3 g3org3 commented Apr 4, 2026

The Problem

I ran the start-basic example but when i refresh the page under /users or /users/9 is not working, it is failing at the fetch

npx gitpick TanStack/router/tree/main/examples/react/start-basic start-basic
cd start-basic
npm install
npm run dev
Screenshot 2026-04-04 at 13 37 34

It was there previously but it was removed I am not sure why.

8b5accb?w=1#diff-78bdb6a3013dc993b00e687f00f3d23bb60c5fd036c6ccc3d2a9b4aee5143690

issue

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 4, 2026

📝 Walkthrough

Walkthrough

The PR updates API endpoint calls in two route loaders to use a configurable DEPLOY_URL base URL constant instead of hard-coded relative paths. A new DEPLOY_URL constant is added to the utils module, pointing to http://localhost:3000.

Changes

Cohort / File(s) Summary
Route loaders with configurable endpoints
examples/react/start-basic/src/routes/users.tsx, examples/react/start-basic/src/routes/users.$userId.tsx
Modified API fetch calls to prepend DEPLOY_URL constant to endpoint paths, replacing relative URLs with configurable base URL references.
Utilities exporting deployment configuration
examples/react/start-basic/src/utils/users.tsx
Added new exported constant DEPLOY_URL set to 'http://localhost:3000'.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 Hopping through the config trees,
We've caught the URLs with ease!
From hardcoded paths so tight,
To DEPLOY_URL shining bright,
Now endpoints flex left and right! 🌍

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title accurately describes the main change: adding a deploy URL constant to fix an SSR fetch bug in the start-basic example.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@g3org3 g3org3 changed the title fix: Add deploy url to fix SSR fetch bug Fix(start-basic): Add deploy url to fix SSR fetch bug Apr 4, 2026
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@examples/react/start-basic/src/routes/users`.$userId.tsx:
- 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.

In `@examples/react/start-basic/src/utils/users.tsx`:
- 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.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8782aaa2-ba11-432c-a5d6-4a84cf4fe5a5

📥 Commits

Reviewing files that changed from the base of the PR and between 5a81726 and 2009c25.

📒 Files selected for processing (3)
  • examples/react/start-basic/src/routes/users.$userId.tsx
  • examples/react/start-basic/src/routes/users.tsx
  • examples/react/start-basic/src/utils/users.tsx

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant