You have `export const handlers = ` when it needs to be `export const handler = `. I'm unsure if there's any build process I need to run for this.
323 lines
8.9 KiB
Markdown
323 lines
8.9 KiB
Markdown
---
|
||
description: |
|
||
Migration guide for Fresh 2.x
|
||
---
|
||
|
||
We tried to keep breaking changes in Fresh 2 as minimal as possible, but some
|
||
changes need to be updated manually. Fresh 2 comes with many quality of life
|
||
[improvements](https://deno.com/blog/an-update-on-fresh) that make it easier to
|
||
extend and adapt Fresh. We've created this upgrade guide as part of upgrading
|
||
our own apps here at Deno.
|
||
|
||
Use this guide to migrate a Fresh 1.x app to Fresh 2.
|
||
|
||
## Applying automatic updates
|
||
|
||
Most changes can be applied automatically with the update script. Start the
|
||
update by running it in your project directory:
|
||
|
||
```sh Terminal
|
||
deno run -Ar jsr:@fresh/update
|
||
```
|
||
|
||
This will apply most API changes made in Fresh 2 automatically update like
|
||
changing `$fresh/server.ts` imports to `fresh`.
|
||
|
||
## Getting `main.ts` and `dev.ts` ready
|
||
|
||
Configuring Fresh doesn't require a dedicated config file anymore. You can
|
||
delete the `fresh.config.ts` file. The `fresh.gen.ts` manifest file isn't needed
|
||
anymore either.
|
||
|
||
```diff Project structure
|
||
<project root>
|
||
├── routes/
|
||
- ├── dev.ts
|
||
- ├── fresh.gen.ts
|
||
- ├── fresh.config.ts
|
||
└── main.ts
|
||
```
|
||
|
||
Fresh 2 takes great care in ensuring that code that's only needed during
|
||
development is separate from production code. This split makes deployments much
|
||
smaller, quicker to upload and allows them to boot up much quicker in
|
||
production.
|
||
|
||
### Replacing `dev.ts` with `vite.config.ts`
|
||
|
||
Delete `dev.ts` and create a `vite.config.ts` file instead. Pass your custom
|
||
Fresh configuration to the [fresh vite plugin](/docs/advanced/vite).
|
||
|
||
```ts vite.config.ts
|
||
import { defineConfig } from "vite";
|
||
import { fresh } from "@fresh/plugin-vite";
|
||
import tailwindcss from "@tailwindcss/vite";
|
||
|
||
export default defineConfig({
|
||
plugins: [
|
||
fresh(),
|
||
tailwindcss(),
|
||
],
|
||
});
|
||
```
|
||
|
||
### Add a `client.ts` file
|
||
|
||
The `client.ts` file is the main entry file for client-side code. Since vite
|
||
requires everything to be part of the internal module graph to get hot module
|
||
reloading to work, this is also the place where you import CSS assets.
|
||
|
||
```diff Project structure
|
||
<project root>
|
||
├── routes/
|
||
+ ├── client.ts
|
||
├── vite.config.ts
|
||
└── main.ts
|
||
```
|
||
|
||
```ts client.ts
|
||
// Import CSS files here for hot module reloading to work.
|
||
import "./assets/styles.css";
|
||
```
|
||
|
||
### Updating `main.ts`
|
||
|
||
Similarly, configuration related to running Fresh in production can be passed to
|
||
`new App()`:
|
||
|
||
```ts main.ts
|
||
import { App, staticFiles } from "fresh";
|
||
|
||
export const app = new App()
|
||
// Add static file serving middleware
|
||
.use(staticFiles())
|
||
// Enable file-system based routing
|
||
.fsRoutes();
|
||
```
|
||
|
||
## Merging error pages
|
||
|
||
Both the `_500.tsx` and `_404.tsx` template have been unified into a single
|
||
`_error.tsx` template.
|
||
|
||
```diff Project structure
|
||
└── <root>/routes/
|
||
- ├── _404.tsx
|
||
- ├── _500.tsx
|
||
+ ├── _error.tsx
|
||
└── ...
|
||
```
|
||
|
||
Inside the `_error.tsx` template you can show different content based on errors
|
||
or status codes with the following code:
|
||
|
||
```tsx routes/_error.tsx
|
||
export default function ErrorPage(props: PageProps) {
|
||
const error = props.error; // Contains the thrown Error or HTTPError
|
||
if (error instanceof HttpError) {
|
||
const status = error.status; // HTTP status code
|
||
|
||
// Render a 404 not found page
|
||
if (status === 404) {
|
||
return <h1>404 - Page not found</h1>;
|
||
}
|
||
}
|
||
|
||
return <h1>Oh no...</h1>;
|
||
}
|
||
```
|
||
|
||
## Updating tasks
|
||
|
||
The server entrypoint is now generated by Fresh for more optimal startup times.
|
||
This means you need to update your task when launching Fresh in production mode.
|
||
|
||
To launch Fresh in production mode:
|
||
|
||
```diff
|
||
- deno run -A main.ts
|
||
+ deno serve -A _fresh/server.js
|
||
```
|
||
|
||
You'll likely have that command inside your `deno.json` as a task. Update it
|
||
accordingly.
|
||
|
||
```diff deno.json
|
||
{
|
||
"tasks":
|
||
- "dev": "deno run -A dev.ts",
|
||
- "build": "deno run -A dev.ts build",
|
||
- "preview": "deno run -A main.ts"
|
||
+ "dev": "vite",
|
||
+ "build": "vite build",
|
||
+ "preview": "deno serve -A _fresh/server.js"
|
||
}
|
||
}
|
||
```
|
||
|
||
## Update imports
|
||
|
||
Add `jsr:@fresh/plugin-vite` and `npm:vite` to `imports` in your `deno.json`
|
||
file, either manually or with this command:
|
||
|
||
```shell
|
||
deno install npm:vite jsr:@fresh/plugin-vite
|
||
```
|
||
|
||
This should be enough in most cases. If you still encounter problems, you may
|
||
also need to update `compilerOptions` and other parts. For a complete,
|
||
up‑to‑date example, generate a new app (“Migrate by example” below) and compare.
|
||
|
||
> Note: With Vite, Fresh’s Tailwind plugin (`jsr:@fresh/plugin-tailwindcss*`) is
|
||
> not needed; use the Vite integration instead (e.g. `npm:@tailwindcss/vite`).
|
||
|
||
## Update deployment settings
|
||
|
||
Fresh 2 requires assets to be built during deployment instead of building them
|
||
on demand. Run the `deno task build` or `deno run build` command as part of your
|
||
deployment process.
|
||
|
||
If you have already set up Fresh's 1.x "Ahead-of-Time Builds", then no changes
|
||
are necessary.
|
||
|
||
## Trailing slash handling
|
||
|
||
The handling trailing slashes has been extracted to an optional middleware that
|
||
you can add if needed. This middleware can be used to ensure that URLs always
|
||
have a trailing slash at the end or that they will never have one.
|
||
|
||
```diff main.ts
|
||
- import { App, staticFiles } from "fresh";
|
||
+ import { App, staticFiles, trailingSlashes } from "fresh";
|
||
|
||
export const app = new App({ root: import.meta.url })
|
||
.use(staticFiles())
|
||
+ .use(trailingSlashes("never"));
|
||
```
|
||
|
||
## Automatic updates
|
||
|
||
> [info]: The changes listed here are applied automatically when running the
|
||
> [`@fresh/update`](https://jsr.io/@fresh/update) script and you shouldn't need
|
||
> to have to do these yourself.
|
||
|
||
### Unified middleware signatures
|
||
|
||
Middleware, handler and route component signatures have been unified to all look
|
||
the same. Instead of receiving two arguments, they receive one. The `Request`
|
||
object is stored on the context object as `ctx.req`.
|
||
|
||
```diff middleware.ts
|
||
- const middleware = (req, ctx) => new Response("ok");
|
||
+ const middleware = (ctx) => new Response("ok");
|
||
```
|
||
|
||
Same is true for handlers:
|
||
|
||
```diff route/page.tsx
|
||
export const handler = {
|
||
- GET(req, ctx) {
|
||
+ GET(ctx) {
|
||
return new Response("ok");
|
||
},
|
||
};
|
||
```
|
||
|
||
...and async route components:
|
||
|
||
```diff routes/my-page.tsx
|
||
- export default async function MyPage(req: Request, ctx: RouteContext) {
|
||
+ export default async function MyPage(props: PageProps) {
|
||
const value = await loadFooValue();
|
||
return <p>foo is: {value}</p>;
|
||
}
|
||
```
|
||
|
||
All the various context interfaces have been consolidated and simplified:
|
||
|
||
| Fresh 1.x | Fresh 2.x |
|
||
| --------------------------------------------- | ----------------------------------- |
|
||
| `AppContext`, `LayoutContext`, `RouteContext` | [`Context`](/docs/concepts/context) |
|
||
|
||
### Context methods
|
||
|
||
The `ctx.renderNotFound()` method has been removed in favor of throwing an
|
||
`HttpError` instance. This allows all middlewares to optionally participate in
|
||
error handling. Other properties have been moved or renamed to make it easier to
|
||
re-use existing objects internally as a minor performance optimization.
|
||
|
||
| Fresh 1.x | Fresh 2.x |
|
||
| ---------------------- | -------------------------- |
|
||
| `ctx.renderNotFound()` | `throw new HttpError(404)` |
|
||
| `ctx.basePath` | `ctx.config.basePath` |
|
||
| `ctx.remoteAddr` | `ctx.info.remoteAddr` |
|
||
|
||
### `ctx.render()`
|
||
|
||
The _meaning_ of `ctx.render()` has changed. In Fresh 1.x this was used to pass
|
||
data from a handler to a component. In Fresh 2.x this function has been
|
||
generalized to render JSX, so it's no longer needed.
|
||
|
||
Fresh 1.x:
|
||
|
||
```ts
|
||
export const handler = {
|
||
async GET(req, ctx) {
|
||
const data = await Query();
|
||
await ctx.render({ value: data });
|
||
},
|
||
};
|
||
|
||
export default function Page({ data }) {
|
||
// ...
|
||
}
|
||
```
|
||
|
||
Fresh 2 (removed):
|
||
|
||
```ts
|
||
export const handler = {
|
||
async GET(ctx) {
|
||
const data = await Query();
|
||
return { data: { value: data } };
|
||
},
|
||
};
|
||
|
||
export default function Page({ data }) {
|
||
// ...
|
||
}
|
||
```
|
||
|
||
To render JSX in general, use the `ctx.render()` function:
|
||
|
||
```tsx
|
||
const app = new App()
|
||
.get("/", () => ctx.render(<h1>hello</h1>));
|
||
```
|
||
|
||
## `createHandler`
|
||
|
||
The `createHandler` function was often used to launch Fresh for tests. This can
|
||
be now done via vite's `createBuilder` function. See the
|
||
[testing page](/docs/testing) for more information.
|
||
|
||
## `deno compile`
|
||
|
||
If you're using `deno compile` to generate a binary out of the Fresh app be sure
|
||
to [update the command](/docs/deployment/deno-compile) to generate the binary.
|
||
|
||
## Getting help
|
||
|
||
### 1. Migrate by example
|
||
|
||
If you run into problems with upgrading your app, first, try starting a new
|
||
Fresh 2 project and looking at the new structure.
|
||
|
||
eg. `mkdir fresh2-demo && cd fresh2-demo && deno run -Ar jsr:@fresh/init`
|
||
|
||
### 2. Document
|
||
|
||
If you still can't solve the migration problems. Please reach out to us by
|
||
creating an issue here https://github.com/denoland/fresh/issues/new . That way
|
||
we can improve this migration guide for everyone.
|