feat: pass middleware state in props (#1264)

This commit is contained in:
Reed von Redwitz
2023-06-28 14:53:42 +02:00
committed by GitHub
parent 851f8d60aa
commit 0db3a0911a
12 changed files with 105 additions and 20 deletions
+6 -3
View File
@@ -7,15 +7,18 @@ An application wrapper is defined in an `_app.tsx` file in `routes/` folder. It
must contain a default export that is a regular Preact component. Only one such
wrapper is allowed per application.
It receives component through props which is to be wrapped. For instance, it
allows to introduce a global container for the whole 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 }: AppProps) {
export default function App({ Component, state }: AppProps) {
//do something with state here
return (
<div class="wrapper">
<Component />
+4 -1
View File
@@ -10,7 +10,10 @@ use-cases for this are logging, authentication, and performance monitoring.
Each middleware gets passed a `next` function in the context argument that is
used to trigger child handlers. The `ctx` also has a `state` property that can
be used to pass arbitrary data to downstream (or upstream) handlers.
be used to pass arbitrary data to downstream (or upstream) handlers. This
`state` is included in `PageProps` by default, which is available to both the
special [_app](/docs/concepts/app-wrapper.md) wrapper and normal
[routes](/docs/concepts/routes.md).
```ts
// routes/_middleware.ts
+3 -2
View File
@@ -17,8 +17,9 @@ passed a `render` function that it can call to invoke rendering a component.
The component is the template for a page. It is a JSX element that is rendered
on the server. The page component gets passed props that can be used by it to
determine exactly what should be rendered. By default components receives props
consisting of: the request URL, the matches from the URL pattern match, and any
data passed to the handler's `render` function.
consisting of: the request URL, the matching route (as a string), the matches
from the URL pattern match, any state set by middleware, and any data passed to
the handler's `render` function.
## Handler route
+1 -1
View File
@@ -22,7 +22,7 @@ Route files that render HTML are JavaScript or TypeScript modules that export a
JSX component as their default export. This component will be rendered for every
request to the route's path. The component receives a few properties that can be
used to customize the rendered output, such as the current route, the url of the
request, and handler data (more on that later).
request, state set by middleware, and handler data (more on the last two later).
In the demo project we'll create a route to handle the `/about` page. To do
this, one needs to create a new `routes/about.tsx` file. In this file, we can