--- description: | Add a layout to provide common meta tags, context for application sub routes, and common layout. --- An layout is defined in a `_layout.tsx` file in any sub directory (at any level) under the `routes/` folder. It must contain a default export that is a regular Preact component. Only one such layout is allowed per sub directory. ```txt Project structure └── routes ├── sub │ ├── page.tsx │ └── index.tsx ├── other │ ├── _layout.tsx # will be applied on top of `routes/_layout.tsx` │ └── page.tsx ├── _layout.tsx # will be applied to all routes └── _app.tsx ``` The component to be wrapped is received via props, in addition to a few other things. This allows for the introduction of a global container functioning as a template which can be conditioned based on state and params. Note that any state set by middleware is available via `props.state`. ```tsx routes/sub/_layout.tsx import { PageProps } from "$fresh/server.ts"; export default function Layout({ Component, state }: PageProps) { // do something with state here return (
{data.greeting}
{data.greeting}
Hello world
; } ``` You can skip already inherited layouts inside a layout file: ```tsx routes/special/_layout.tsx import { LayoutConfig } from "$fresh/server.ts"; export const config: LayoutConfig = { skipInheritedLayouts: true, // Skip already inherited layouts }; export default function MyPage() { returnHello world
; } ```