Exercise 3: Correct Not-Found Handling — Possible Solution
====================================================================
THE FIX
------------------------------
// src/pages/[...path].astro
---
import { db } from '../db/client';
import { pages } from '../db/schema';
import { eq } from 'drizzle-orm';
const { path } = Astro.params;
const fullPath = path ?? '';
const [page] = await db.select().from(pages).where(eq(pages.fullPath, fullPath));
if (!page) {
Astro.response.status = 404;
return Astro.rewrite('/404');
}
---
{page.title}
CONFIRMATION
------------------------------
Visiting the same nonexistent path from Exercise 2 now returns a
response carrying a real 404 status code (visible in the browser's
Network tab, not the misleading 200 a plain rewrite alone would
produce), and the browser actually displays src/pages/404.astro's
own content.
WHY THIS WORKS AS AN ANSWER
------------------------------
It correctly combines Astro.response.status = 404 (a real status
code) with Astro.rewrite('/404') (the correct page content), and
correctly confirms both pieces are needed together - rewrite alone
would render the right content behind a wrong 200 status.