docs: update Fresh 2 migration info (#3161)

This commit is contained in:
Marvin Hagemeister
2025-07-31 10:46:44 +02:00
committed by GitHub
parent c08710f7dd
commit 62f8bb008e
7 changed files with 28 additions and 29 deletions
+7 -5
View File
@@ -70,14 +70,16 @@ if (Deno.args.includes("build")) {
} }
``` ```
> [info]: Fresh 1.x used Tailwind CSS v3. To keep using v3 use the
> `@fresh/plugin-tailwind-v3` instead.
### Updating `main.ts` ### Updating `main.ts`
Similarly, configuration related to running Fresh in production can be passed to Similarly, configuration related to running Fresh in production can be passed to
`new App()`: `new App()`:
```ts ```ts main.ts
// main.ts import { App, staticFiles } from "fresh";
import { App, fsRoutes, staticFiles } from "fresh";
export const app = new App() export const app = new App()
// Add static file serving middleware // Add static file serving middleware
@@ -102,7 +104,7 @@ Both the `_500.tsx` and `_404.tsx` template have been unified into a single
Inside the `_error.tsx` template you can show different content based on errors Inside the `_error.tsx` template you can show different content based on errors
or status codes with the following code: or status codes with the following code:
```tsx ```tsx routes/_error.tsx
export default function ErrorPage(props: PageProps) { export default function ErrorPage(props: PageProps) {
const error = props.error; // Contains the thrown Error or HTTPError const error = props.error; // Contains the thrown Error or HTTPError
if (error instanceof HttpError) { if (error instanceof HttpError) {
@@ -138,7 +140,7 @@ export const handler = {
}; };
// Render that in _app.tsx // Render that in _app.tsx
export default function AppWrapper(ctx: FreshContext) { export default function AppWrapper(ctx: Context) {
return ( return (
<html lang="en"> <html lang="en">
<head> <head>
+6 -7
View File
@@ -1,30 +1,29 @@
import type { ComponentChildren } from "preact"; import type { ComponentChildren } from "preact";
import type { FreshContext } from "./context.ts"; import type { Context } from "./context.ts";
import type { PageProps } from "./render.ts"; import type { PageProps } from "./render.ts";
import type { HandlerFn, RouteHandler } from "./handlers.ts"; import type { HandlerFn, RouteHandler } from "./handlers.ts";
/** /**
* @deprecated Use {@linkcode PageProps} instead. * @deprecated Use {@linkcode PageProps} instead.
*/ */
export type AppProps<_Data = unknown, T = unknown> = FreshContext<T>; export type AppProps<_Data = unknown, T = unknown> = Context<T>;
/** /**
* @deprecated Use {@linkcode PageProps} instead. * @deprecated Use {@linkcode PageProps} instead.
*/ */
export type LayoutProps<_Data = unknown, T = unknown> = FreshContext<T>; export type LayoutProps<_Data = unknown, T = unknown> = Context<T>;
/** /**
* @deprecated Use {@linkcode PageProps} instead. * @deprecated Use {@linkcode PageProps} instead.
*/ */
export type UnknownPageProps<_Data = unknown, T = unknown> = FreshContext<T>; export type UnknownPageProps<_Data = unknown, T = unknown> = Context<T>;
/** /**
* @deprecated Use {@linkcode PageProps} instead. * @deprecated Use {@linkcode PageProps} instead.
*/ */
export type ErrorPageProps<_Data = unknown, T = unknown> = FreshContext<T>; export type ErrorPageProps<_Data = unknown, T = unknown> = Context<T>;
/** /**
* @deprecated Use {@linkcode FreshContext} instead. * @deprecated Use {@linkcode FreshContext} instead.
*/ */
export type RouteContext<_T = never, S = Record<string, unknown>> = export type RouteContext<_T = never, S = Record<string, unknown>> = Context<S>;
FreshContext<S>;
/** /**
* @deprecated Use {@linkcode RouteHandler} instead. * @deprecated Use {@linkcode RouteHandler} instead.
+3 -3
View File
@@ -21,7 +21,7 @@ export interface Define<State> {
* import { define } from "../utils.ts"; * import { define } from "../utils.ts";
* *
* export const handler = define.handlers((ctx) => { * export const handler = define.handlers((ctx) => {
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL * ctx.url; // ctx is inferred to be a Context object, so this is a URL
* return new Response("Hello, world!"); * return new Response("Hello, world!");
* }); * });
* ``` * ```
@@ -70,7 +70,7 @@ export interface Define<State> {
* import { define } from "../utils.ts"; * import { define } from "../utils.ts";
* *
* export default define.page((props) => { * export default define.page((props) => {
* const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string * const slug = props.params.slug; // Because props is inferred to be a Context object, slug is inferred to be a string
* return <h1>{slug}</h1>; * return <h1>{slug}</h1>;
* }); * });
* ``` * ```
@@ -124,7 +124,7 @@ export interface Define<State> {
* import { define } from "../utils.ts"; * import { define } from "../utils.ts";
* *
* export const middleware = define.middleware((ctx) => { * export const middleware = define.middleware((ctx) => {
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL * ctx.url; // ctx is inferred to be a Context object, so this is a URL
* return ctx.next(); * return ctx.next();
* }); * });
* ``` * ```
+3 -3
View File
@@ -1,4 +1,4 @@
import type { FreshContext } from "./context.ts"; import type { Context } from "./context.ts";
import type { Method } from "./router.ts"; import type { Method } from "./router.ts";
export interface PageResponse<T> { export interface PageResponse<T> {
@@ -106,7 +106,7 @@ export function isHandlerByMethod<D, S>(
/** /**
* A handler function that is invoked when a request is made to a route. The * A handler function that is invoked when a request is made to a route. The
* handler function is passed a {@link FreshContext} object that contains the * handler function is passed a {@link Context} object that contains the
* original request object, as well as any state related to the current request. * original request object, as well as any state related to the current request.
* *
* The handler function can either return a {@link Response} object, which will * The handler function can either return a {@link Response} object, which will
@@ -191,7 +191,7 @@ export function isHandlerByMethod<D, S>(
* ``` * ```
*/ */
export interface HandlerFn<Data, State> { export interface HandlerFn<Data, State> {
(ctx: FreshContext<State>): (ctx: Context<State>):
| Response | Response
| PageResponse<Data> | PageResponse<Data>
| Promise<Response | PageResponse<Data>>; | Promise<Response | PageResponse<Data>>;
+2 -2
View File
@@ -34,8 +34,8 @@ export type CORSOptions<State> = {
/** /**
* CORS Middleware to set [`Cross-Origin-Resource-Sharing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) headers. * CORS Middleware to set [`Cross-Origin-Resource-Sharing`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) headers.
* *
* @param {CORSOptions} [options] - The options for the CORS middleware. * @param [options] - The options for the CORS middleware.
* @returns {(req: Request, ctx: FreshContext) => Promise<Response>} The Fresh middleware handler function. * @returns The Fresh middleware handler function.
* *
* @example * @example
* ```ts * ```ts
+5 -5
View File
@@ -1,4 +1,4 @@
import type { FreshContext } from "fresh"; import type { Context } from "fresh";
import type { Event } from "$ga4"; import type { Event } from "$ga4";
import { GA4Report, isDocument, isServerError } from "$ga4"; import { GA4Report, isDocument, isServerError } from "$ga4";
@@ -6,9 +6,9 @@ const GA4_MEASUREMENT_ID = Deno.env.get("GA4_MEASUREMENT_ID");
let showedMissingEnvWarning = false; let showedMissingEnvWarning = false;
function ga4( function ga4<T>(
request: Request, request: Request,
conn: FreshContext, conn: Context<T>,
response: Response, response: Response,
_start: number, _start: number,
error?: unknown, error?: unknown,
@@ -87,8 +87,8 @@ function ga4(
}); });
} }
export async function handler( export async function handler<T>(
ctx: FreshContext, ctx: Context<T>,
): Promise<Response> { ): Promise<Response> {
let err; let err;
let res: Response; let res: Response;
+2 -4
View File
@@ -1,13 +1,11 @@
import type { FreshContext } from "fresh"; import type { Context } from "fresh";
const REDIRECTS: Record<string, string> = { const REDIRECTS: Record<string, string> = {
"/docs/getting-started/fetching-data": "/docs/getting-started/fetching-data":
"/docs/getting-started/custom-handlers", "/docs/getting-started/custom-handlers",
}; };
export async function handler( export async function handler<T>(ctx: Context<T>) {
ctx: FreshContext,
) {
// Redirect from old doc URLs to new ones // Redirect from old doc URLs to new ones
const redirect = REDIRECTS[ctx.url.pathname]; const redirect = REDIRECTS[ctx.url.pathname];
if (redirect) { if (redirect) {