Repository navigation
fix(web): inline the build-time site origin for the function runtime - #245
Conversation
`siteUrl.ts` falls back to Netlify's `URL` / `DEPLOY_PRIME_URL`, which only exist at build. The function runtime that serves ISR regenerations and route handlers lacks them, so canonicals and the sitemap fell back to `http://localhost:3000` on every deploy without `NEXT_PUBLIC_SITE_URL`, deploy previews included. `next.config.ts` now resolves the origin at build and inlines it as `NEXT_PUBLIC_SITE_URL`. Netlify's production context now always takes `URL`: a staging site on the development dataset (`SANITY_STUDIO_DEPLOYMENT_TARGET=development`) serves the primary domain, where `DEPLOY_PRIME_URL` is the `<branch>--` host. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
✅ Deploy Preview for bef-next-sanity-starter ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
🔵 Needs a closer look
The build-to-function handoff central to this fix remains unverified on a real Netlify deployment.
No open issues 🎉
What changed in this PR
This PR preserves the build-time site origin for later server renders, preventing Netlify metadata and sitemap URLs from falling back to localhost when runtime host variables are unavailable.
Changes:
- Inlines the resolved origin as
NEXT_PUBLIC_SITE_URLthrough Next.js configuration. - Uses Netlify’s primary domain in production context, including staging sites using the development dataset.
- Updates comments to explain origin resolution and deployment behavior.
| File | Description |
|---|---|
web/src/utils/siteUrl.ts |
Adjusts Netlify origin selection and documents build-time resolution. |
web/next.config.ts |
Inlines the resolved site origin for application runtime use. |
packages/sanity-dataset-resolve/src/index.ts |
Updates documentation for the Netlify canonical-origin exception. |
Review effort: Balanced
Runtime check: build env ≠ runtime envSimulated the Netlify function runtime locally to check that the origin survives an ISR regeneration. Setup, run for both the merge-base (
Result
On main the bug reproduces: without Not covered: |
On Netlify, canonicals,
hreflang,og:urland the sitemap fell back tohttp://localhost:3000on any deploy withoutNEXT_PUBLIC_SITE_URL, deploy previews always.siteUrl.tsreads Netlify'sURL/DEPLOY_PRIME_URLat module load, but those only exist at build: the function runtime that serves ISR regenerations and route handlers doesn't have them. Found in a project built from the starter.1. Inline the build-time origin (
web/next.config.ts)next.config.tsimportsSITE_BASE_URLand setsNEXT_PUBLIC_SITE_URL: SITE_BASE_URLinenv, so the value resolved at build is what every later render reads. An explicitNEXT_PUBLIC_SITE_URLstill wins; a deploy host with nothing resolvable now fails at config load instead of later.2. Production context takes
URL(web/src/utils/siteUrl.ts)netlifyOrigin()usesURLwhenisProduction || CONTEXT === "production". A staging site on the development dataset (SANITY_STUDIO_DEPLOYMENT_TARGET=developmenton the production context) serves its primary domain;DEPLOY_PRIME_URLthere is the<branch>--host. Doc comments insiteUrl.tsandisProductionDeploymentupdated to match.Verified
URL; production + targetdevelopment→URL; deploy preview / branch deploy →DEPLOY_PRIME_URL; runtime with only the inlined value → that value; Netlify with nothing → throws; local →http://localhost:3000.next buildwithout any host variables succeeds;required-server-files.jsoncarriesNEXT_PUBLIC_SITE_URL: "http://localhost:3000".pnpm typecheck,pnpm lint,pnpm format(no diff).hreflang,og:urlandsitemap.xmlusehttps://deploy-preview-245--…. So did preview fix: Presentation connects again from the Sanity Dashboard #243 before the fix: these pages are prerendered at build, where the old code already resolved.Not verified: the runtime path the fix targets (an ISR regeneration or route handler on Netlify). It needs a revalidation on a deployed site.
Variant-Port: #246
🤖 Generated with Claude Code