Skip to content

fix: public/ assets 404 in PR previews (basePath) + lint guard#276

Merged
nourshoreibah merged 1 commit into
mainfrom
fix/preview-basepath-public-assets
Jul 1, 2026
Merged

fix: public/ assets 404 in PR previews (basePath) + lint guard#276
nourshoreibah merged 1 commit into
mainfrom
fix/preview-basepath-public-assets

Conversation

@nourshoreibah

Copy link
Copy Markdown
Collaborator

Bug

Images (sidebar Branch logo, leaves background, profile icon) don't load in ephemeral PR preview environments.

Cause: previews are served under /pr-<N>/ with Next basePath=/pr-<N>. Next prefixes /_next/* build assets with basePath but not public/ assets referenced by a string src (<Image src="/branch-logo.png" />). So the browser requests /branch-logo.png (root of the shared CloudFront) → 404; the file actually lives at /pr-<N>/branch-logo.png.

Fix

  • next.config.ts: expose NEXT_PUBLIC_BASE_PATH (= /pr-<N>) in preview builds only.
  • src/lib/asset.ts: assetPath("/foo.png") prefixes it with that base path — no-op in prod (empty var).
  • Route the 3 existing public-asset <Image src>s through assetPath().

Lint guard (requested)

Added an ESLint no-restricted-syntax rule that flags any raw leading-slash src string literal, pointing devs at assetPath():

Reference public/ assets via assetPath("/...") from @/lib/asset — a raw "/foo.png" src is not prefixed with basePath and 404s in preview environments (/pr-/).

Verified: fixed files pass, a raw <Image src="/x.png"> is flagged, and 0 violations exist across src/.

Notes

Draft — needs a preview to visually confirm. After merge to main, the throwaway PR #275 (rebased on main) should render the logo correctly under its preview URL.

Prod deploy path is unchanged (NEXT_PUBLIC_BASE_PATH is empty → assetPath returns the path as-is).

Next prefixes /_next/* build assets with basePath but NOT public/ assets
referenced by string src (e.g. <Image src="/branch-logo.png" />), so those
404 in ephemeral previews served under /pr-<N>/ (the file is actually at
/pr-<N>/branch-logo.png). Images were broken in preview environments.

- next.config.ts: expose NEXT_PUBLIC_BASE_PATH (= "/pr-<N>") in preview builds.
- lib/asset.ts: assetPath() prefixes a public path with it (no-op in prod).
- Route the 3 existing public-asset <Image src>s (Navbar logo + leaves bg,
  Header profile icon) through assetPath().
- ESLint: no-restricted-syntax rule flags any raw leading-slash src literal so
  future public-asset refs must use assetPath().

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nourshoreibah nourshoreibah marked this pull request as ready for review July 1, 2026 05:46
github-actions Bot added a commit that referenced this pull request Jul 1, 2026
@nourshoreibah nourshoreibah merged commit e51d45b into main Jul 1, 2026
14 checks passed
@nourshoreibah nourshoreibah deleted the fix/preview-basepath-public-assets branch July 1, 2026 05:46
github-actions Bot added a commit that referenced this pull request Jul 1, 2026
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