caelum0x 9f3f3f4248 fix(ritgram): the real fix for fake-200 routes -- delete root app/loading.tsx, not generateMetadata
Previous commit's generateMetadata-based notFound() fix did not actually
work, confirmed live: curl on a brand-new, never-requested /[owner] path
still returned HTTP 200 after that deploy, but this time the response body
contained `<template data-dgst="NEXT_NOT_FOUND">` -- hard proof Next.js did
resolve the 404 internally, generateMetadata's notFound() included, just
after the 200-status loading-skeleton shell had already started streaming
to the client. Status codes are committed with the first flushed byte;
nothing thrown later (including from generateMetadata) can change them
once a Suspense fallback shell has gone out.

Actual root cause: app/loading.tsx at the app root creates an implicit
Suspense boundary around every route with no more specific loading.tsx of
its own, including /[owner] and /[owner]/[name]. Next streams that
boundary's fallback (the feed-skeleton UI) as an instant 200 before the
async Server Component below it -- and its notFound() call -- resolves.

Fix: delete app/loading.tsx. Checked the blast radius first: feed/
messages/search/settings each already have their own scoped loading.tsx
(Next uses the nearest one, so they're unaffected); every other top-level
route (home, pricing, terms, privacy-policy, explore, discover, trending,
about, docs, import-from-github) is either a 'use client' component that
renders immediately or a server component with no slow data fetch (about
has neither), so none of them actually needed the global skeleton. Left
the generateMetadata additions from the previous commit in place -- they
are correct, harmless, deduped by Next's per-request fetch memoization,
and get the 404 <head> title right if some future loading.tsx narrows
back onto either route.

tsc --noEmit clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-17 04:02:30 +03:00

84 lines
3.7 KiB
XML

/**
* UserProfilePage — /[owner]
*
* Async Server Component: fetches the profile and calls Next's `notFound()`
* BEFORE any HTML is streamed to the client. This is the fix for a real bug
* (found 2026-11-17): the previous version of this page was a `'use client'`
* component that rendered a 200 OK loading skeleton immediately and only
* discovered a missing profile after hydration + a client-side fetch, so a
* plain HTTP request (curl, a crawler, any non-JS client) got a 200 for
* literally any path under `/[owner]` — including paths that don't exist.
* `/[owner]/[name]/page.tsx` (the repo-detail route one level down) already
* used this same async-Server-Component + `notFound()` pattern correctly;
* this brings the top-level profile route in line with it.
*
* Second bug found the same day, live-verified after the first fix deployed:
* the page component's own `notFound()` above was NOT enough on its own.
* `app/loading.tsx` used to exist at the root of the app and applied to
* every route (including this one) with no more specific `loading.tsx` of
* its own, which made Next.js stream an instant 200 OK loading-skeleton
* *shell* before this async component (and its `notFound()` call) had
* resolved — once that shell starts streaming, the response's HTTP status
* is locked at 200, no matter what the deferred content later throws.
* Confirmed live, twice: curl on a brand-new, never-requested `/[owner]`
* value kept returning 200 with the loading-skeleton HTML even with a
* `generateMetadata` below also calling `notFound()` (metadata generation
* turned out not to be exempt from the same deferral here — the response
* body literally contained `<template data-dgst="NEXT_NOT_FOUND">`, proof
* Next.js *did* resolve the 404 internally, just after the 200 shell had
* already gone out). Root `app/loading.tsx` has been deleted — this is the
* actual fix; every other top-level route either has its own scoped
* `loading.tsx` (feed/messages/search/settings, all unaffected by this
* deletion) or renders fast enough not to need one. `generateMetadata`
* below is kept anyway as a harmless, correct defensive check (and gets
* the `<head>` title right for a 404 if any future `loading.tsx` narrows
* back in on this route) — `fetchProfile`'s `fetch()` call is automatically
* memoized by Next.js per request, so it does not double the network call.
*
* All interactive state (follow button, tab switching, lazy-loaded
* posts/repos/liked, contribution-year picker) now lives in the client
* island `ProfileClient`, which receives the already-verified profile as a
* prop — see `./ProfileClient.tsx` and `./profileApi.ts`.
*/
import { notFound } from 'next/navigation'
import { fetchProfile } from './profileApi'
import { ProfileClient } from './ProfileClient'
interface PageProps {
params: { owner: string }
}
export async function generateMetadata({ params }: PageProps) {
const { is404 } = await fetchProfile(params.owner)
if (is404) notFound()
return {}
}
export default async function UserProfilePage({ params }: PageProps) {
const { owner: username } = params
const { profile, is404 } = await fetchProfile(username)
if (is404) notFound()
if (!profile) {
// Fetch failed for a reason other than "doesn't exist" (e.g. backend
// temporarily unreachable) — a transient error, not a 404.
return (
<div
style={{
minHeight: '100vh',
background: '#0F0F1A',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<p style={{ color: '#9994C4' }}>Could not load profile.</p>
</div>
)
}
return <ProfileClient key={username} username={username} initialProfile={profile} />
}