www: new fenced code block design (#1629)
This commit is contained in:
@@ -6,7 +6,7 @@ description: |
|
||||
When you initialize a project with `deno run -A -r https://fresh.deno.dev`,
|
||||
you'll end up with a project like the following:
|
||||
|
||||
```
|
||||
```txt Project Structure
|
||||
.
|
||||
├── README.md
|
||||
├── components
|
||||
@@ -40,7 +40,7 @@ your choosing), then you'll need to do the following things:
|
||||
|
||||
Here's what the diff of `deno.json` looks like:
|
||||
|
||||
```diff
|
||||
```diff deno.json
|
||||
{
|
||||
"lock": false,
|
||||
"tasks": {
|
||||
@@ -53,7 +53,7 @@ Here's what the diff of `deno.json` looks like:
|
||||
|
||||
The resulting file structure looks like this:
|
||||
|
||||
```
|
||||
```txt Project Structure
|
||||
.
|
||||
├── README.md
|
||||
├── deno.json
|
||||
|
||||
@@ -19,12 +19,12 @@ In this example we'll be creating a small API that uses
|
||||
Our project structure will look like this (in addition to the rest of the Fresh
|
||||
code from a new project):
|
||||
|
||||
```
|
||||
├── routes
|
||||
│ └── api
|
||||
│ └── users
|
||||
│ ├── [id].ts
|
||||
│ └── index.ts
|
||||
```txt Project Structure
|
||||
routes
|
||||
└── api
|
||||
└── users
|
||||
├── [id].ts
|
||||
└── index.ts
|
||||
```
|
||||
|
||||
In each section about a method, only the relevant handler will be shown. The
|
||||
@@ -34,15 +34,12 @@ full files are available at the bottom for reference.
|
||||
|
||||
`POST` (create) is used to create a resource.
|
||||
|
||||
```tsx
|
||||
// routes/api/users/index.ts
|
||||
```tsx routes/api/users/index.ts
|
||||
export const handler: Handlers<User | null> = {
|
||||
async POST(req, _ctx) {
|
||||
const user = await req.json() as User;
|
||||
const user = (await req.json()) as User;
|
||||
const userKey = ["user", user.id];
|
||||
const ok = await kv.atomic()
|
||||
.set(userKey, user)
|
||||
.commit();
|
||||
const ok = await kv.atomic().set(userKey, user).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(JSON.stringify(user));
|
||||
},
|
||||
@@ -71,8 +68,7 @@ You should receive the same thing back:
|
||||
`GET` (read) is used to retrieve a resource and is by far the most common HTTP
|
||||
method. You can use `GET` to fetch database content, markdown, or static files.
|
||||
|
||||
```tsx
|
||||
// routes/api/users/[id].ts
|
||||
```tsx routes/api/users/[id].ts
|
||||
export const handler: Handlers<User | null> = {
|
||||
async GET(_req, ctx) {
|
||||
const id = ctx.params.id;
|
||||
@@ -102,19 +98,15 @@ The short version of it: `PUT` requires the entire object to be submitted, while
|
||||
|
||||
An example of an update endpoint using `PUT`:
|
||||
|
||||
```tsx
|
||||
// routes/api/users/[id].ts
|
||||
```tsx routes/api/users/[id].ts
|
||||
export const handler: Handlers<User | null> = {
|
||||
async PUT(req, ctx) {
|
||||
const id = ctx.params.id;
|
||||
const user = await req.json() as User;
|
||||
const user = (await req.json()) as User;
|
||||
const userKey = ["user", id];
|
||||
const userRes = await kv.get(userKey);
|
||||
if (!userRes.value) return new Response(`no user with id ${id} found`);
|
||||
const ok = await kv.atomic()
|
||||
.check(userRes)
|
||||
.set(userKey, user)
|
||||
.commit();
|
||||
const ok = await kv.atomic().check(userRes).set(userKey, user).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(JSON.stringify(user));
|
||||
},
|
||||
@@ -152,18 +144,14 @@ No need to send in the id in this case.
|
||||
|
||||
`DELETE` (delete) is used to delete a resource.
|
||||
|
||||
```tsx
|
||||
// routes/api/users/[id].ts
|
||||
```tsx routes/api/users/[id].ts
|
||||
export const handler: Handlers<User | null> = {
|
||||
async DELETE(_req, ctx) {
|
||||
const id = ctx.params.id;
|
||||
const userKey = ["user", id];
|
||||
const userRes = await kv.get(userKey);
|
||||
if (!userRes.value) return new Response(`no user with id ${id} found`);
|
||||
const ok = await kv.atomic()
|
||||
.check(userRes)
|
||||
.delete(userKey)
|
||||
.commit();
|
||||
const ok = await kv.atomic().check(userRes).delete(userKey).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(`user ${id} deleted`);
|
||||
},
|
||||
@@ -173,7 +161,7 @@ export const handler: Handlers<User | null> = {
|
||||
Try sending `DELETE` to `http://localhost:8000/api/users/2` without a body.
|
||||
We'll get back:
|
||||
|
||||
```
|
||||
```txt
|
||||
user 2 deleted
|
||||
```
|
||||
|
||||
@@ -210,23 +198,17 @@ export const handler: Handlers<User | null> = {
|
||||
const userKey = ["user", id];
|
||||
const userRes = await kv.get(userKey);
|
||||
if (!userRes.value) return new Response(`no user with id ${id} found`);
|
||||
const ok = await kv.atomic()
|
||||
.check(userRes)
|
||||
.delete(userKey)
|
||||
.commit();
|
||||
const ok = await kv.atomic().check(userRes).delete(userKey).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(`user ${id} deleted`);
|
||||
},
|
||||
async PUT(req, ctx) {
|
||||
const id = ctx.params.id;
|
||||
const user = await req.json() as User;
|
||||
const user = (await req.json()) as User;
|
||||
const userKey = ["user", id];
|
||||
const userRes = await kv.get(userKey);
|
||||
if (!userRes.value) return new Response(`no user with id ${id} found`);
|
||||
const ok = await kv.atomic()
|
||||
.check(userRes)
|
||||
.set(userKey, user)
|
||||
.commit();
|
||||
const ok = await kv.atomic().check(userRes).set(userKey, user).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(JSON.stringify(user));
|
||||
},
|
||||
@@ -257,11 +239,9 @@ export const handler: Handlers<User | null> = {
|
||||
return new Response(JSON.stringify(users));
|
||||
},
|
||||
async POST(req, _ctx) {
|
||||
const user = await req.json() as User;
|
||||
const user = (await req.json()) as User;
|
||||
const userKey = ["user", user.id];
|
||||
const ok = await kv.atomic()
|
||||
.set(userKey, user)
|
||||
.commit();
|
||||
const ok = await kv.atomic().set(userKey, user).commit();
|
||||
if (!ok) throw new Error("Something went wrong.");
|
||||
return new Response(JSON.stringify(user));
|
||||
},
|
||||
|
||||
@@ -15,13 +15,10 @@ As per the above link, "simple" requests involve `GET`, `HEAD`, or `POST`
|
||||
requests. You can CORS enable all the routes affected by some `middleware` by
|
||||
doing the following:
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
) {
|
||||
export async function handler(req: Request, ctx: MiddlewareHandlerContext) {
|
||||
const origin = req.headers.get("Origin") || "*";
|
||||
const resp = await ctx.next();
|
||||
const headers = resp.headers;
|
||||
@@ -30,11 +27,11 @@ export async function handler(
|
||||
headers.set("Access-Control-Allow-Credentials", "true");
|
||||
headers.set(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With",
|
||||
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"
|
||||
);
|
||||
headers.set(
|
||||
"Access-Control-Allow-Methods",
|
||||
"POST, OPTIONS, GET, PUT, DELETE",
|
||||
"POST, OPTIONS, GET, PUT, DELETE"
|
||||
);
|
||||
|
||||
return resp;
|
||||
@@ -47,13 +44,10 @@ What about for one of the other HTTP methods? Then you'll need to be able to
|
||||
deal with "preflight requests". Let's imagine you're trying to support a
|
||||
`DELETE` route. Then you'd need to do something like this:
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export async function handler(
|
||||
_req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
) {
|
||||
export async function handler(_req: Request, ctx: MiddlewareHandlerContext) {
|
||||
if (_req.method == "OPTIONS") {
|
||||
const resp = new Response(null, {
|
||||
status: 204,
|
||||
@@ -61,10 +55,7 @@ export async function handler(
|
||||
const origin = _req.headers.get("Origin") || "*";
|
||||
const headers = resp.headers;
|
||||
headers.set("Access-Control-Allow-Origin", origin);
|
||||
headers.set(
|
||||
"Access-Control-Allow-Methods",
|
||||
"DELETE",
|
||||
);
|
||||
headers.set("Access-Control-Allow-Methods", "DELETE");
|
||||
return resp;
|
||||
}
|
||||
const origin = _req.headers.get("Origin") || "*";
|
||||
@@ -75,11 +66,11 @@ export async function handler(
|
||||
headers.set("Access-Control-Allow-Credentials", "true");
|
||||
headers.set(
|
||||
"Access-Control-Allow-Headers",
|
||||
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With",
|
||||
"Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With"
|
||||
);
|
||||
headers.set(
|
||||
"Access-Control-Allow-Methods",
|
||||
"POST, OPTIONS, GET, PUT, DELETE",
|
||||
"POST, OPTIONS, GET, PUT, DELETE"
|
||||
);
|
||||
|
||||
return resp;
|
||||
|
||||
@@ -16,7 +16,7 @@ Here you can define named groups, wildcards, regex groups, and other bits.
|
||||
Let's look at the example from the routing page more closely. We'll flesh out
|
||||
the handler so that we end up with something like the following:
|
||||
|
||||
```ts
|
||||
```ts routes/x.tsx
|
||||
import { HandlerContext, RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const handler = {
|
||||
@@ -47,7 +47,7 @@ show the following:
|
||||
|
||||
Let's look at something a bit more complex:
|
||||
|
||||
```ts
|
||||
```ts routes/api.tsx
|
||||
import { HandlerContext, RouteConfig } from "$fresh/server.ts";
|
||||
|
||||
export const handler = {
|
||||
|
||||
@@ -7,7 +7,7 @@ Let's pretend you've just initialized a new Fresh project. You want to do some
|
||||
complicated setup that runs once, before the server is started. This is,
|
||||
fortunately, quite easy. Here's how:
|
||||
|
||||
```diff
|
||||
```diff main.ts
|
||||
import { start } from "$fresh/server.ts";
|
||||
import manifest from "./fresh.gen.ts";
|
||||
+import { Context } from "./routes/_middleware.ts";
|
||||
@@ -18,7 +18,7 @@ fortunately, quite easy. Here's how:
|
||||
|
||||
So your full `main.ts` should look like this:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
@@ -37,7 +37,7 @@ await start(manifest);
|
||||
|
||||
But what's going on in this new `_middleware.ts` we've created?
|
||||
|
||||
```ts
|
||||
```ts routes/_middleware.ts
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export interface State {
|
||||
@@ -66,7 +66,7 @@ export class Context {
|
||||
|
||||
export async function handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
ctx: MiddlewareHandlerContext<State>
|
||||
) {
|
||||
ctx.state.context = Context.instance();
|
||||
const resp = await ctx.next();
|
||||
|
||||
@@ -13,8 +13,7 @@ the web page. Some uses include:
|
||||
- Linking to resources like stylesheets using `<link>`
|
||||
- Including third-party JavaScript code using `<script>`
|
||||
|
||||
```tsx
|
||||
// routes/index.tsx
|
||||
```tsx routes/index.tsx
|
||||
import { Head } from "$fresh/runtime.ts";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
|
||||
|
||||
@@ -14,8 +14,7 @@ The following file uses
|
||||
[dynamic routing](https://fresh.deno.dev/docs/getting-started/dynamic-routes) to
|
||||
handle the three cases. It's assumed this file is called `[slug].tsx`:
|
||||
|
||||
```ts
|
||||
// routes/[slug].tsx
|
||||
```ts routes/[slug].tsx
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { extract } from "$std/front_matter/yaml.ts";
|
||||
import { CSS, render } from "$gfm";
|
||||
@@ -31,7 +30,7 @@ export const handler: Handlers<Page> = {
|
||||
let rawMarkdown = "";
|
||||
if (ctx.params.slug === "remote") {
|
||||
const resp = await fetch(
|
||||
`https://raw.githubusercontent.com/denoland/fresh/main/docs/introduction/index.md`,
|
||||
`https://raw.githubusercontent.com/denoland/fresh/main/docs/introduction/index.md`
|
||||
);
|
||||
if (resp.status !== 200) {
|
||||
return ctx.render(undefined);
|
||||
@@ -81,7 +80,7 @@ export default function MarkdownPage({ data }: PageProps<Page | null>) {
|
||||
|
||||
The contents of the `text.md` file are the following:
|
||||
|
||||
```md
|
||||
```md text.md
|
||||
---
|
||||
description: testFromText
|
||||
---
|
||||
|
||||
@@ -6,7 +6,7 @@ description: |
|
||||
When you initialize a project with `deno run -A -r https://fresh.deno.dev`,
|
||||
you'll end up with a `main.ts` like the following:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
@@ -26,7 +26,7 @@ This is a great start if your site is in English, but let's say you want to
|
||||
change the language, as per the `<html lang=asdf>` tag. Then you'll need to do
|
||||
something like this:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
|
||||
@@ -11,8 +11,7 @@ All of this content is lifted from this great
|
||||
|
||||
Imagine we have `Counter.tsx` like this:
|
||||
|
||||
```ts
|
||||
// islands/Counter.tsx
|
||||
```tsx islands/Counter.tsx
|
||||
import { useSignal } from "@preact/signals";
|
||||
import { Button } from "../components/Button.tsx";
|
||||
|
||||
@@ -37,7 +36,7 @@ export default function Counter(props: CounterProps) {
|
||||
Note how `useSignal` is within the `Counter` component. Then if we instantiate
|
||||
some counters like this...
|
||||
|
||||
```ts
|
||||
```tsx
|
||||
<Counter start={3} />
|
||||
<Counter start={4} />
|
||||
```
|
||||
@@ -49,8 +48,7 @@ here, yet.
|
||||
|
||||
But we can switch things up by looking at a `SynchronizedSlider.tsx` like this:
|
||||
|
||||
```ts
|
||||
// islands/SynchronizedSlider.tsx
|
||||
```tsx islands/SynchronizedSlider.tsx
|
||||
import { Signal } from "@preact/signals";
|
||||
|
||||
interface SliderProps {
|
||||
@@ -67,7 +65,7 @@ export default function SynchronizedSlider(props: SliderProps) {
|
||||
min={1}
|
||||
max={100}
|
||||
value={props.slider.value}
|
||||
onInput={(e) => props.slider.value = Number(e.currentTarget.value)}
|
||||
onInput={(e) => (props.slider.value = Number(e.currentTarget.value))}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -75,8 +73,7 @@ export default function SynchronizedSlider(props: SliderProps) {
|
||||
|
||||
Now if we were to do the following...
|
||||
|
||||
```ts
|
||||
// routes/index.tsx
|
||||
```tsx routes/index.tsx
|
||||
export default function Home() {
|
||||
const sliderSignal = useSignal(50);
|
||||
return (
|
||||
@@ -96,15 +93,13 @@ they would all use the same value.
|
||||
We can also create a `signal` in a utility file and export it for consumption
|
||||
across multiple places.
|
||||
|
||||
```ts
|
||||
// utils/cart.ts
|
||||
```ts utils/cart.ts
|
||||
import { signal } from "@preact/signals";
|
||||
|
||||
export const cart = signal<string[]>([]);
|
||||
```
|
||||
|
||||
```ts
|
||||
// islands/AddToCart.tsx
|
||||
```tsx islands/AddToCart.tsx
|
||||
import { Button } from "../components/Button.tsx";
|
||||
import { cart } from "../utils/cart.ts";
|
||||
|
||||
@@ -116,18 +111,17 @@ interface AddToCartProps {
|
||||
export default function AddToCart(props: AddToCartProps) {
|
||||
return (
|
||||
<Button
|
||||
onClick={() => cart.value = [...cart.value, props.product]}
|
||||
onClick={() => (cart.value = [...cart.value, props.product])}
|
||||
class="w-full"
|
||||
>
|
||||
Add{cart.value.includes(props.product) ? " another" : ""}{" "}
|
||||
"{props.product}" to cart
|
||||
Add{cart.value.includes(props.product) ? " another" : ""} "{props.product}
|
||||
" to cart
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
```ts
|
||||
// islands/Cart.tsx
|
||||
```tsx islands/Cart.tsx
|
||||
import { Button } from "../components/Button.tsx";
|
||||
import { cart } from "../utils/cart.ts";
|
||||
import * as icons from "../components/Icons.tsx";
|
||||
@@ -185,7 +179,7 @@ function CartItem(props: CartItemProps) {
|
||||
|
||||
Now we can add the islands to our site by doing the following:
|
||||
|
||||
```ts
|
||||
```tsx
|
||||
<AddToCart product="Lemon" />
|
||||
<AddToCart product="Lime" />
|
||||
<Cart />
|
||||
|
||||
@@ -145,8 +145,7 @@ applied to any of the directives.
|
||||
|
||||
We'll start off by having an example stylesheet defined like this:
|
||||
|
||||
```css
|
||||
/* static/example.css */
|
||||
```css static/example.css
|
||||
h1 {
|
||||
font-size: 25px;
|
||||
font-weight: normal;
|
||||
@@ -161,8 +160,7 @@ To kick things off, we'll create the following control route which doesn't do
|
||||
anything with CSP. We include a stylesheet to confirm that our sheet correctly
|
||||
styles the response.
|
||||
|
||||
```tsx
|
||||
// routes/noCSP.tsx
|
||||
```tsx routes/noCSP.tsx
|
||||
import { RouteContext } from "$fresh/server.ts";
|
||||
|
||||
export default function Home(req: Request, ctx: RouteContext) {
|
||||
@@ -177,7 +175,7 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
|
||||
We can hit `http://localhost:8000/noCSP` and we should see the following:
|
||||
|
||||
```
|
||||
```txt
|
||||
This page doesn't use CSP at all. Styles will be applied.
|
||||
```
|
||||
|
||||
@@ -188,8 +186,7 @@ closely, we're using the wrong URL! This will cause the browser to reject the
|
||||
stylesheet, due to the header that Fresh produces. We get a `(blocked:csp)`
|
||||
status when the browser tries to request this resource.
|
||||
|
||||
```tsx
|
||||
// routes/incorrectCSP.tsx
|
||||
```tsx routes/incorrectCSP.tsx
|
||||
import { RouteConfig, RouteContext } from "$fresh/server.ts";
|
||||
import { useCSP } from "$fresh/runtime.ts";
|
||||
|
||||
@@ -198,9 +195,7 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
if (!csp.directives.styleSrc) {
|
||||
csp.directives.styleSrc = [];
|
||||
}
|
||||
csp.directives.styleSrc.push(
|
||||
"http://www.example.com",
|
||||
);
|
||||
csp.directives.styleSrc.push("http://www.example.com");
|
||||
});
|
||||
return (
|
||||
<>
|
||||
@@ -217,7 +212,7 @@ export const config: RouteConfig = {
|
||||
|
||||
We can hit `http://localhost:8000/incorrectCSP` and we should see the following:
|
||||
|
||||
```
|
||||
```txt
|
||||
This page violates our configured CSP. Styles won't be applied.
|
||||
```
|
||||
|
||||
@@ -226,8 +221,7 @@ This page violates our configured CSP. Styles won't be applied.
|
||||
Let's fix our simple mistake and use the correct URL. Everything is working
|
||||
correctly here.
|
||||
|
||||
```tsx
|
||||
// routes/correctCSP.tsx
|
||||
```tsx routes/correctCSP.tsx
|
||||
import { RouteConfig, RouteContext } from "$fresh/server.ts";
|
||||
import { useCSP } from "$fresh/runtime.ts";
|
||||
|
||||
@@ -236,9 +230,7 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
if (!csp.directives.styleSrc) {
|
||||
csp.directives.styleSrc = [];
|
||||
}
|
||||
csp.directives.styleSrc.push(
|
||||
"http://localhost:8000/example.css",
|
||||
);
|
||||
csp.directives.styleSrc.push("http://localhost:8000/example.css");
|
||||
});
|
||||
return (
|
||||
<>
|
||||
@@ -255,7 +247,7 @@ export const config: RouteConfig = {
|
||||
|
||||
We can hit `http://localhost:8000/correctCSP` and we should see the following:
|
||||
|
||||
```
|
||||
```txt
|
||||
This page adheres to our configured CSP. Styles will be applied.
|
||||
```
|
||||
|
||||
@@ -263,8 +255,7 @@ This page adheres to our configured CSP. Styles will be applied.
|
||||
|
||||
What happens if we forget to use a `RouteConfig` in our route?
|
||||
|
||||
```tsx
|
||||
// routes/cspNoRouteConfig.tsx
|
||||
```tsx routes/cspNoRouteConfig.tsx
|
||||
import { RouteContext } from "$fresh/server.ts";
|
||||
import { useCSP } from "$fresh/runtime.ts";
|
||||
|
||||
@@ -273,16 +264,14 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
if (!csp.directives.styleSrc) {
|
||||
csp.directives.styleSrc = [];
|
||||
}
|
||||
csp.directives.styleSrc.push(
|
||||
"http://www.example.com",
|
||||
);
|
||||
csp.directives.styleSrc.push("http://www.example.com");
|
||||
});
|
||||
return (
|
||||
<>
|
||||
<h1>
|
||||
This page violates our configured CSP. But we don't have a{" "}
|
||||
<code>RouteConfig</code>{" "}
|
||||
enabled, so Fresh doesn't know to use the CSP. Styles will be applied.
|
||||
<code>RouteConfig</code> enabled, so Fresh doesn't know to use the CSP.
|
||||
Styles will be applied.
|
||||
</h1>
|
||||
<link rel="stylesheet" type="text/css" href="example.css" />
|
||||
</>
|
||||
@@ -293,7 +282,7 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
We can hit `http://localhost:8000/cspNoRouteConfig` and we should see the
|
||||
following:
|
||||
|
||||
```
|
||||
```txt
|
||||
This page violates our configured CSP. But we don't have a RouteConfig enabled, so Fresh doesn't know to use the CSP. Styles will be applied.
|
||||
```
|
||||
|
||||
@@ -305,8 +294,7 @@ should be able to receive `POST` requests. If the `reportOnly` flag is enabled,
|
||||
then the browser will ignore the CSP headers and log any issues to the
|
||||
`reportUri` destination.
|
||||
|
||||
```tsx
|
||||
// routes/incorrectCSPwithReport.tsx
|
||||
```tsx routes/incorrectCSPwithReport.tsx
|
||||
import { RouteConfig, RouteContext } from "$fresh/server.ts";
|
||||
import { useCSP } from "$fresh/runtime.ts";
|
||||
|
||||
@@ -317,9 +305,7 @@ export default function Home(req: Request, ctx: RouteContext) {
|
||||
csp.directives.styleSrc = [];
|
||||
}
|
||||
csp.directives.reportUri = "http://localhost:8000/reportHandler";
|
||||
csp.directives.styleSrc.push(
|
||||
"http://www.example.com",
|
||||
);
|
||||
csp.directives.styleSrc.push("http://www.example.com");
|
||||
});
|
||||
return (
|
||||
<>
|
||||
@@ -337,8 +323,7 @@ export const config: RouteConfig = {
|
||||
};
|
||||
```
|
||||
|
||||
```ts
|
||||
// routes/reportHandler.ts
|
||||
```ts routes/reportHandler.ts
|
||||
import { HandlerContext } from "$fresh/server.ts";
|
||||
|
||||
export const handler = {
|
||||
@@ -357,14 +342,14 @@ export const handler = {
|
||||
We can hit `http://localhost:8000/incorrectCSPwithReport` and we should see the
|
||||
following:
|
||||
|
||||
```
|
||||
```txt
|
||||
This page violates our configured CSP. But we're using "reportOnly". Styles will be applied.
|
||||
```
|
||||
|
||||
We can then check our server and we'll see that `csp-reports.txt` has an entry
|
||||
like this:
|
||||
|
||||
```json
|
||||
```json csp-reports.txt
|
||||
{
|
||||
"csp-report": {
|
||||
"document-uri": "http://localhost:8000/incorrectCSPwithReport",
|
||||
|
||||
@@ -22,8 +22,7 @@ available.
|
||||
2. Create your pre-configured OAuth client instance. For reusability the
|
||||
instance is stored in `utils/oauth2_client.ts`.
|
||||
|
||||
```ts
|
||||
// utils/oauth2_client.ts
|
||||
```ts utils/oauth2_client.ts
|
||||
import { createGitHubOAuth2Client } from "https://deno.land/x/deno_kv_oauth@v0.2.4/mod.ts";
|
||||
|
||||
export const oauth2Client = createGitHubOAuth2Client();
|
||||
@@ -35,8 +34,7 @@ available.
|
||||
Please ensure that the `callback` handler matches the authorization callback
|
||||
URL in the configured OAuth application.
|
||||
|
||||
```ts
|
||||
// routes/signin.ts
|
||||
```ts routes/signin.ts
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { signIn } from "https://deno.land/x/deno_kv_oauth@v0.2.4/mod.ts";
|
||||
import { oauth2Client } from "../utils/oauth2_client.ts";
|
||||
@@ -48,8 +46,7 @@ available.
|
||||
};
|
||||
```
|
||||
|
||||
```ts
|
||||
// routes/signout.ts
|
||||
```ts routes/signout.ts
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { signOut } from "https://deno.land/x/deno_kv_oauth@v0.2.4/mod.ts";
|
||||
|
||||
@@ -60,8 +57,7 @@ available.
|
||||
};
|
||||
```
|
||||
|
||||
```ts
|
||||
// routes/callback.ts
|
||||
```ts routes/callback.ts
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { handleCallback } from "https://deno.land/x/deno_kv_oauth@v0.2.4/mod.ts";
|
||||
import { oauth2Client } from "../utils/oauth2_client.ts";
|
||||
@@ -77,8 +73,7 @@ available.
|
||||
|
||||
4. Use Deno KV OAuth's helper functions where needed.
|
||||
|
||||
```tsx
|
||||
// routes/index.tsx
|
||||
```tsx routes/index.tsx
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import {
|
||||
getSessionAccessToken,
|
||||
|
||||
@@ -16,7 +16,7 @@ How can you modify your project to no longer use the current release, but
|
||||
instead this one particular commit? Just make the following changes to your
|
||||
`deno.json`:
|
||||
|
||||
```diff
|
||||
```diff deno.json
|
||||
"update": "deno run -A -r https://fresh.deno.dev/update ."
|
||||
},
|
||||
"imports": {
|
||||
@@ -34,7 +34,7 @@ worry, you can use the same approach to reference any branch in a fork as well.
|
||||
Here's an example of referencing a feature in a forked repository that hasn't
|
||||
been merged yet (at the time of writing this):
|
||||
|
||||
```diff
|
||||
```diff deno.json
|
||||
"update": "deno run -A -r https://fresh.deno.dev/update ."
|
||||
},
|
||||
"imports": {
|
||||
@@ -55,13 +55,13 @@ create a test project based on your local changes.
|
||||
|
||||
Instead of doing it like this:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
deno run -A -r https://fresh.deno.dev/
|
||||
```
|
||||
|
||||
do it like this:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
deno run -A -r path/to/fresh/init.ts
|
||||
```
|
||||
|
||||
@@ -73,6 +73,6 @@ Of course there's no reason why you have to check out the Fresh source. You can
|
||||
create a project from the latest commit by combining the techniques on this page
|
||||
like this:
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
deno run -A -r https://raw.githubusercontent.com/denoland/fresh/main/init.ts
|
||||
```
|
||||
|
||||
@@ -6,7 +6,7 @@ description: |
|
||||
When you initialize a project with `deno run -A -r https://fresh.deno.dev`,
|
||||
you'll end up with a `main.ts` like the following:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
@@ -26,7 +26,7 @@ The template generates a Twind v0 project by default. If you want to use Twind
|
||||
v1 you can follow this guide. First of all, change the import path to use the
|
||||
`twindv1` plugin:
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
/// <reference no-default-lib="true" />
|
||||
/// <reference lib="dom" />
|
||||
/// <reference lib="dom.iterable" />
|
||||
@@ -45,7 +45,7 @@ await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
||||
The twind config object has changed significantly in v1, so we must also go
|
||||
change `twind.config.ts`. A good base looks like this:
|
||||
|
||||
```ts
|
||||
```ts twind.config.ts
|
||||
import { defineConfig, Preset } from "https://esm.sh/@twind/core@1.1.3";
|
||||
import presetTailwind from "https://esm.sh/@twind/preset-tailwind@1.1.4";
|
||||
import presetAutoprefix from "https://esm.sh/@twind/preset-autoprefix@1.0.7";
|
||||
|
||||
@@ -9,9 +9,7 @@ through
|
||||
|
||||
## 1. Create your routes
|
||||
|
||||
```tsx
|
||||
// routes/index.tsx
|
||||
|
||||
```tsx routes/index.tsx
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
@@ -28,31 +26,19 @@ export const handler: Handlers = {
|
||||
};
|
||||
|
||||
export default function HomePage() {
|
||||
return (
|
||||
<div>
|
||||
Hello Deno!
|
||||
</div>
|
||||
);
|
||||
return <div>Hello Deno!</div>;
|
||||
}
|
||||
```
|
||||
|
||||
```tsx
|
||||
// routes/foo.tsx
|
||||
|
||||
```tsx routes/foo.tsx
|
||||
export default function FooPage() {
|
||||
return (
|
||||
<div>
|
||||
Hello Foo!
|
||||
</div>
|
||||
);
|
||||
return <div>Hello Foo!</div>;
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Write your tests
|
||||
|
||||
```ts
|
||||
// tests/main_test.ts
|
||||
|
||||
```ts tests/main_test.ts
|
||||
import { createHandler, ServeHandlerInfo } from "$fresh/server.ts";
|
||||
import manifest from "../fresh.gen.ts";
|
||||
import { assert, assertEquals } from "$std/testing/asserts.ts";
|
||||
@@ -81,10 +67,7 @@ Deno.test("HTTP assert test.", async (t) => {
|
||||
});
|
||||
|
||||
await t.step("#3 GET /foo", async () => {
|
||||
const resp = await handler(
|
||||
new Request("http://127.0.0.1/foo"),
|
||||
CONN_INFO,
|
||||
);
|
||||
const resp = await handler(new Request("http://127.0.0.1/foo"), CONN_INFO);
|
||||
const text = await resp.text();
|
||||
assert(text.includes("<div>Hello Foo!</div>"));
|
||||
});
|
||||
@@ -93,7 +76,7 @@ Deno.test("HTTP assert test.", async (t) => {
|
||||
|
||||
## 3. Run the tests
|
||||
|
||||
```sh
|
||||
```sh Terminal
|
||||
$ deno test --allow-read --allow-env --allow-net
|
||||
running 1 test from ./tests/main_test.ts
|
||||
HTTP assert test. ...
|
||||
|
||||
Reference in New Issue
Block a user