Skip to content

@paretojs/core

The main entry point for Pareto’s runtime API.

import {
Link,
Await,
ParetoErrorBoundary,
useLoaderData,
useRouter,
useStreamData,
defer,
redirect,
notFound,
} from '@paretojs/core'
import type {
HeadProps,
HeadComponent,
DocumentContext,
HtmlAttributes,
GetDocumentProps,
LoaderContext,
LoaderFunction,
NavigateOptions,
ParetoConfig,
} from '@paretojs/core'

Client-side navigation link. Intercepts clicks for same-origin navigation. See File-Based Routing for how routes are defined.

<Link href="/about">About</Link>
<Link href="/blog" prefetch="viewport">Blog</Link>
<Link href="/login" replace>Login</Link>
PropTypeDefaultDescription
hrefstringrequiredTarget URL
prefetch'hover' | 'viewport' | 'none''hover'Prefetch strategy
replacebooleanfalseReplace history entry
scrollbooleantrueScroll to top on navigation

Renders deferred data from defer(). Shows fallback until the promise resolves. See Streaming SSR for usage patterns and error handling.

<Await resolve={data.feed} fallback={<Skeleton />}>
{(feed) => <Feed items={feed} />}
</Await>

React error boundary for catching render errors. Wrap any section of your UI that might throw. See Error Handling for usage patterns and nested boundary strategies.

<ParetoErrorBoundary fallback={({ error }) => <p>{error.message}</p>}>
<RiskyComponent />
</ParetoErrorBoundary>
PropTypeDescription
fallbackReact.ComponentType<{ error: Error }>Component to render when an error is caught
childrenReactNodeContent to render normally

Access data returned by the route’s loader function. The generic type parameter provides type safety for the returned data.

const data = useLoaderData<{ user: User }>()

Access the router state and navigation methods. Useful for programmatic navigation, active link styling, and navigation state.

const { pathname, params, isNavigating, push, replace, back, prefetch } = useRouter()
PropertyTypeDescription
pathnamestringCurrent URL path
paramsRecord<string, string>Dynamic route parameters
isNavigatingbooleantrue during navigation transitions
push(url, opts?)(url: string, opts?: NavigateOptions) => voidNavigate to URL (adds history entry)
replace(url, opts?)(url: string, opts?: NavigateOptions) => voidNavigate to URL (replaces history entry)
back()() => voidNavigate back in history
prefetch(url)(url: string) => voidPrefetch a route’s loader data

NavigateOptions accepts { replace?: boolean, scroll?: boolean }. For example, push('/page', { scroll: false }) navigates without scrolling to top.

Hook to consume a deferred value without <Await>. Suspends the component until the promise resolves, so it must be used inside a <Suspense> boundary.

function Activity({ data }: { data: Promise<Items> | Items }) {
const items = useStreamData(data)
return <div>{items.length} items</div>
}

Wrap loader return value for streaming. Resolved values are sent immediately; promises stream in via Suspense. See Streaming SSR for detailed usage.

return defer({
instant: { count: 42 },
streamed: fetchSlowData(),
})

Throw in a loader to trigger an HTTP redirect. Default status: 302. See Redirect & 404 for common patterns like auth guards and URL migrations.

throw redirect('/login')
throw redirect('/new-url', 301)

Throw in a loader to render not-found.tsx with 404 status. See Redirect & 404 for the difference between notFound() and throwing errors.

throw notFound()

The context object passed to every loader and action function. Provides access to the Express request/response and route parameters.

interface LoaderContext {
req: Request // Express request
res: Response // Express response
params: Record<string, string>
}

The type for loader functions exported from page.tsx or loader.ts files.

type LoaderFunction = (context: LoaderContext) => unknown

Props passed to Head components in head.tsx files. See Head Management for usage patterns.

interface HeadProps {
loaderData: unknown
params: Record<string, string>
}

The type for Head components exported from head.tsx files.

type HeadComponent = (props: HeadProps) => ReactNode

The context object passed to getDocumentProps in document.tsx.

interface DocumentContext {
req: Request
params: Record<string, string>
pathname: string
loaderData: unknown
}

The return type of getDocumentProps. All properties are applied as attributes on the <html> element. Common attributes lang, dir, and className are typed explicitly for convenience.

type HtmlAttributes = Record<string, string> & {
lang?: string
dir?: string
className?: string
}

The function type for document.tsx exports.

type GetDocumentProps = (ctx: DocumentContext) => HtmlAttributes

See Document Customization for document.tsx usage and Error Handling for error.tsx usage.