chore: release 1.3.0 (#1460)
This commit is contained in:
@@ -1,28 +0,0 @@
|
|||||||
---
|
|
||||||
description: |
|
|
||||||
Add a global application wrapper to provide common meta tags or context for application routes.
|
|
||||||
---
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
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 (
|
|
||||||
<div class="wrapper">
|
|
||||||
<Component />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
---
|
|
||||||
description: |
|
|
||||||
Islands enable client side interactivity in Fresh. They are hydrated on the client in addition to being rendered on the server.
|
|
||||||
---
|
|
||||||
|
|
||||||
Islands enable client side interactivity in Fresh. Islands are isolated Preact
|
|
||||||
components that are rendered on the client. This is different from all other
|
|
||||||
components in Fresh, as they are usually just rendered on the server.
|
|
||||||
|
|
||||||
Islands are defined by creating a file in the `islands/` folder in a Fresh
|
|
||||||
project. The name of this file must be a PascalCase or kebab-case name of the
|
|
||||||
island.
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// islands/MyIsland.tsx
|
|
||||||
|
|
||||||
import { useSignal } from "@preact/signals";
|
|
||||||
|
|
||||||
export default function MyIsland() {
|
|
||||||
const count = useSignal(0);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
Counter is at {count}.{" "}
|
|
||||||
<button onClick={() => (count.value += 1)}>+</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
An island can be used in a page like a regular Preact component. Fresh will take
|
|
||||||
care of automatically re-hydrating the island on the client.
|
|
||||||
|
|
||||||
Passing props to islands is supported, but only if the props are serializable.
|
|
||||||
Fresh can serialize the following types of values:
|
|
||||||
|
|
||||||
- Primitive types `string`, `boolean`, `bigint`, and `null`
|
|
||||||
- Most `number`s (`Infinity`, `-Infinity`, and `NaN` are silently converted to
|
|
||||||
`null`)
|
|
||||||
- Plain objects with string keys and serializable values
|
|
||||||
- Arrays containing serializable values
|
|
||||||
- Uint8Array
|
|
||||||
- JSX Elements (restricted to `props.children`)
|
|
||||||
- Preact Signals (if the inner value is serializable)
|
|
||||||
|
|
||||||
Circular references are supported. If an object or signal is referenced multiple
|
|
||||||
times, it is only serialized once and the references are restored upon
|
|
||||||
deserialization. Passing complex objects like `Date`, custom classes, or
|
|
||||||
functions is not supported.
|
|
||||||
|
|
||||||
## Passing JSX to islands
|
|
||||||
|
|
||||||
Islands support passing JSX elements via the `children` property. This allows
|
|
||||||
you to pass static content rendered by the server to an island in the browser.
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
// route/index.tsx
|
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
|
||||||
<MyIsland>
|
|
||||||
<p>This text is rendered on the server</p>
|
|
||||||
</MyIsland>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
We can deduce which parts were rendered by the server and which parts where
|
|
||||||
rendered by an island from the HTML alone. It contains all the information we
|
|
||||||
need, which allows us to skip the work of having to send a serialized version of
|
|
||||||
`props.children` to the browser.
|
|
||||||
|
|
||||||
### Nesting islands
|
|
||||||
|
|
||||||
Islands can be nested within other islands as well. In that scenario they act
|
|
||||||
like a normal Preact component, but still receive the serialized props if any
|
|
||||||
were present.
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
// route/index.tsx
|
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
|
||||||
import OtherIsland from "../islands/other-island.tsx";
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
|
||||||
<MyIsland>
|
|
||||||
<OtherIsland foo="this prop will be serialized">
|
|
||||||
<p>This text is rendered on the server</p>
|
|
||||||
</OtherIsland>
|
|
||||||
</MyIsland>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
In essence, fresh allows you to mix static and interactive parts in your app in
|
|
||||||
a way that's most optimal for your use app. We'll keep sending only the
|
|
||||||
JavaScript that is needed for the islands to the browser.
|
|
||||||
|
|
||||||
```jsx
|
|
||||||
// route/index.tsx
|
|
||||||
import MyIsland from "../islands/my-island.tsx";
|
|
||||||
import OtherIsland from "../islands/other-island.tsx";
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return (
|
|
||||||
<MyIsland>
|
|
||||||
<div>
|
|
||||||
<h1>Rendered by server</h1>
|
|
||||||
<OtherIsland>
|
|
||||||
<p>also rendered by server</p>
|
|
||||||
</OtherIsland>
|
|
||||||
</div>
|
|
||||||
</MyIsland>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
---
|
|
||||||
description: |
|
|
||||||
Add middleware routes to intercept requests or responses for analytics purposes, access control, or anything else.
|
|
||||||
---
|
|
||||||
|
|
||||||
A middleware is defined in a `_middleware.ts` file. It will intercept the
|
|
||||||
request in order for you to perform custom logic before or after the route
|
|
||||||
handler. This allows modifying or checking requests and responses. Common
|
|
||||||
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. 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
|
|
||||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
data: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function handler(
|
|
||||||
req: Request,
|
|
||||||
ctx: MiddlewareHandlerContext<State>,
|
|
||||||
) {
|
|
||||||
ctx.state.data = "myData";
|
|
||||||
const resp = await ctx.next();
|
|
||||||
resp.headers.set("server", "fresh server");
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```ts
|
|
||||||
// routes/myHandler.ts
|
|
||||||
export const handler: Handlers<any, { data: string }> = {
|
|
||||||
GET(_req, ctx) {
|
|
||||||
return new Response(`middleware data is ${ctx.state.data}`);
|
|
||||||
},
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
Middlewares are scoped and can be layered. This means a project can have
|
|
||||||
multiple middlewares, each covering a different set of routes. If multiple
|
|
||||||
middlewares cover a route, they will all be run, in order of specificity (least
|
|
||||||
specific first).
|
|
||||||
|
|
||||||
For example, take a project with the following routes:
|
|
||||||
|
|
||||||
```
|
|
||||||
└── routes
|
|
||||||
├── _middleware.ts
|
|
||||||
├── index.ts
|
|
||||||
└── admin
|
|
||||||
├── _middleware.ts
|
|
||||||
└── index.ts
|
|
||||||
└── signin.ts
|
|
||||||
```
|
|
||||||
|
|
||||||
For a request to `/` the request will flow like this:
|
|
||||||
|
|
||||||
1. The `routes/_middleware.ts` middleware is invoked.
|
|
||||||
2. Calling `ctx.next()` will invoke the `routes/index.ts` handler.
|
|
||||||
|
|
||||||
For a request to `/admin` the request flows like this:
|
|
||||||
|
|
||||||
1. The `routes/_middleware.ts` middleware is invoked.
|
|
||||||
2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts`
|
|
||||||
middleware.
|
|
||||||
3. Calling `ctx.next()` will invoke the `routes/admin/index.ts` handler.
|
|
||||||
|
|
||||||
For a request to `/admin/signin` the request flows like this:
|
|
||||||
|
|
||||||
1. The `routes/_middleware.ts` middleware is invoked.
|
|
||||||
2. Calling `ctx.next()` will invoke the `routes/admin/_middleware.ts`
|
|
||||||
middleware.
|
|
||||||
3. Calling `ctx.next()` will invoke the `routes/admin/signin.ts` handler.
|
|
||||||
|
|
||||||
A single middleware file can also define multiple middlewares (all for the same
|
|
||||||
route) by exporting an array of handlers instead of a single handler. For
|
|
||||||
example:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
// routes/_middleware.ts
|
|
||||||
|
|
||||||
export const handler = [
|
|
||||||
async function middleware1(req, ctx) {
|
|
||||||
// do something
|
|
||||||
return ctx.next();
|
|
||||||
},
|
|
||||||
async function middleware2(req, ctx) {
|
|
||||||
// do something
|
|
||||||
return ctx.next();
|
|
||||||
},
|
|
||||||
];
|
|
||||||
```
|
|
||||||
|
|
||||||
It should be noted that `middleware` has access to route parameters. If you're
|
|
||||||
running a fictitious `routes/[tenant]/admin/_middleware.ts` like this:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
|
||||||
const currentTenant = ctx.params.tenant;
|
|
||||||
//do something with the tenant
|
|
||||||
const resp = await ctx.next();
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
and the request is to `mysaas.com/acme/admin/`, then `currentTenant` will have
|
|
||||||
the value of `acme` in your middleware.
|
|
||||||
|
|
||||||
## Middleware Destination
|
|
||||||
|
|
||||||
To set the stage for this section, `MiddlewareHandlerContext` looks like this:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>> {
|
|
||||||
next: () => Promise<Response>;
|
|
||||||
state: State;
|
|
||||||
destination: router.DestinationKind;
|
|
||||||
remoteAddr: {
|
|
||||||
transport: "tcp" | "udp";
|
|
||||||
hostname: string;
|
|
||||||
port: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
and `router.DestinationKind` is defined like this:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
export type DestinationKind = "internal" | "static" | "route" | "notFound";
|
|
||||||
```
|
|
||||||
|
|
||||||
This is useful if you want your middleware to only run when a request is headed
|
|
||||||
for a `route`, as opposed to something like `http://localhost:8001/favicon.ico`.
|
|
||||||
|
|
||||||
### Example
|
|
||||||
|
|
||||||
Initiate a new Fresh project (`deno run -A -r https://fresh.deno.dev/`) and then
|
|
||||||
create a `_middleware.ts` file in the `routes` folder like this:
|
|
||||||
|
|
||||||
```ts
|
|
||||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
|
||||||
console.log(ctx.destination);
|
|
||||||
console.log(req.url);
|
|
||||||
const resp = await ctx.next();
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
If you start up your server (`deno task start`) you'll see the following:
|
|
||||||
|
|
||||||
```
|
|
||||||
Task start deno run -A --watch=static/,routes/ dev.ts
|
|
||||||
Watcher Process started.
|
|
||||||
The manifest has been generated for 4 routes and 1 islands.
|
|
||||||
|
|
||||||
🍋 Fresh ready
|
|
||||||
Local: http://localhost:8000/
|
|
||||||
|
|
||||||
route
|
|
||||||
http://localhost:8000/
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/deserializer.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/signals.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/plugin-twind-main.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/main.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/island-counter.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/refresh.js
|
|
||||||
static
|
|
||||||
http://localhost:8000/logo.svg?__frsh_c=3c7400558fc00915df88cb181036c0dbf73ab7f5
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/alive
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-PDMKJVJ5.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-UGFDDSOV.js
|
|
||||||
internal
|
|
||||||
http://localhost:8000/_frsh/js/3c7400558fc00915df88cb181036c0dbf73ab7f5/chunk-RCK7U3UF.js
|
|
||||||
```
|
|
||||||
|
|
||||||
That first `route` request is for when `Fresh` responds with the root level
|
|
||||||
`index.tsx` route. The rest, as you can see, are either `internal` or `static`
|
|
||||||
requests. You can use `ctx.destination` to filter these out if your middleware
|
|
||||||
is only supposed to deal with routes.
|
|
||||||
@@ -1,144 +0,0 @@
|
|||||||
---
|
|
||||||
description: |
|
|
||||||
Routes are the basic building block of fresh applications. They are used to define the behaviour the application when a given path is requested.
|
|
||||||
---
|
|
||||||
|
|
||||||
At their core, routes describe how a request for a given path should be handled,
|
|
||||||
and what the response should be. To do this, routes have two main parts: the
|
|
||||||
handler, and the component. A route can have either one, or both, but never
|
|
||||||
neither.
|
|
||||||
|
|
||||||
The handler is a function that is called for every request to the route. It
|
|
||||||
needs to return a response that is then sent to the client. The response could
|
|
||||||
be anything: a plain text string, a JSON object, an HTML page, a WebSocket
|
|
||||||
connection, a streaming file, or pretty much anything else. The handler is
|
|
||||||
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 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
|
|
||||||
|
|
||||||
Let's look at a basic route that returns a plain text string:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// routes/plain.tsx
|
|
||||||
|
|
||||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
|
||||||
GET(_req: Request, _ctx: HandlerContext) {
|
|
||||||
return new Response("Hello World");
|
|
||||||
},
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
To define a handler, one needs to export a `handler` function or object from the
|
|
||||||
route module. If the handler is an object, each key in the object is the name of
|
|
||||||
the HTTP method that the handler should be called for. For example the `GET`
|
|
||||||
handler above is called for `GET` requests. If the handler is a function, it is
|
|
||||||
called for all requests regardless of the method. If an HTTP method does not
|
|
||||||
have a corresponding handler, a 405 HTTP error is returned.
|
|
||||||
|
|
||||||
## Component route
|
|
||||||
|
|
||||||
Now, let's render some HTML using the route component:
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// routes/html.tsx
|
|
||||||
|
|
||||||
import { PageProps } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export default function Page(props: PageProps) {
|
|
||||||
return <div>You are on the page '{props.url.href}'.</div>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The page component needs to be the default export of the route module. It is
|
|
||||||
passed props that can be used to render the page.
|
|
||||||
|
|
||||||
As you can see in the second example, if no handler is explicitly defined a
|
|
||||||
default handler is used that just renders out the page component if present. You
|
|
||||||
can also override the default handler though to modify how exactly rendering
|
|
||||||
should work.
|
|
||||||
|
|
||||||
## Mixed handler and component route
|
|
||||||
|
|
||||||
In the below example, a custom handler is used to add a custom header to the
|
|
||||||
response after rendering the page component.
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// routes/html.tsx
|
|
||||||
|
|
||||||
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";
|
|
||||||
|
|
||||||
export const handler: Handlers = {
|
|
||||||
async GET(_req: Request, ctx: HandlerContext) {
|
|
||||||
const resp = await ctx.render();
|
|
||||||
resp.headers.set("X-Custom-Header", "Hello World");
|
|
||||||
return resp;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function Page(props: PageProps) {
|
|
||||||
return <div>You are on the page '{props.url.href}'.</div>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Async route components
|
|
||||||
|
|
||||||
Having a separate route handler and component function is nice, when you want to
|
|
||||||
test these in isolation, but can become a bit cumbersome to maintain. They
|
|
||||||
require some additional indirection of declaring an interface for the component
|
|
||||||
`Data` when you're passing it around through `ctx.render()`.
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
interface Data {
|
|
||||||
foo: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const handler: Handlers<Data> = {
|
|
||||||
async GET(req, ctx) {
|
|
||||||
const value = await loadFooValue();
|
|
||||||
ctx.render({ foo: value });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function MyPage(props: PageProps<Data>) {
|
|
||||||
return <p>foo is: {props.data.foo}</p>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
When a route has both a component and a `GET` handler, they are typically very
|
|
||||||
closely coupled. With async route components you can merge the two together and
|
|
||||||
avoid having to create the `Data` interface boilerplate.
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
// Async route component
|
|
||||||
export default async function MyPage(req: Request, ctx: RouteContext) {
|
|
||||||
const value = await loadFooValue();
|
|
||||||
return <p>foo is: {value}</p>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The code gets a little shorter with async route components. Conceptually, you
|
|
||||||
can think of async route components inlining the `GET` handler into the
|
|
||||||
component function. Note, that you can still add additional HTTP handlers in the
|
|
||||||
same file like before.
|
|
||||||
|
|
||||||
```tsx
|
|
||||||
export const handler: Handlers = {
|
|
||||||
async POST(req) {
|
|
||||||
// ... do something here
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default async function MyPage(req: Request, ctx: RouteContext) {
|
|
||||||
const value = await loadFooValue();
|
|
||||||
return <p>foo is: {value}</p>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,128 +0,0 @@
|
|||||||
---
|
|
||||||
description: |
|
|
||||||
New versions of Fresh are regularly released. This page explains how to update your project.
|
|
||||||
---
|
|
||||||
|
|
||||||
Fresh constists of multiple pieces which are independently versioned and
|
|
||||||
released.
|
|
||||||
|
|
||||||
- Fresh (https://deno.land/x/fresh)
|
|
||||||
- Preact (https://esm.sh/preact)
|
|
||||||
- preact-render-to-string (https://esm.sh/preact-render-to-string)
|
|
||||||
|
|
||||||
Some plugins also have their own dependencies that can be updated independently.
|
|
||||||
|
|
||||||
- Twind (https://esm.sh/twind) (for the twind plugin)
|
|
||||||
|
|
||||||
For the most part these pieces can be updated independently. Certain versions of
|
|
||||||
Fresh may require a minimum version of a given dependency. This is documented
|
|
||||||
below.
|
|
||||||
|
|
||||||
| Fresh version | Preact | preact-render-to-string | Deno |
|
|
||||||
| ------------- | ----------------- | ----------------------- | --------- |
|
|
||||||
| 1.0.0-1.0.2 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.23.0 |
|
|
||||||
| 1.1.0-1.1.5 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.25.0 |
|
|
||||||
| 1.2.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.25.0 |
|
|
||||||
|
|
||||||
## Updating dependencies
|
|
||||||
|
|
||||||
To update your dependencies, you have two options:
|
|
||||||
|
|
||||||
- Run the fresh updater to update your project dependencies.
|
|
||||||
- Manually update the dependency versions in your `deno.json` file.
|
|
||||||
|
|
||||||
### Auto updater
|
|
||||||
|
|
||||||
The auto updater is a command line tool that will update your project's
|
|
||||||
`deno.json` file to the latest versions of Fresh and its dependencies. It may
|
|
||||||
also contain code mods for your project that will update your code to the latest
|
|
||||||
recommended patterns for Fresh projects.
|
|
||||||
|
|
||||||
To run the auto updater, run the following command from the root of your
|
|
||||||
project:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ deno run -A -r https://fresh.deno.dev/update .
|
|
||||||
```
|
|
||||||
|
|
||||||
You will be prompted to confirm the changes that will be made to your project.
|
|
||||||
|
|
||||||
### Manual update
|
|
||||||
|
|
||||||
To manually update your project's dependencies, you can edit the `deno.json`
|
|
||||||
file in the root of your projects directory. Dependency versions are encoded
|
|
||||||
into the URLs in this file. For example, here is how to update a project from
|
|
||||||
Fresh 1.0.2 to 1.1.3, and update Preact to the latest version:
|
|
||||||
|
|
||||||
```diff
|
|
||||||
{
|
|
||||||
"imports": {
|
|
||||||
- "$fresh/": "https://deno.land/x/fresh@1.0.2/",
|
|
||||||
+ "$fresh/": "https://deno.land/x/fresh@1.1.5/",
|
|
||||||
|
|
||||||
- "preact": "https://esm.sh/preact@10.8.1",
|
|
||||||
- "preact/": "https://esm.sh/preact@10.8.1/",
|
|
||||||
+ "preact": "https://esm.sh/preact@10.11.0",
|
|
||||||
+ "preact/": "https://esm.sh/preact@10.11.0/",
|
|
||||||
|
|
||||||
- "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.0",
|
|
||||||
+ "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
|
|
||||||
|
|
||||||
"twind": "https://esm.sh/twind@0.16.17",
|
|
||||||
"twind/": "https://esm.sh/twind@0.16.17/"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Automatic update checks
|
|
||||||
|
|
||||||
Fresh will periodically check if a new Fresh version is available if it's
|
|
||||||
running outside of CI. This happens once per day and can be disabled by setting
|
|
||||||
the `FRESH_NO_UPDATE_CHECK=true` environment variable.
|
|
||||||
|
|
||||||
## Code mods
|
|
||||||
|
|
||||||
Code mods are small scripts that can be run to update your project's code to
|
|
||||||
match the latest recommended patterns for Fresh projects. Code mods can be run
|
|
||||||
through the auto updater. Sometimes the code mod can not cover all cases, so you
|
|
||||||
may need to manually update some code. This section explains the code mods
|
|
||||||
currently available.
|
|
||||||
|
|
||||||
### Classical JSX -> Automatic JSX
|
|
||||||
|
|
||||||
> This code mod is only available in Fresh 1.1.0 and above.
|
|
||||||
|
|
||||||
The classical JSX transform that relies on a `/** @jsx h */` pragma is no longer
|
|
||||||
the recommended way to use JSX in Fresh projects. Instead, starting with version
|
|
||||||
1.1.0, Fresh projects should use the automatic JSX transform that requires no
|
|
||||||
JSX pragma or preact import.
|
|
||||||
|
|
||||||
```diff
|
|
||||||
- /** @jsx h */
|
|
||||||
- import { h } from "preact";
|
|
||||||
|
|
||||||
export default function Page() {
|
|
||||||
return <div>Hello world!</div>;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
This code mod will update your deno.json file to include the relevant compiler
|
|
||||||
options to enable the automatic JSX transform. It will then go through your
|
|
||||||
project and remove any `/** @jsx h */` pragmas and `import { h } from "preact"`
|
|
||||||
statements.
|
|
||||||
|
|
||||||
### Classic twind -> Twind plugin
|
|
||||||
|
|
||||||
> This code mod is only available in Fresh 1.1.0 and above.
|
|
||||||
|
|
||||||
Fresh version 1.1.0 introduced a new plugin for using twind with Fresh. This
|
|
||||||
plugin is much nicer to use than the raw twind integration that was previously
|
|
||||||
available.
|
|
||||||
|
|
||||||
This code mod will update your project to use the new twind plugin. It will
|
|
||||||
update your `main.ts` file to import the twind plugin and add it to the plugins
|
|
||||||
array. It will also update your files to remove many unnecessary uses of the
|
|
||||||
`tw` function, and remove unnecessary twind imports. While the code mod can
|
|
||||||
handle most cases, you may need to manually update some code. Additionally you
|
|
||||||
will need to manually update your `twind.config.ts` if you use a custom
|
|
||||||
configuration.
|
|
||||||
@@ -9,14 +9,16 @@ wrapper is allowed per application.
|
|||||||
|
|
||||||
The component to be wrapped is received via props, in addition to a few other
|
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
|
things. This allows for the introduction of a global container functioning as a
|
||||||
template which can be conditioned based on state and params.
|
template which can be conditioned based on state and params. Note that any state
|
||||||
|
set by middleware is available via `props.state`.
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
// routes/_app.tsx
|
// routes/_app.tsx
|
||||||
|
|
||||||
import { AppProps } from "$fresh/server.ts";
|
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 (
|
return (
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
<Component />
|
<Component />
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ components in Fresh, as they are usually just rendered on the server.
|
|||||||
|
|
||||||
Islands are defined by creating a file in the `islands/` folder in a Fresh
|
Islands are defined by creating a file in the `islands/` folder in a Fresh
|
||||||
project. The name of this file must be a PascalCase or kebab-case name of the
|
project. The name of this file must be a PascalCase or kebab-case name of the
|
||||||
island. The file must have a default export that is a regular Preact component.
|
island.
|
||||||
|
|
||||||
```tsx
|
```tsx
|
||||||
// islands/MyIsland.tsx
|
// islands/MyIsland.tsx
|
||||||
|
|||||||
@@ -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
|
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
|
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
|
```ts
|
||||||
// routes/_middleware.ts
|
// routes/_middleware.ts
|
||||||
@@ -95,6 +98,23 @@ export const handler = [
|
|||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
|
It should be noted that `middleware` has access to route parameters. If you're
|
||||||
|
running a fictitious `routes/[tenant]/admin/_middleware.ts` like this:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||||
|
|
||||||
|
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||||
|
const currentTenant = ctx.params.tenant;
|
||||||
|
//do something with the tenant
|
||||||
|
const resp = await ctx.next();
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
and the request is to `mysaas.com/acme/admin/`, then `currentTenant` will have
|
||||||
|
the value of `acme` in your middleware.
|
||||||
|
|
||||||
## Middleware Destination
|
## Middleware Destination
|
||||||
|
|
||||||
To set the stage for this section, `MiddlewareHandlerContext` looks like this:
|
To set the stage for this section, `MiddlewareHandlerContext` looks like this:
|
||||||
|
|||||||
+56
-2
@@ -18,8 +18,8 @@ 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
|
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
|
determine exactly what should be rendered. By default components receives props
|
||||||
consisting of: the request URL, the matching route (as a string), the matches
|
consisting of: the request URL, the matching route (as a string), the matches
|
||||||
from the URL pattern match, and any data passed to the handler's `render`
|
from the URL pattern match, any state set by middleware, and any data passed to
|
||||||
function.
|
the handler's `render` function.
|
||||||
|
|
||||||
## Handler route
|
## Handler route
|
||||||
|
|
||||||
@@ -88,3 +88,57 @@ export default function Page(props: PageProps) {
|
|||||||
return <div>You are on the page '{props.url.href}'.</div>;
|
return <div>You are on the page '{props.url.href}'.</div>;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Async route components
|
||||||
|
|
||||||
|
Having a separate route handler and component function is nice, when you want to
|
||||||
|
test these in isolation, but can become a bit cumbersome to maintain. They
|
||||||
|
require some additional indirection of declaring an interface for the component
|
||||||
|
`Data` when you're passing it around through `ctx.render()`.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
interface Data {
|
||||||
|
foo: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const handler: Handlers<Data> = {
|
||||||
|
async GET(req, ctx) {
|
||||||
|
const value = await loadFooValue();
|
||||||
|
ctx.render({ foo: value });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function MyPage(props: PageProps<Data>) {
|
||||||
|
return <p>foo is: {props.data.foo}</p>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
When a route has both a component and a `GET` handler, they are typically very
|
||||||
|
closely coupled. With async route components you can merge the two together and
|
||||||
|
avoid having to create the `Data` interface boilerplate.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
// Async route component
|
||||||
|
export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||||
|
const value = await loadFooValue();
|
||||||
|
return <p>foo is: {value}</p>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The code gets a little shorter with async route components. Conceptually, you
|
||||||
|
can think of async route components inlining the `GET` handler into the
|
||||||
|
component function. Note, that you can still add additional HTTP handlers in the
|
||||||
|
same file like before.
|
||||||
|
|
||||||
|
```tsx
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async POST(req) {
|
||||||
|
// ... do something here
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||||
|
const value = await loadFooValue();
|
||||||
|
return <p>foo is: {value}</p>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
description: |
|
description: |
|
||||||
Fresh regularly releases new versions of the framework. This page explains how to update your project.
|
New versions of Fresh are regularly released. This page explains how to update your project.
|
||||||
---
|
---
|
||||||
|
|
||||||
Fresh constists of multiple pieces which are independently versioned and
|
Fresh constists of multiple pieces which are independently versioned and
|
||||||
@@ -23,7 +23,6 @@ below.
|
|||||||
| 1.0.0-1.0.2 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.23.0 |
|
| 1.0.0-1.0.2 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.23.0 |
|
||||||
| 1.1.0-1.1.5 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.25.0 |
|
| 1.1.0-1.1.5 | >=10.8.1 <11.0.0 | >=5.2.0 <6.0.0 | >= 1.25.0 |
|
||||||
| 1.2.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.25.0 |
|
| 1.2.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.25.0 |
|
||||||
| 1.3.0 | >=10.15.0 <11.0.0 | >=6.1.0 | >= 1.35.0 |
|
|
||||||
|
|
||||||
## Updating dependencies
|
## Updating dependencies
|
||||||
|
|
||||||
@@ -75,6 +74,12 @@ Fresh 1.0.2 to 1.1.3, and update Preact to the latest version:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Automatic update checks
|
||||||
|
|
||||||
|
Fresh will periodically check if a new Fresh version is available if it's
|
||||||
|
running outside of CI. This happens once per day and can be disabled by setting
|
||||||
|
the `FRESH_NO_UPDATE_CHECK=true` environment variable.
|
||||||
|
|
||||||
## Code mods
|
## Code mods
|
||||||
|
|
||||||
Code mods are small scripts that can be run to update your project's code to
|
Code mods are small scripts that can be run to update your project's code to
|
||||||
|
|||||||
+4
-4
@@ -46,11 +46,11 @@ const toc: RawTableOfContents = {
|
|||||||
pages: [
|
pages: [
|
||||||
["architecture", "Architecture", "link:latest"],
|
["architecture", "Architecture", "link:latest"],
|
||||||
["server-components", "Server Components", "link:latest"],
|
["server-components", "Server Components", "link:latest"],
|
||||||
["routes", "Routes", "link:canary"],
|
["routes", "Routes", "link:latest"],
|
||||||
["forms", "Forms", "link:latest"],
|
["forms", "Forms", "link:latest"],
|
||||||
["islands", "Interactive islands", "link:canary"],
|
["islands", "Interactive islands", "link:latest"],
|
||||||
["static-files", "Static files", "link:latest"],
|
["static-files", "Static files", "link:latest"],
|
||||||
["middleware", "Route middleware", "link:canary"],
|
["middleware", "Route middleware", "link:latest"],
|
||||||
["error-pages", "Error pages", "link:latest"],
|
["error-pages", "Error pages", "link:latest"],
|
||||||
["routing", "Routing", "link:latest"],
|
["routing", "Routing", "link:latest"],
|
||||||
["data-fetching", "Data fetching", "link:latest"],
|
["data-fetching", "Data fetching", "link:latest"],
|
||||||
@@ -58,7 +58,7 @@ const toc: RawTableOfContents = {
|
|||||||
["plugins", "Plugins", "link:latest"],
|
["plugins", "Plugins", "link:latest"],
|
||||||
["updating", "Updating Fresh", "link:canary"],
|
["updating", "Updating Fresh", "link:canary"],
|
||||||
["app-wrapper", "Application wrapper", "link:latest"],
|
["app-wrapper", "Application wrapper", "link:latest"],
|
||||||
["server-configuration", "Server configuration", "link:canary"],
|
["server-configuration", "Server configuration", "link:latest"],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
integrations: {
|
integrations: {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
[
|
[
|
||||||
|
"1.3.0",
|
||||||
"1.2.0",
|
"1.2.0",
|
||||||
"1.1.6",
|
"1.1.6",
|
||||||
"1.1.5",
|
"1.1.5",
|
||||||
|
|||||||
@@ -77,10 +77,11 @@ function HelloBar() {
|
|||||||
return (
|
return (
|
||||||
<a
|
<a
|
||||||
class="bg-green-400 text-black border(b green-500) p-3 text-center group"
|
class="bg-green-400 text-black border(b green-500) p-3 text-center group"
|
||||||
href="https://deno.com/blog/fresh-1.2"
|
href="https://deno.com/blog/fresh-1.3"
|
||||||
>
|
>
|
||||||
<b>Fresh v1.2</b> has been released with <b>improved islands</b>,{" "}
|
<b>Fresh v1.3</b> has been released with <b>async route components</b>,
|
||||||
<b>preview npm: support</b>, and more!{" "}
|
{" "}
|
||||||
|
<b>plugin enhancements</b>, and more!{" "}
|
||||||
<span class="group-hover:underline">→</span>
|
<span class="group-hover:underline">→</span>
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user