Skip to content

sitemap.ts and robots.ts silently fall back to http://localhost:3000 when NEXT_PUBLIC_SITE_URL is unset #304

Description

@Jagadeeshftw

📌 Description

Both sitemap() (src/app/sitemap.ts) and robots() (src/app/robots.ts) compute const baseUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000'; with no further check. If a production deployment forgets to set NEXT_PUBLIC_SITE_URL (an easy omission — it isn't validated anywhere at build or request time), the live sitemap.xml and robots.txt would silently render every URL and the sitemap: directive pointing at http://localhost:3000, which is useless to search engines and actively wrong in production, with no error, warning, or build failure to surface the misconfiguration.

🧩 Requirements and context

  • A missing NEXT_PUBLIC_SITE_URL in a non-local environment should be surfaced (e.g. a console warning at request time identifying which route emitted it) rather than silently producing a localhost URL with no indication anything is wrong.
  • Local development (no NEXT_PUBLIC_SITE_URL set, e.g. via next dev) must continue to work unchanged, falling back to http://localhost:3000 without requiring the variable to be set.
  • The existing route shapes, priorities, and robots.ts's rules/sitemap fields must be unchanged for the common case where the variable is set correctly.

🛠️ Suggested execution

  • In src/app/sitemap.ts and src/app/robots.ts, detect when NEXT_PUBLIC_SITE_URL is unset while process.env.NODE_ENV === "production" and emit a console.warn (or equivalent) naming the missing variable and the affected route, before falling back to the existing http://localhost:3000 default.
  • Extend src/app/sitemap.test.ts and src/app/robots.test.ts with a case that stubs NODE_ENV to "production" with no NEXT_PUBLIC_SITE_URL set and asserts a warning is logged, plus a case confirming no warning is logged in development.

✅ Acceptance criteria

  • A missing NEXT_PUBLIC_SITE_URL in production logs a clear warning identifying the affected route.
  • Local development behavior (silent localhost fallback, no warning) is unchanged.
  • Test coverage for both the warned (production, unset) and unwarned (development, or set) cases.

🔒 Security notes

No new attack surface; a build/deployment-configuration hardening fix that surfaces a misconfiguration instead of silently shipping a broken sitemap/robots file.

📋 Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

GrantFox OSSIssue tracked in GrantFox OSSMaybe RewardedIssue may be eligible for a GrantFox rewardOfficial Campaign | FWC26Campaign: Official Campaign | FWC26bugSomething isn't workingfrontendFrontend / UI work

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions