getServerData on every request,
getStaticData during static generation and cacheable production requests, or
fetch from a client component. Each one has its place.
Examples below use the default app router. Set router: "pages" in veryfront.config.ts to switch to the pages router.
Prerequisites
- A project with at least one page (see Pages and routing).
- A data source you can call from server code, build-time scripts, or the browser (REST API, database, or in-memory data).
Server data
getServerData runs on every request. Use it when data depends on the request (auth, query params, cookies):
veryfront dev and open http://localhost:3000/dashboard?name=Grace. The page should render Welcome, Grace.
getServerData, getStaticData, and getStaticPaths are reserved server data
export names in browser project modules. Veryfront strips their bodies from
browser bundles, and imports used exclusively by those stripped hooks are removed
entirely, including their top-level side effects. Put client initialization in a
separate client-referenced module or a bare side-effect import that is not only
used by a server data hook.
The props you return are passed to the page component. To read the same props
data from a layout or nested component without prop-drilling, use
usePageContext().data (see
Pages and routing). Veryfront serializes that data
into hydration markup and restores it after client-side navigation; do not rely
on JavaScript object identity surviving serialization.
The DataContext provides:
Static data
getStaticData supplies cacheable data for static builds and production
requests. Use it for content that does not depend on request headers, cookies,
or request bodies:
getStaticData with getStaticPaths to tell the framework which pages to generate.
getStaticData receives params and url. It does not receive request,
request headers, cookies, a body, or a separate query property. Read query
parameters from url.searchParams; the complete URL, including the query
string, participates in static cache identity.
Revalidate static data
Setrevalidate to a finite, non-negative number of seconds to refresh static
data after it becomes stale:
revalidate or set it to false to
disable background refreshes.
Redirects and 404s
Returnredirect() or notFound() from getServerData or getStaticData:
redirect() accepts an optional second argument for permanent redirects:
throw notFound() and throw redirect(...) behave exactly like returning them, which is useful inside a helper that has no clean way to return to the data function:
notFound() and redirect() produce are read as control flow. Every other thrown value is an error, including an object that happens to carry a notFound property, such as a parsed error body from an upstream API.
Client-side fetching
For data that loads after the page renders, fetch in a client component:Verify it worked
- For
getServerData, hit the page withcurl http://localhost:3000/<path>and confirm the response contains the value you returned inprops. - For
getStaticData, runveryfront buildand inspect the generated HTML for the page. The HTML should contain the static value rather than a client-side fetch loop. - For client-side fetching, open the browser dev tools network tab. The request should fire after the page paints and the rendered output should match the response.