Follow-up from #3162. This PR does various tweaks, mostly related to code blocks. - Adds missing titles to code blocks, and updates titles to be more consistent - Adds filenames to most ` ```tsx` blocks with e.g. `main.ts`, `dev.ts` or a suitable example (e.g. `routes/greeting.tsx`) based on the code - Adds filenames to ` ```diff` blocks if there's a relevant file, to make the icon e.g. TS over Diff icon - Code blocks with Fresh code (e.g. interfaces) uses ` ```tsx fresh 🍋` - Changes most ` ```sh` blocks with example terminal commands to ` ```sh Terminal` - Updates ` ```txt Project Structure` or ` ```diff Project Structure` blocks to be more consistent - Changes `sh` or `txt` to `txt-files` which improves highlighting and makes icon consistent - Renames `<project dir>` to `<project root>` to match builder `root` option terminology - Adds `<project root>` root folder to most/all overview folder overviews - Makes the structure blocks more consistently displayed - Tried to make some "bare" examples more complete ## Comparison Here's a small curated set of before/after changes. ### Deployment - `deno` highlighted in terminal commands - `.gitignore` syntax highlighting works without using `sh` as language <img width="1641" height="1009" alt="image" src="https://github.com/user-attachments/assets/170cf833-1f6a-4b0d-8d2d-a081bc28a967" /> ### File routing Updated icon, comments same as before, but root and hierarchy have new colors (`operator`/`function`). <img width="1647" height="588" alt="image" src="https://github.com/user-attachments/assets/f28db51b-4bee-45e4-be1c-e137a6d3ae3d" />
274 lines
8.0 KiB
Markdown
274 lines
8.0 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.
|
|
|
|
### Updating `dev.ts`
|
|
|
|
Development related configuration can be passed to the `Builder` class instance
|
|
in `dev.ts`. This file is also where you typically set up development-only
|
|
plugins like [tailwindcss](https://tailwindcss.com/).
|
|
|
|
The full `dev.ts` file for newly generated Fresh 2 projects looks like this:
|
|
|
|
```ts dev.ts
|
|
import { Builder } from "fresh/dev";
|
|
import { tailwind } from "@fresh/plugin-tailwind";
|
|
|
|
// Pass development only configuration here
|
|
const builder = new Builder({ target: "safari12" });
|
|
|
|
// Example: Enabling the tailwind plugin for Fresh
|
|
tailwind(builder);
|
|
|
|
// Create optimized assets for the browser when
|
|
// running `deno run -A dev.ts build`
|
|
if (Deno.args.includes("build")) {
|
|
await builder.build();
|
|
} else {
|
|
// ...otherwise start the development server
|
|
await builder.listen(() => import("./main.ts"));
|
|
}
|
|
```
|
|
|
|
> [info]: Fresh 1.x used Tailwind CSS v3. To keep using v3 use the
|
|
> `@fresh/plugin-tailwind-v3` instead.
|
|
|
|
### 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>;
|
|
}
|
|
```
|
|
|
|
## Removal of `<Head>` component
|
|
|
|
The `<Head>` component was used in Fresh 1.x to add additional tags to the
|
|
`<head>` portion of an HTML document from anywhere on the page. This feature was
|
|
removed in preparation and due to performance concerns as it required a complex
|
|
machinery in the background to work.
|
|
|
|
Instead, passing head-related data is best done via `ctx.state`
|
|
|
|
```tsx routes/about.tsx
|
|
export const handler = {
|
|
GET(ctx) {
|
|
// Set a route specific data in a handler
|
|
ctx.state.title = "About Me";
|
|
return page();
|
|
},
|
|
};
|
|
```
|
|
|
|
```tsx routes/_app.tsx
|
|
export default function AppWrapper(ctx: Context) {
|
|
return (
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
{ctx.state.title ? <title>{ctx.state.title}</title> : null}
|
|
</head>
|
|
<body>
|
|
<ctx.Component />
|
|
</body>
|
|
</html>
|
|
);
|
|
}
|
|
```
|
|
|
|
## Update deployment settings
|
|
|
|
Fresh 2 requires assets to be build during deployment instead of building them
|
|
on demand. Run the `deno task 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/canary/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` |
|
|
|
|
## `createHandler`
|
|
|
|
The `createHandler` function was often used to launch Fresh for tests. This can
|
|
be now done via the [`Builder`](/docs/canary/concepts/builder).
|
|
|
|
```ts main.test.ts
|
|
// Best to do this once instead of for every test case for
|
|
// performance reasons.
|
|
const builder = new Builder();
|
|
const applySnapshot = await builder.build({ snapshot: "memory" });
|
|
|
|
function testApp() {
|
|
const app = new App()
|
|
.get("/", () => new Response("hello"));
|
|
// Applies build snapshot to this app instance.
|
|
applySnapshot(app);
|
|
}
|
|
|
|
Deno.test("My Test", () => {
|
|
const handler = testApp().handler();
|
|
|
|
const response = await handler(new Request("http://localhost"));
|
|
const text = await response.text();
|
|
|
|
if (text !== "hello") {
|
|
throw new Error("fail");
|
|
}
|
|
});
|
|
```
|
|
|
|
## Getting help
|
|
|
|
If you run into problems with upgrading your app, 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.
|