Page Router
What is the Page Router?
Val's page router allows you to create dynamic routes based on your content. By using s.router(nextAppRouter, schema), you can automatically generate pages for each entry in your content record.
Basic Setup
The documentation you're reading right now is built using Val's page router. Here's how it's structured:
How It Works
The router maps keys in your record to URL paths. Each key becomes a route, and Next.js will generate the corresponding pages automatically.
Links
Typically you also want to define links to pages in your application. You can do this using s.route(). Read more about it here.
Rendering Routes in Server Components
In server components, use fetchValRoute to fetch route content based on URL parameters. This function is async and returns the content for the matching route, or null if no route matches.
Server Component Setup
Make sure to set up fetchValRoute in your val/val.rsc.ts file:
Rendering Routes in Client Components
In client components, use the useValRoute hook to access route content. This hook takes the Val module and the params Promise from Next.js and returns the content for the matching route.
Client Component Setup
Make sure to set up useValRoute in your val/val.client.ts file:
Choosing Between Server and Client Components
Use server components (fetchValRoute) when:
The page content is mostly static
You want better SEO and initial page load performance
You don't need client-side interactivity for the content
Use client components (useValRoute) when:
You need client-side interactivity
You want real-time content updates in Val Studio
The page has other client-side state or effects