Remote Routes (Build‑time Fetch)
Fetch external data during build in data().
Example: /posts
// src-api/posts/index.js
export default async function data() {
const res = await fetch('https://jsonplaceholder.typicode.com/posts?_limit=5', {
headers: { Accept: 'application/json' },
cache: 'no-store',
});
if (!res.ok) return { error: 'Upstream HTTP ' + res.status };
const posts = await res.json();
return { count: posts.length, posts, generatedAt: new Date().toISOString() };
}
Example: /posts/:id (dynamic + remote)
// src-api/posts/[id].js
export async function paths() {
return ['1', '2', '3'];
}
export async function data({ params }) {
const res = await fetch('https://jsonplaceholder.typicode.com/posts/' + params.id, {
headers: { Accept: 'application/json' },
cache: 'no-store',
});
if (!res.ok) return { error: 'Upstream HTTP ' + res.status, id: params.id };
const post = await res.json();
return { id: params.id, post, generatedAt: new Date().toISOString() };
}
Remote fetches run at build time, and their responses are baked into static JSON.
That means:
- regular CLI builds fetch remote data when you run
statikapi build - regular CLI dev mode refetches when a rebuild happens
- Cloudflare public routes refetch on
build+deploy - Cloudflare private routes can refetch on authenticated private rebuild webhooks
This is the key mental model:
- the route is generated ahead of reads
- the fetch does not happen on every client request