What you’ll build
- A Server Component request with cache: no-store.
- A Client Component with refresh and error states.
- Explicit local and preview environment configuration.
- Checks preventing production from using mock data.
01
Create a predictable catalog contract
Create a project named catalog-preview and add GET /v1/catalog with 200 OK. A static list is ideal for building the App Router page because each render receives the same known shape.
Add separate /v1/catalog-empty and /v1/catalog-error routes for edge states. Mockport provides up to 15 routes per project, so descriptive scenario paths remain easy to scan.
{
"items": [
{ "id": "prd_01", "name": "Field Notes", "price": 18 },
{ "id": "prd_02", "name": "Desk Calendar", "price": 24 }
]
}02
Configure the API URL by environment
Browser-visible requests need a NEXT_PUBLIC_ variable. Use the hosted Mockport domain in .env.local and set the same key to a dedicated mock domain in preview deployment settings.
Server-only requests can use an unprefixed API_URL instead. Public variables are not secrets and are embedded into client bundles at build time. Do not reuse a production API value as a hidden fallback.
NEXT_PUBLIC_API_URL=https://catalog-preview.mockport.dev
APP_ENV=localProduction should receive its own explicit service URL. Keep mock and production domains in separate deployment scopes.
03
Fetch in a Server Component with no-store
A Server Component can request the hosted endpoint before rendering HTML. Setting cache to no-store makes the intent explicit during development and prevents a previous response from hiding a route change.
Check response.ok before decoding the body. The thrown message reaches the route error UI and keeps unexpected statuses from masquerading as empty data.
type Catalog = {
items: Array<{ id: string; name: string; price: number }>;
};
async function getCatalog(): Promise<Catalog> {
const base = process.env.NEXT_PUBLIC_API_URL;
if (!base) throw new Error("NEXT_PUBLIC_API_URL is missing");
const response = await fetch(`${base}/v1/catalog`, {
cache: "no-store",
});
if (!response.ok) {
throw new Error(`Catalog failed: ${response.status}`);
}
return response.json() as Promise<Catalog>;
}
export default async function CatalogPage() {
const catalog = await getCatalog();
return (
<main>
<h1>Catalog</h1>
{catalog.items.length === 0 ? <p>No products yet.</p> : (
<ul>
{catalog.items.map(item => (
<li key={item.id}>{item.name}: ${item.price}</li>
))}
</ul>
)}
</main>
);
}04
Add a Client Component refresh and error state
Use a Client Component when the review needs a visible refresh control. Track loading and error text independently so a failed refresh does not erase the last successful catalog unless that behavior is intentional.
The refresh function points at the same environment value, checks the selected status, and exposes a concise failure message beside the action.
"use client";
import { useState } from "react";
type Item = { id: string; name: string; price: number };
export default function CatalogRefresh() {
const [items, setItems] = useState<Item[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
async function refresh() {
setLoading(true);
setError(null);
try {
const base = process.env.NEXT_PUBLIC_API_URL;
if (!base) throw new Error("API URL is missing");
const response = await fetch(`${base}/v1/catalog`, {
cache: "no-store",
});
if (!response.ok) throw new Error(`Status ${response.status}`);
setItems((await response.json()).items);
} catch (error) {
setError(error instanceof Error ? error.message : "Refresh failed");
} finally {
setLoading(false);
}
}
return (
<section>
<button onClick={refresh} disabled={loading}>
{loading ? "Loading…" : "Refresh catalog"}
</button>
{error && <p role="alert">{error}</p>}
<ul>{items.map(item => <li key={item.id}>{item.name}</li>)}</ul>
</section>
);
}05
Exercise empty, 404, and 500 statuses
Point the page at each scenario route through configuration or a small test helper. An empty collection should return 200 with an empty items array, because it is a successful response rather than a missing resource.
Use 404 for a missing item and 500 for the error route. Confirm the route-level error UI, retry action, and user-facing language are distinct from the valid empty state.
- 200 with items renders the grid.
- 200 with [] renders the empty catalog message.
- 404 names the missing resource.
- 500 exposes a retry path without leaking internal details.
06
Promote safely to previews and production
Assign catalog-preview.mockport.dev only to development and preview scopes. A pull request build can share stable fixtures with designers and testers without reaching a private machine.
Before production promotion, verify that an explicit production API URL is present and is not a Mockport project domain. Use your deployment’s environment label—not NODE_ENV alone—to distinguish previews from production.
const raw = process.env.NEXT_PUBLIC_API_URL;
if (!raw) throw new Error("API URL is required");
const host = new URL(raw).hostname;
const production = process.env.APP_ENV === "production";
if (
production &&
(host === "mockport.dev" || host.endsWith(".mockport.dev"))
) {
throw new Error("Production cannot use a Mockport project domain");
}Keep one named variable everywhere, but give each deployment scope an explicit value.
Keep the contract predictable, then keep building.
Your App Router page now has deterministic server requests, an optional client refresh path, and explicit environment boundaries. Preview reviews can use hosted fixtures while production remains separate.
Put your next endpoint online in minutes.
Start a 14-day trial. Team workspaces are $9/month with 5 project domains, 15 routes per project, and up to 5 seats.
Start free trial →