www: new fenced code block design (#1629)
This commit is contained in:
@@ -12,9 +12,7 @@ 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
|
||||
|
||||
```tsx routes/_app.tsx
|
||||
import { AppProps } from "$fresh/server.ts";
|
||||
|
||||
export default function App({ Component, state }: AppProps) {
|
||||
|
||||
@@ -10,7 +10,7 @@ component through the `data` property on the `props`.
|
||||
|
||||
Here is an example:
|
||||
|
||||
```tsx
|
||||
```tsx routes/projects/[id].tsx
|
||||
interface Project {
|
||||
name: string;
|
||||
stars: number;
|
||||
|
||||
@@ -35,7 +35,7 @@ caching **will** cause your project to not function correctly.
|
||||
|
||||
Here is an example `Dockerfile` for a Fresh project:
|
||||
|
||||
```dockerfile
|
||||
```dockerfile Dockerfile
|
||||
FROM denoland/deno:1.35.0
|
||||
|
||||
ARG GIT_REVISION
|
||||
@@ -53,13 +53,13 @@ CMD ["run", "-A", "main.ts"]
|
||||
|
||||
To build your Docker image inside of a Git repository:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
$ docker build --build-arg GIT_REVISION=$(git rev-parse HEAD) -t my-fresh-app .
|
||||
```
|
||||
|
||||
Then run your Docker container:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
$ docker run -t -i -p 80:8000 my-fresh-app
|
||||
```
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ The 404 page can be customized by creating a `_404.tsx` file in the `routes/`
|
||||
folder. The file must have a default export that is a regular Preact component.
|
||||
A props object of type `UnknownPageProps` is passed in as an argument.
|
||||
|
||||
```tsx
|
||||
```tsx routes/_404.tsx
|
||||
import { UnknownPageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function NotFoundPage({ url }: UnknownPageProps) {
|
||||
@@ -29,7 +29,7 @@ In some cases, one needs to manually trigger the rendering of the 404 page, for
|
||||
example when the route did match, but the requested resource does not exist.
|
||||
This can be achieved with `ctx.renderNotFound`.
|
||||
|
||||
```tsx
|
||||
```tsx routes/blog/[slug].tsx
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
@@ -60,7 +60,7 @@ The 500 page can be customized by creating a `_500.tsx` file in the `routes/`
|
||||
folder. The file must have a default export that is a regular Preact component.
|
||||
A props object of type `ErrorPageProps` is passed in as an argument.
|
||||
|
||||
```tsx
|
||||
```tsx routes/_500.tsx
|
||||
import { ErrorPageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function Error500Page({ error }: ErrorPageProps) {
|
||||
|
||||
@@ -18,8 +18,7 @@ or as a `POST` request with `multipart/form-data`.
|
||||
This example demonstrates how to handle `multipart/form-data` `<form>`
|
||||
submissions:
|
||||
|
||||
```tsx
|
||||
// routes/subscribe.tsx
|
||||
```tsx routes/subscribe.tsx
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
|
||||
@@ -11,9 +11,7 @@ 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/my-island.tsx
|
||||
|
||||
```tsx islands/my-island.tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
export default function MyIsland() {
|
||||
@@ -31,8 +29,7 @@ export default function MyIsland() {
|
||||
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.
|
||||
|
||||
```tsx
|
||||
// route/index.tsx
|
||||
```tsx route/index.tsx
|
||||
import MyIsland from "../islands/my-island.tsx";
|
||||
|
||||
export default function Home() {
|
||||
@@ -44,9 +41,7 @@ export default function Home() {
|
||||
|
||||
Islands support passing JSX elements via the `children` property.
|
||||
|
||||
```tsx
|
||||
// islands/my-island.tsx
|
||||
|
||||
```tsx islands/my-island.tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { ComponentChildren } from "preact";
|
||||
|
||||
@@ -70,8 +65,7 @@ export default function MyIsland({ children }: Props) {
|
||||
This allows you to pass static content rendered by the server to an island in
|
||||
the browser.
|
||||
|
||||
```tsx
|
||||
// routes/index.tsx
|
||||
```tsx routes/index.tsx
|
||||
import MyIsland from "../islands/my-island.tsx";
|
||||
|
||||
export default function Home() {
|
||||
@@ -108,10 +102,10 @@ enough information to put the islands where they are supposed to go without
|
||||
requiring hydration for the static children of interactive islands. No
|
||||
Javascript is sent to the client when no interactivity is needed.
|
||||
|
||||
```HTML
|
||||
```html
|
||||
<!--frsh-myisland_default:default:0-->
|
||||
<div>
|
||||
Counter is at 0.
|
||||
Counter is at 0.
|
||||
<button>+</button>
|
||||
<!--frsh-slot-myisland_default:children-->
|
||||
<p>This text is rendered on the server</p>
|
||||
@@ -126,8 +120,7 @@ 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.
|
||||
|
||||
```tsx
|
||||
// islands/other-island.tsx
|
||||
```tsx islands/other-island.tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { ComponentChildren } from "preact";
|
||||
|
||||
@@ -147,8 +140,7 @@ export default function MyIsland({ children, foo }: Props) {
|
||||
<div>
|
||||
<p>String from props: {foo}</p>
|
||||
<p>
|
||||
<button onClick={() => number.value = randomNumber()}>Random</button>
|
||||
{" "}
|
||||
<button onClick={() => (number.value = randomNumber())}>Random</button>{" "}
|
||||
number is: {number}.
|
||||
</p>
|
||||
</div>
|
||||
@@ -160,8 +152,7 @@ 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.
|
||||
|
||||
```tsx
|
||||
// route/index.tsx
|
||||
```tsx route/index.tsx
|
||||
import MyIsland from "../islands/my-island.tsx";
|
||||
import OtherIsland from "../islands/other-island.tsx";
|
||||
|
||||
|
||||
@@ -15,8 +15,7 @@ be used to pass arbitrary data to downstream (or upstream) handlers. This
|
||||
special [\_app](/docs/concepts/app-wrapper.md) wrapper and normal
|
||||
[routes](/docs/concepts/routes.md).
|
||||
|
||||
```ts
|
||||
// routes/_middleware.ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
interface State {
|
||||
@@ -25,7 +24,7 @@ interface State {
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
ctx: MiddlewareHandlerContext<State>
|
||||
) {
|
||||
ctx.state.data = "myData";
|
||||
const resp = await ctx.next();
|
||||
@@ -34,8 +33,7 @@ export async function handler(
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
// routes/myHandler.ts
|
||||
```ts routes/myHandler.ts
|
||||
export const handler: Handlers<any, { data: string }> = {
|
||||
GET(_req, ctx) {
|
||||
return new Response(`middleware data is ${ctx.state.data}`);
|
||||
@@ -50,7 +48,7 @@ specific first).
|
||||
|
||||
For example, take a project with the following routes:
|
||||
|
||||
```
|
||||
```txt Project Structure
|
||||
└── routes
|
||||
├── _middleware.ts
|
||||
├── index.ts
|
||||
@@ -83,9 +81,7 @@ 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
|
||||
|
||||
```ts routes/_middleware.ts
|
||||
export const handler = [
|
||||
async function middleware1(req, ctx) {
|
||||
// do something
|
||||
@@ -101,7 +97,7 @@ 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
|
||||
```ts routes/[tenant]/admin/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
@@ -146,7 +142,7 @@ for a `route`, as opposed to something like `http://localhost:8001/favicon.ico`.
|
||||
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
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
@@ -159,7 +155,7 @@ export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
|
||||
If you start up your server (`deno task start`) you'll see the following:
|
||||
|
||||
```
|
||||
```sh Terminal
|
||||
Task start deno run -A --watch=static/,routes/ dev.ts
|
||||
Watcher Process started.
|
||||
The manifest has been generated for 4 routes and 1 islands.
|
||||
|
||||
@@ -6,9 +6,7 @@ Plugins can dynamically add new functionality to Fresh without exposing
|
||||
significant complexity to the user. Users can add plugins by importing and
|
||||
initializing them in their `main.ts` file:
|
||||
|
||||
```ts
|
||||
// main.ts
|
||||
|
||||
```ts main.ts
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
|
||||
|
||||
@@ -25,9 +25,7 @@ the handler's `render` function.
|
||||
|
||||
Let's look at a basic route that returns a plain text string:
|
||||
|
||||
```tsx
|
||||
// routes/plain.tsx
|
||||
|
||||
```tsx routes/plain.tsx
|
||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
@@ -48,9 +46,7 @@ have a corresponding handler, a 405 HTTP error is returned.
|
||||
|
||||
Now, let's render some HTML using the route component:
|
||||
|
||||
```tsx
|
||||
// routes/html.tsx
|
||||
|
||||
```tsx routes/html.tsx
|
||||
import { PageProps } from "$fresh/server.ts";
|
||||
|
||||
export default function Page(props: PageProps) {
|
||||
@@ -71,9 +67,7 @@ should work.
|
||||
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
|
||||
|
||||
```tsx routes/html.tsx
|
||||
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
@@ -96,7 +90,7 @@ 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
|
||||
```tsx routes/page.tsx
|
||||
interface Data {
|
||||
foo: number;
|
||||
}
|
||||
@@ -117,7 +111,7 @@ 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
|
||||
```tsx routes/page.tsx
|
||||
// Async route component
|
||||
export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||
const value = await loadFooValue();
|
||||
@@ -130,7 +124,7 @@ 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
|
||||
```tsx routes/page.tsx
|
||||
export const handler: Handlers = {
|
||||
async POST(req) {
|
||||
// ... do something here
|
||||
@@ -148,7 +142,7 @@ export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||
Quite often a route handler needs to render a 404 page or bail out of rendering
|
||||
in another manner. This can be done by returning a `Response` object.
|
||||
|
||||
```tsx
|
||||
```tsx route/page.tsx
|
||||
// Async route component
|
||||
export default async function MyPage(req: Request, ctx: RouteContext) {
|
||||
const value = await loadFooValue();
|
||||
|
||||
@@ -41,9 +41,7 @@ Advanced use-cases can require that a more complex pattern be used for matching.
|
||||
A custom [URL pattern][urlpattern] can be specified in the route configuration.
|
||||
This pattern will be used instead of the file path based pattern:
|
||||
|
||||
```ts
|
||||
// routes/x.ts
|
||||
|
||||
```ts routes/x.ts
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const config: RouteConfig = {
|
||||
|
||||
@@ -7,7 +7,7 @@ In this page we discuss how the server can be configured during startup.
|
||||
|
||||
The signature of the primary method looks like this:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
export async function start(routes: Manifest, opts: StartOptions = {});
|
||||
```
|
||||
|
||||
@@ -52,7 +52,7 @@ export interface RouterOptions {
|
||||
See the [docs](/docs/concepts/plugins) on this topic for more detail. But for
|
||||
completion, you can do something like this to load plugins:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
||||
```
|
||||
|
||||
@@ -61,7 +61,7 @@ await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
||||
This allows you to specify the location where your site's static assets are
|
||||
stored. Here's an example:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
await start(manifest, { staticDir: "./custom_static" });
|
||||
```
|
||||
|
||||
@@ -81,6 +81,6 @@ By default Fresh uses URLs like `https://www.example.com/about`. If you'd like,
|
||||
you can configure this to `https://www.example.com/about/` by using the
|
||||
`trailingSlash` setting.
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
await start(manifest, { router: { trailingSlash: true } });
|
||||
```
|
||||
|
||||
@@ -29,7 +29,7 @@ version of this path that contains a build ID for cache busting. When the asset
|
||||
is requested at this "locked" path, it will be served with a cache lifetime of
|
||||
one year.
|
||||
|
||||
```jsx
|
||||
```jsx routes/page.tsx
|
||||
import { asset } from "$fresh/runtime.ts";
|
||||
|
||||
export default function Page() {
|
||||
|
||||
@@ -41,7 +41,7 @@ recommended patterns for Fresh projects.
|
||||
To run the auto updater, run the following command from the root of your
|
||||
project:
|
||||
|
||||
```bash
|
||||
```sh Terminal
|
||||
$ deno run -A -r https://fresh.deno.dev/update .
|
||||
```
|
||||
|
||||
@@ -54,7 +54,7 @@ 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
|
||||
```diff deno.json
|
||||
{
|
||||
"imports": {
|
||||
- "$fresh/": "https://deno.land/x/fresh@1.0.2/",
|
||||
|
||||
Reference in New Issue
Block a user