Skip to content

Commit 62f8bb0

Browse files
docs: update Fresh 2 migration info (denoland#3161)
1 parent c08710f commit 62f8bb0

File tree

7 files changed

+28
-29
lines changed

7 files changed

+28
-29
lines changed

docs/canary/examples/migration-guide.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,16 @@ if (Deno.args.includes("build")) {
7070
}
7171
```
7272

73+
> [info]: Fresh 1.x used Tailwind CSS v3. To keep using v3 use the
74+
> `@fresh/plugin-tailwind-v3` instead.
75+
7376
### Updating `main.ts`
7477

7578
Similarly, configuration related to running Fresh in production can be passed to
7679
`new App()`:
7780

78-
```ts
79-
// main.ts
80-
import { App, fsRoutes, staticFiles } from "fresh";
81+
```ts main.ts
82+
import { App, staticFiles } from "fresh";
8183

8284
export const app = new App()
8385
// Add static file serving middleware
@@ -102,7 +104,7 @@ Both the `_500.tsx` and `_404.tsx` template have been unified into a single
102104
Inside the `_error.tsx` template you can show different content based on errors
103105
or status codes with the following code:
104106

105-
```tsx
107+
```tsx routes/_error.tsx
106108
export default function ErrorPage(props: PageProps) {
107109
const error = props.error; // Contains the thrown Error or HTTPError
108110
if (error instanceof HttpError) {
@@ -138,7 +140,7 @@ export const handler = {
138140
};
139141

140142
// Render that in _app.tsx
141-
export default function AppWrapper(ctx: FreshContext) {
143+
export default function AppWrapper(ctx: Context) {
142144
return (
143145
<html lang="en">
144146
<head>

src/compat.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
import type { ComponentChildren } from "preact";
2-
import type { FreshContext } from "./context.ts";
2+
import type { Context } from "./context.ts";
33
import type { PageProps } from "./render.ts";
44
import type { HandlerFn, RouteHandler } from "./handlers.ts";
55

66
/**
77
* @deprecated Use {@linkcode PageProps} instead.
88
*/
9-
export type AppProps<_Data = unknown, T = unknown> = FreshContext<T>;
9+
export type AppProps<_Data = unknown, T = unknown> = Context<T>;
1010
/**
1111
* @deprecated Use {@linkcode PageProps} instead.
1212
*/
13-
export type LayoutProps<_Data = unknown, T = unknown> = FreshContext<T>;
13+
export type LayoutProps<_Data = unknown, T = unknown> = Context<T>;
1414
/**
1515
* @deprecated Use {@linkcode PageProps} instead.
1616
*/
17-
export type UnknownPageProps<_Data = unknown, T = unknown> = FreshContext<T>;
17+
export type UnknownPageProps<_Data = unknown, T = unknown> = Context<T>;
1818
/**
1919
* @deprecated Use {@linkcode PageProps} instead.
2020
*/
21-
export type ErrorPageProps<_Data = unknown, T = unknown> = FreshContext<T>;
21+
export type ErrorPageProps<_Data = unknown, T = unknown> = Context<T>;
2222

2323
/**
2424
* @deprecated Use {@linkcode FreshContext} instead.
2525
*/
26-
export type RouteContext<_T = never, S = Record<string, unknown>> =
27-
FreshContext<S>;
26+
export type RouteContext<_T = never, S = Record<string, unknown>> = Context<S>;
2827

2928
/**
3029
* @deprecated Use {@linkcode RouteHandler} instead.

src/define.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface Define<State> {
2121
* import { define } from "../utils.ts";
2222
*
2323
* export const handler = define.handlers((ctx) => {
24-
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
24+
* ctx.url; // ctx is inferred to be a Context object, so this is a URL
2525
* return new Response("Hello, world!");
2626
* });
2727
* ```
@@ -70,7 +70,7 @@ export interface Define<State> {
7070
* import { define } from "../utils.ts";
7171
*
7272
* export default define.page((props) => {
73-
* const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string
73+
* const slug = props.params.slug; // Because props is inferred to be a Context object, slug is inferred to be a string
7474
* return <h1>{slug}</h1>;
7575
* });
7676
* ```
@@ -124,7 +124,7 @@ export interface Define<State> {
124124
* import { define } from "../utils.ts";
125125
*
126126
* export const middleware = define.middleware((ctx) => {
127-
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
127+
* ctx.url; // ctx is inferred to be a Context object, so this is a URL
128128
* return ctx.next();
129129
* });
130130
* ```

src/handlers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FreshContext } from "./context.ts";
1+
import type { Context } from "./context.ts";
22
import type { Method } from "./router.ts";
33

44
export interface PageResponse<T> {
@@ -106,7 +106,7 @@ export function isHandlerByMethod<D, S>(
106106

107107
/**
108108
* A handler function that is invoked when a request is made to a route. The
109-
* handler function is passed a {@link FreshContext} object that contains the
109+
* handler function is passed a {@link Context} object that contains the
110110
* original request object, as well as any state related to the current request.
111111
*
112112
* The handler function can either return a {@link Response} object, which will
@@ -191,7 +191,7 @@ export function isHandlerByMethod<D, S>(
191191
* ```
192192
*/
193193
export interface HandlerFn<Data, State> {
194-
(ctx: FreshContext<State>):
194+
(ctx: Context<State>):
195195
| Response
196196
| PageResponse<Data>
197197
| Promise<Response | PageResponse<Data>>;

src/middlewares/cors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ export type CORSOptions<State> = {
3434
/**
3535
* CORS Middleware to set [`Cross-Origin-Resource-Sharing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) headers.
3636
*
37-
* @param {CORSOptions} [options] - The options for the CORS middleware.
38-
* @returns {(req: Request, ctx: FreshContext) => Promise<Response>} The Fresh middleware handler function.
37+
* @param [options] - The options for the CORS middleware.
38+
* @returns The Fresh middleware handler function.
3939
*
4040
* @example
4141
* ```ts

www/routes/_middleware.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import type { FreshContext } from "fresh";
1+
import type { Context } from "fresh";
22
import type { Event } from "$ga4";
33
import { GA4Report, isDocument, isServerError } from "$ga4";
44

55
const GA4_MEASUREMENT_ID = Deno.env.get("GA4_MEASUREMENT_ID");
66

77
let showedMissingEnvWarning = false;
88

9-
function ga4(
9+
function ga4<T>(
1010
request: Request,
11-
conn: FreshContext,
11+
conn: Context<T>,
1212
response: Response,
1313
_start: number,
1414
error?: unknown,
@@ -87,8 +87,8 @@ function ga4(
8787
});
8888
}
8989

90-
export async function handler(
91-
ctx: FreshContext,
90+
export async function handler<T>(
91+
ctx: Context<T>,
9292
): Promise<Response> {
9393
let err;
9494
let res: Response;

www/routes/docs/_middleware.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import type { FreshContext } from "fresh";
1+
import type { Context } from "fresh";
22

33
const REDIRECTS: Record<string, string> = {
44
"/docs/getting-started/fetching-data":
55
"/docs/getting-started/custom-handlers",
66
};
77

8-
export async function handler(
9-
ctx: FreshContext,
10-
) {
8+
export async function handler<T>(ctx: Context<T>) {
119
// Redirect from old doc URLs to new ones
1210
const redirect = REDIRECTS[ctx.url.pathname];
1311
if (redirect) {

0 commit comments

Comments
 (0)