diff --git a/docs/canary/concepts/app-wrapper.md b/docs/canary/concepts/app-wrapper.md
new file mode 100644
index 00000000..47a96c34
--- /dev/null
+++ b/docs/canary/concepts/app-wrapper.md
@@ -0,0 +1,104 @@
+---
+description: |
+ Add a global app wrapper to provide common meta tags or context for application routes.
+---
+
+An app wrapper is defined in an `_app.tsx` file in `routes/` folder and is
+typically used to create the outer structure of an HTML document. It must
+contain a default export that is a regular Preact component. Only one such
+wrapper is allowed per application.
+
+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/_app.tsx
+import { AppProps } from "$fresh/server.ts";
+
+export default function App({ Component, state }: AppProps) {
+ // do something with state here
+ return (
+
+
+
@@ -37,6 +37,48 @@ export default function Layout({ Component, state }: LayoutProps) {
}
```
+## Async layouts
+
+In case you need to fetch data asynchronously before rendering the layout, you
+can use an async layout to do so.
+
+```tsx routes/sub/_layout.tsx
+import { LayoutProps } from "$fresh/server.ts";
+
+export default async function Layout(req: Request, ctx: LayoutContext) {
+ // do something with state here
+ const data = await loadData();
+
+ return (
+
+ );
+}
+```
+
+### Define helper
+
+To make it a little quicker to write async layouts, Fresh ships with a
+`defineLayout` helper which automatically infers the correct types for the
+function arguments.
+
+```tsx
+import { defineLayout } from "$fresh/server.ts";
+
+export default defineLayout(async (req, ctx) => {
+ const data = await loadData();
+
+ return (
+
+ );
+});
+```
+
## Opting out of layout inheritance
Sometimes you want to opt out of the layout inheritance mechanism for a
@@ -53,13 +95,13 @@ structure like this:
```
To make `routes/sub/special.tsx` opt out of rendering layouts we can set
-`rootLayout: true`.
+`skipInheritedLayouts: true`.
```tsx routes/sub/special.tsx
import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
- rootLayout: true, // Mark this route as the root layout
+ skipInheritedLayouts: true, // Skip already inherited layouts
};
export default function MyPage() {
@@ -67,18 +109,13 @@ export default function MyPage() {
}
```
-Note that you can also mark layouts as root layouts.
+You can skip already inherited layouts inside a layout file:
-## Disabling the root `_app` template
+```tsx routes/special/_layout.sx
+import { LayoutConfig } from "$fresh/server.ts";
-In very rare cases you might want to disable the root `_app` template for a
-particular route. This can be done by setting `appTemplate: false`.
-
-```tsx
-import { RouteConfig } from "$fresh/server.ts";
-
-export const config: RouteConfig = {
- appTemplate: false, // Disable the `_app` template
+export const config: LayoutConfig = {
+ skipInheritedLayouts: true, // Skip already inherited layouts
};
export default function MyPage() {
diff --git a/docs/canary/concepts/route-groups.md b/docs/canary/concepts/route-groups.md
deleted file mode 100644
index 8f12dc31..00000000
--- a/docs/canary/concepts/route-groups.md
+++ /dev/null
@@ -1,58 +0,0 @@
----
-description: |
- Route groups allow you to organize your route segments into groups and share layout or middleware code without affecting the URL structure.
----
-
-When working with [route layouts](/docs/canary/concepts/route-layout) or
-[route middlewares](/docs/canary/concepts/middleware) you'll sometimes come
-across a situation where you want your routes to inherit from a different layout
-than another route that's in the same URL segment.
-
-Let's illustrate that with an example:
-
-```txt
-/about -> layout A
-/career -> layout A
-/archive -> layout B
-/contact -> layout B
-```
-
-Without any way to group routes this is a problem because every route segment
-can only have one `_layout` file.
-
-```txt Project structure
-└── routes
- ├── _layout.tsx # applies to all routes here :(
- ├── about.tsx
- ├── career.tsx
- ├── archive.tsx
- └── contact.tsx
-```
-
-We can solve this problem with route groups. A route group is a folder which has
-a name that is wrapped in braces. For example `(pages)` would be considered a
-route and so would be `(marketing)`. This enables us to group related routes in
-a folder and use a different `_layout` file for each group.
-
-```txt Project structure
-└── routes
- ├── (marketing)
- │ ├── _layout.tsx # only applies to about.tsx and career.tsx
- │ ├── about.tsx
- │ └── career.tsx
- └── (info)
- ├── _layout.tsx # only applies to archive.tsx and contact.tsx
- ├── archive.tsx
- └── contact.tsx
-```
-
-> ℹ️ Be careful about routes in different groups which match to the same URL.
-> Such scenarios will lead to ambiguity as to which route file should be picked.
->
-> ```txt Project structure
-> └── routes
-> ├── (group-1)
-> │ └── about.tsx # Bad: Maps to same `/about` url
-> └── (group-2)
-> └── about.tsx # Bad: Maps to same `/about` url
-> ```
diff --git a/docs/latest/concepts/routes.md b/docs/latest/concepts/routes.md
index 1c265823..b517c5ef 100644
--- a/docs/latest/concepts/routes.md
+++ b/docs/latest/concepts/routes.md
@@ -165,3 +165,23 @@ export default async function MyPage(req: Request, ctx: RouteContext) {
return
foo is: {value}
;
}
```
+
+### Define helper
+
+To make it a little quicker to write async routes, Fresh ships with a
+`defineRoute` helper which automatically infers the correct types for the
+function arguments.
+
+```tsx
+import { defineRoute } from "$fresh/server.ts";
+
+export default defineRoute(async (req, ctx) => {
+ const data = await loadData();
+
+ return (
+
+
Hello {data.name}
+
+ );
+});
+```
diff --git a/docs/latest/concepts/routing.md b/docs/latest/concepts/routing.md
index 773f4acd..da49e0bd 100644
--- a/docs/latest/concepts/routing.md
+++ b/docs/latest/concepts/routing.md
@@ -51,4 +51,60 @@ export const config: RouteConfig = {
// ...
```
+## Route Groups
+
+When working with [route layouts](/docs/canary/concepts/route-layout) or
+[route middlewares](/docs/canary/concepts/middleware), you'll sometimes come
+across a situation where you want your routes to inherit from a layout other
+than what's suggested by the URL segment.
+
+Let's illustrate that with an example:
+
+```txt
+/about -> layout A
+/career -> layout A
+/archive -> layout B
+/contact -> layout B
+```
+
+Without any way to group routes this is a problem because every route segment
+can only have one `_layout` file.
+
+```txt Project structure
+└── routes
+ ├── _layout.tsx # applies to all routes here :(
+ ├── about.tsx
+ ├── career.tsx
+ ├── archive.tsx
+ └── contact.tsx
+```
+
+We can solve this problem with route groups. A route group is a folder which has
+a name that is wrapped in braces. For example `(pages)` would be considered a
+route and so would be `(marketing)`. This enables us to group related routes in
+a folder and use a different `_layout` file for each group.
+
+```txt Project structure
+└── routes
+ ├── (marketing)
+ │ ├── _layout.tsx # only applies to about.tsx and career.tsx
+ │ ├── about.tsx
+ │ └── career.tsx
+ └── (info)
+ ├── _layout.tsx # only applies to archive.tsx and contact.tsx
+ ├── archive.tsx
+ └── contact.tsx
+```
+
+> ℹ️ Be careful about routes in different groups which match to the same URL.
+> Such scenarios will lead to ambiguity as to which route file should be picked.
+>
+> ```txt Project structure
+> └── routes
+> ├── (group-1)
+> │ └── about.tsx # Bad: Maps to same `/about` url
+> └── (group-2)
+> └── about.tsx # Bad: Maps to same `/about` url
+> ```
+
[urlpattern]: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
diff --git a/docs/toc.ts b/docs/toc.ts
index e1430ec2..7db10e66 100644
--- a/docs/toc.ts
+++ b/docs/toc.ts
@@ -46,20 +46,19 @@ const toc: RawTableOfContents = {
pages: [
["architecture", "Architecture", "link:latest"],
["server-components", "Server Components", "link:latest"],
+ ["routing", "Routing", "link:latest"],
["routes", "Routes", "link:latest"],
+ ["app-wrapper", "App wrapper", "link:canary"],
+ ["layouts", "Layouts", "link:canary"],
["forms", "Forms", "link:latest"],
["islands", "Interactive islands", "link:latest"],
["static-files", "Static files", "link:latest"],
- ["middleware", "Route middleware", "link:canary"],
+ ["middleware", "Middlewares", "link:canary"],
["error-pages", "Error pages", "link:latest"],
- ["routing", "Routing", "link:latest"],
["data-fetching", "Data fetching", "link:latest"],
["deployment", "Deployment", "link:latest"],
["plugins", "Plugins", "link:latest"],
["updating", "Updating Fresh", "link:latest"],
- ["app-wrapper", "Application wrapper", "link:latest"],
- ["route-layout", "Route layouts", "link:canary"],
- ["route-groups", "Route groups", "link:canary"],
["server-configuration", "Server configuration", "link:latest"],
],
},
@@ -72,7 +71,6 @@ const toc: RawTableOfContents = {
link: "latest",
pages: [
["modifying-the-head", "Modifying the ", "link:latest"],
- ["setting-the-language", "Setting the language", "link:latest"],
["writing-tests", "Writing tests", "link:latest"],
[
"changing-the-src-dir",
diff --git a/src/server/context.ts b/src/server/context.ts
index 542ee360..ff5568ba 100644
--- a/src/server/context.ts
+++ b/src/server/context.ts
@@ -253,7 +253,7 @@ export class ServerContext {
component,
handler,
csp: Boolean(config?.csp ?? false),
- appTemplate: !config?.skipAppTemplate,
+ appWrapper: !config?.skipAppWrapper,
inheritLayouts: !config?.skipInheritedLayouts,
};
routes.push(route);
@@ -274,7 +274,7 @@ export class ServerContext {
baseRoute: toBaseRoute(baseRoute),
handler: mod.handler,
component: mod.default,
- appTemplate: !config?.skipAppTemplate,
+ appWrapper: !config?.skipAppWrapper,
inheritLayouts: !config?.skipInheritedLayouts,
});
} else if (
@@ -295,7 +295,7 @@ export class ServerContext {
component,
handler: handler ?? ((req) => router.defaultOtherHandler(req)),
csp: Boolean(config?.csp ?? false),
- appTemplate: !config?.skipAppTemplate,
+ appWrapper: !config?.skipAppWrapper,
inheritLayouts: !config?.skipInheritedLayouts,
};
} else if (
@@ -317,7 +317,7 @@ export class ServerContext {
handler: handler ??
((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)),
csp: Boolean(config?.csp ?? false),
- appTemplate: !config?.skipAppTemplate,
+ appWrapper: !config?.skipAppWrapper,
inheritLayouts: !config?.skipInheritedLayouts,
};
}
@@ -982,7 +982,7 @@ const DEFAULT_NOT_FOUND: UnknownPage = {
name: "_404",
handler: (req) => router.defaultOtherHandler(req),
csp: false,
- appTemplate: true,
+ appWrapper: true,
inheritLayouts: true,
};
@@ -994,7 +994,7 @@ const DEFAULT_ERROR: ErrorPage = {
component: DefaultErrorHandler,
handler: (_req, ctx) => ctx.render(),
csp: false,
- appTemplate: true,
+ appWrapper: true,
inheritLayouts: true,
};
diff --git a/src/server/render.ts b/src/server/render.ts
index e5d8e4ce..26ab547e 100644
--- a/src/server/render.ts
+++ b/src/server/render.ts
@@ -265,8 +265,8 @@ export async function render
(
const renderStack: any[] = [];
// Check if appLayout is enabled
if (
- opts.route.appTemplate &&
- layouts.every((layout) => layout.appTemplate)
+ opts.route.appWrapper &&
+ layouts.every((layout) => layout.appWrapper)
) {
renderStack.push(opts.app.default);
}
diff --git a/src/server/types.ts b/src/server/types.ts
index 2aae260f..3051cac5 100644
--- a/src/server/types.ts
+++ b/src/server/types.ts
@@ -98,7 +98,7 @@ export interface RouteConfig {
* Skip rendering the `routes/_app` template
* Default: `false`
*/
- skipAppTemplate?: boolean;
+ skipAppWrapper?: boolean;
}
// deno-lint-ignore no-empty-interface
@@ -173,7 +173,7 @@ export interface Route {
component?: PageComponent | AsyncLayout | AsyncRoute;
handler: Handler | Handlers;
csp: boolean;
- appTemplate: boolean;
+ appWrapper: boolean;
inheritLayouts: boolean;
}
@@ -218,7 +218,7 @@ export interface LayoutConfig {
* Skip already inherited layouts
* Default: `false`
*/
- skipAppTemplate?: boolean;
+ skipAppWrapper?: boolean;
/**
* Skip rendering the `routes/_app`.
* Default: `false`
@@ -238,7 +238,7 @@ export interface LayoutRoute {
// deno-lint-ignore no-explicit-any
handler?: Handler | Handlers;
component: LayoutModule["default"];
- appTemplate: boolean;
+ appWrapper: boolean;
inheritLayouts: boolean;
}
@@ -286,7 +286,7 @@ export interface UnknownPage {
component?: PageComponent;
handler: UnknownHandler;
csp: boolean;
- appTemplate: boolean;
+ appWrapper: boolean;
inheritLayouts: boolean;
}
@@ -333,7 +333,7 @@ export interface ErrorPage {
component?: PageComponent;
handler: ErrorHandler;
csp: boolean;
- appTemplate: boolean;
+ appWrapper: boolean;
inheritLayouts: boolean;
}
diff --git a/tests/fixture_layouts/fresh.gen.ts b/tests/fixture_layouts/fresh.gen.ts
index 9571b43d..db696282 100644
--- a/tests/fixture_layouts/fresh.gen.ts
+++ b/tests/fixture_layouts/fresh.gen.ts
@@ -23,17 +23,16 @@ import * as $17 from "./routes/foo/_layout.tsx";
import * as $18 from "./routes/foo/bar.tsx";
import * as $19 from "./routes/foo/index.tsx";
import * as $20 from "./routes/index.tsx";
-import * as $21 from "./routes/no_app.tsx";
-import * as $22 from "./routes/other.tsx";
-import * as $23 from "./routes/override/_layout.tsx";
-import * as $24 from "./routes/override/index.tsx";
-import * as $25 from "./routes/override/layout_no_app/_layout.tsx";
-import * as $26 from "./routes/override/layout_no_app/index.tsx";
-import * as $27 from "./routes/override/no_app.tsx";
-import * as $28 from "./routes/override/no_layout.tsx";
-import * as $29 from "./routes/override/no_layout_no_app.tsx";
-import * as $30 from "./routes/skip/sub/_layout.tsx";
-import * as $31 from "./routes/skip/sub/index.tsx";
+import * as $21 from "./routes/other.tsx";
+import * as $22 from "./routes/override/_layout.tsx";
+import * as $23 from "./routes/override/index.tsx";
+import * as $24 from "./routes/override/layout_no_app/_layout.tsx";
+import * as $25 from "./routes/override/layout_no_app/index.tsx";
+import * as $26 from "./routes/override/no_app.tsx";
+import * as $27 from "./routes/override/no_layout.tsx";
+import * as $28 from "./routes/override/no_layout_no_app.tsx";
+import * as $29 from "./routes/skip/sub/_layout.tsx";
+import * as $30 from "./routes/skip/sub/index.tsx";
const manifest = {
routes: {
@@ -58,17 +57,16 @@ const manifest = {
"./routes/foo/bar.tsx": $18,
"./routes/foo/index.tsx": $19,
"./routes/index.tsx": $20,
- "./routes/no_app.tsx": $21,
- "./routes/other.tsx": $22,
- "./routes/override/_layout.tsx": $23,
- "./routes/override/index.tsx": $24,
- "./routes/override/layout_no_app/_layout.tsx": $25,
- "./routes/override/layout_no_app/index.tsx": $26,
- "./routes/override/no_app.tsx": $27,
- "./routes/override/no_layout.tsx": $28,
- "./routes/override/no_layout_no_app.tsx": $29,
- "./routes/skip/sub/_layout.tsx": $30,
- "./routes/skip/sub/index.tsx": $31,
+ "./routes/other.tsx": $21,
+ "./routes/override/_layout.tsx": $22,
+ "./routes/override/index.tsx": $23,
+ "./routes/override/layout_no_app/_layout.tsx": $24,
+ "./routes/override/layout_no_app/index.tsx": $25,
+ "./routes/override/no_app.tsx": $26,
+ "./routes/override/no_layout.tsx": $27,
+ "./routes/override/no_layout_no_app.tsx": $28,
+ "./routes/skip/sub/_layout.tsx": $29,
+ "./routes/skip/sub/index.tsx": $30,
},
islands: {},
baseUrl: import.meta.url,
diff --git a/tests/fixture_layouts/routes/no_app.tsx b/tests/fixture_layouts/routes/no_app.tsx
deleted file mode 100644
index 9cddb138..00000000
--- a/tests/fixture_layouts/routes/no_app.tsx
+++ /dev/null
@@ -1,17 +0,0 @@
-import { RouteConfig } from "$fresh/server.ts";
-
-export const config: RouteConfig = {
- skipAppTemplate: false,
-};
-
-export default function OverridePage() {
- return (
-
-
-
- no
_app.tsx
template
-
-
-
- );
-}
diff --git a/tests/fixture_layouts/routes/override/layout_no_app/_layout.tsx b/tests/fixture_layouts/routes/override/layout_no_app/_layout.tsx
index a161d4f3..c306c255 100644
--- a/tests/fixture_layouts/routes/override/layout_no_app/_layout.tsx
+++ b/tests/fixture_layouts/routes/override/layout_no_app/_layout.tsx
@@ -1,7 +1,7 @@
import { LayoutConfig, LayoutProps } from "$fresh/server.ts";
export const config: LayoutConfig = {
- skipAppTemplate: true,
+ skipAppWrapper: true,
};
export default function OverrideLayout({ Component }: LayoutProps) {
diff --git a/tests/fixture_layouts/routes/override/no_app.tsx b/tests/fixture_layouts/routes/override/no_app.tsx
index 59178989..0cbd5d35 100644
--- a/tests/fixture_layouts/routes/override/no_app.tsx
+++ b/tests/fixture_layouts/routes/override/no_app.tsx
@@ -1,7 +1,7 @@
import { RouteConfig } from "$fresh/server.ts";
export const config: RouteConfig = {
- skipAppTemplate: true,
+ skipAppWrapper: true,
};
export default function OverridePage() {
diff --git a/tests/fixture_layouts/routes/override/no_layout_no_app.tsx b/tests/fixture_layouts/routes/override/no_layout_no_app.tsx
index 5edeec11..e48d50fb 100644
--- a/tests/fixture_layouts/routes/override/no_layout_no_app.tsx
+++ b/tests/fixture_layouts/routes/override/no_layout_no_app.tsx
@@ -1,7 +1,7 @@
import { LayoutConfig } from "$fresh/server.ts";
export const config: LayoutConfig = {
- skipAppTemplate: true,
+ skipAppWrapper: true,
skipInheritedLayouts: true,
};