Files
sweet/docs/canary/concepts/layouts.md
T
2025-09-02 10:45:27 +02:00

2.8 KiB

description
description
Add a layout to provide common meta tags, context for application sub routes, and common layout.

A 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.

<project root>
└── 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.

import { define } from "../../utils.ts";

export default define.layout({ Component, state }) => {
  // do something with state here
  return (
    <div class="layout">
      <Component />
    </div>
  );
})

Async layouts

In case you need to fetch data asynchronously before rendering the layout, you can use an async layout to do so.

import { define } from "../../utils.ts";

export default define.layout(async (ctx) => {
  // do something with state here
  const data = await loadData();

  return (
    <div class="layout">
      <p>{data.greeting}</p>
      <ctx.Component />
    </div>
  );
});

Opting out of layout inheritance

Sometimes you want to opt out of the layout inheritance mechanism for a particular route. This can be done via route configuration. Picture a directory structure like this:

└── <root>/routes
    ├── sub
    │   ├── _layout_.tsx
    │   ├── special.tsx  # should not inherit layouts
    │   └── index.tsx
    └── _layout.tsx

To make routes/sub/special.tsx opt out of rendering layouts we can set skipInheritedLayouts: true.

import { type RouteConfig } from "fresh";
import { define } from "../../utils.ts";

export const config: RouteConfig = {
  skipInheritedLayouts: true, // Skip already inherited layouts
};

export default define.layout(() => {
  return <p>Hello world</p>;
});

You can skip already inherited layouts inside a layout file:

import { type LayoutConfig } from "fresh";

export const config: LayoutConfig = {
  skipInheritedLayouts: true, // Skip already inherited layouts
};

export default function MyPage() {
  return <p>Hello world</p>;
}