fix(docs): improve and increase consistency for code blocks (#3163)
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" />
This commit is contained in:
@@ -7,7 +7,7 @@ The `App` class is the heart of Fresh and routes incoming requests to the
|
||||
correct middlewares. This is where routes, middlewares, layouts and more are
|
||||
defined.
|
||||
|
||||
```tsx
|
||||
```tsx main.ts
|
||||
const app = new App()
|
||||
.use(staticFiles())
|
||||
.get("/", () => new Response("hello"))
|
||||
@@ -20,7 +20,7 @@ app.listen();
|
||||
All items are applied from top to bottom. This means that when you defined a
|
||||
middleware _after_ a `.get()` handler, it won't be included.
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
const app = new App()
|
||||
.use((ctx) => {
|
||||
// Will be called for all middlewares
|
||||
|
||||
@@ -24,7 +24,7 @@ if (Deno.args.includes("build")) {
|
||||
|
||||
You can customize the builder by passing options.
|
||||
|
||||
```ts
|
||||
```ts dev.ts
|
||||
const builder = new Builder({
|
||||
// Browser target for generated code. Maps to https://esbuild.github.io/api/#target
|
||||
target?: string | string[];
|
||||
@@ -57,7 +57,7 @@ const builder = new Builder({
|
||||
The builder is where you'll register files that contain islands. This is the
|
||||
same API that Fresh uses internally.
|
||||
|
||||
```ts
|
||||
```ts dev.ts
|
||||
const builder = new Builder();
|
||||
|
||||
// Path to local island
|
||||
@@ -72,7 +72,7 @@ builder.registerIsland("jsr:@marvinh-test/fresh-island");
|
||||
|
||||
The `Builder` has a very simple processing mechanism for static files.
|
||||
|
||||
```ts
|
||||
```ts dev.ts
|
||||
builder.onTransformStaticFile({
|
||||
pluginName: "My cool plugin",
|
||||
filter: /\.css$/,
|
||||
|
||||
@@ -36,7 +36,8 @@ const app = new App({ root: import.meta.url })
|
||||
|
||||
Example project structure:
|
||||
|
||||
```sh Project structure
|
||||
```txt-files Project structure
|
||||
<project root>
|
||||
├── deno.json
|
||||
├── main.ts
|
||||
├── dev.ts
|
||||
@@ -104,7 +105,7 @@ suggested by the URL segment.
|
||||
|
||||
Let's illustrate that with an example:
|
||||
|
||||
```txt
|
||||
```txt Example page layout
|
||||
/about -> layout A
|
||||
/career -> layout A
|
||||
/archive -> layout B
|
||||
@@ -114,8 +115,8 @@ Let's illustrate that with an example:
|
||||
Without any way to group routes this is a problem because every route segment
|
||||
can only have one `_layout` file.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── _layout.tsx # applies to all routes here :(
|
||||
├── about.tsx
|
||||
├── career.tsx
|
||||
@@ -128,8 +129,8 @@ a name that is wrapped in parentheses. For example `(info)` would be considered
|
||||
a route group and so would `(marketing)`. This enables us to group related
|
||||
routes in a folder and use a different `_layout` file for each group.
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── (marketing)
|
||||
│ ├── _layout.tsx # only applies to about.tsx and career.tsx
|
||||
│ ├── about.tsx
|
||||
@@ -144,8 +145,8 @@ routes in a folder and use a different `_layout` file for each group.
|
||||
> URL. Such scenarios will lead to ambiguity as to which route file should be
|
||||
> picked.
|
||||
>
|
||||
> ```txt Project structure
|
||||
> └── routes
|
||||
> ```txt-files Project structure
|
||||
> └── <root>/routes
|
||||
> ├── (group-1)
|
||||
> │ └── about.tsx # Bad: Maps to same `/about` url
|
||||
> └── (group-2)
|
||||
|
||||
@@ -28,7 +28,7 @@ export default function MyIsland() {
|
||||
An island can be used anywhere like a regular Preact component. Fresh will take
|
||||
care of making it interactive on the client.
|
||||
|
||||
```tsx
|
||||
```tsx main.tsx
|
||||
import MyIsland from "../islands/my-island.tsx";
|
||||
|
||||
const app = new App()
|
||||
@@ -56,9 +56,11 @@ deserialization.
|
||||
|
||||
> [warn]: Passing functions to an island is not supported.
|
||||
>
|
||||
> ```tsx
|
||||
> // WRONG
|
||||
> <MyIsland onClick={() => console.log("hey")} />;
|
||||
> ```tsx routes/example.tsx
|
||||
> export default function () {
|
||||
> // WRONG
|
||||
> return <MyIsland onClick={() => console.log("hey")} />;
|
||||
> }
|
||||
> ```
|
||||
|
||||
### Passing JSX
|
||||
|
||||
@@ -7,7 +7,8 @@ A layout is defined in a `_layout.tsx` file in any sub directory (at any level)
|
||||
under the `routes/` folder. It must contain a default export that is a regular
|
||||
Preact component. Only one such layout is allowed per sub directory.
|
||||
|
||||
```txt Project structure
|
||||
```txt-files Project structure
|
||||
<project root>
|
||||
└── routes
|
||||
├── sub
|
||||
│ ├── page.tsx
|
||||
@@ -85,8 +86,8 @@ Sometimes you want to opt out of the layout inheritance mechanism for a
|
||||
particular route. This can be done via route configuration. Picture a directory
|
||||
structure like this:
|
||||
|
||||
```txt Project structure
|
||||
└── routes
|
||||
```txt-files Project structure
|
||||
└── <root>/routes
|
||||
├── sub
|
||||
│ ├── _layout_.tsx
|
||||
│ ├── special.tsx # should not inherit layouts
|
||||
|
||||
@@ -11,7 +11,7 @@ returns a
|
||||
are typically used to set HTTP Headers, measure response times or fetch data and
|
||||
pass it to another middleware.
|
||||
|
||||
```tsx
|
||||
```tsx main.ts
|
||||
const app = new App<{ greeting: string }>()
|
||||
.use((ctx) => {
|
||||
// Middleware to pass data
|
||||
@@ -38,7 +38,7 @@ excellent way to make http-related logic reusable on the server.
|
||||
|
||||
Use the `define.middleware()` helper to get typings out of the box:
|
||||
|
||||
```ts
|
||||
```ts middleware/my-middleware.ts
|
||||
import { define } from "./utils.ts";
|
||||
|
||||
const middleware = define.middleware(async (ctx) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ description: |
|
||||
Routing defines which middlewares and routes should respond to a particular
|
||||
request.
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
const app = new App()
|
||||
.get("/", () => new Response("hello")) // Responds to: GET /
|
||||
.get("/other", () => new Response("other")) // Responds to: GET /other
|
||||
|
||||
@@ -9,7 +9,7 @@ disk for optimal performance with
|
||||
[`ETag`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag)
|
||||
headers.
|
||||
|
||||
```ts
|
||||
```ts main.ts
|
||||
import { staticFiles } from "fresh";
|
||||
|
||||
const app = new App()
|
||||
@@ -30,17 +30,20 @@ const builder = new Builder({ staticDir: "path/to/staticDir" });
|
||||
By default, Fresh adds caching headers for the `src` and `srcset` attributes on
|
||||
`<img>` and `<source>` tags.
|
||||
|
||||
```tsx
|
||||
```tsx main.tsx
|
||||
// Caching headers will be automatically added
|
||||
<img src="/user.png" />;
|
||||
app.get("/user", (ctx) => ctx.render(<img src="/user.png" />));
|
||||
```
|
||||
|
||||
You can always opt out of this behaviour per tag, by adding the
|
||||
`data-fresh-disable-lock` attribute.
|
||||
|
||||
```tsx
|
||||
```tsx main.tsx
|
||||
// Opt-out of automatic caching headers
|
||||
<img src="/user.png" data-fresh-disable-lock />;
|
||||
app.get(
|
||||
"/user",
|
||||
(ctx) => ctx.render(<img src="/user.png" data-fresh-disable-lock />),
|
||||
);
|
||||
```
|
||||
|
||||
## Adding caching headers manually
|
||||
@@ -48,9 +51,11 @@ You can always opt out of this behaviour per tag, by adding the
|
||||
Use the `asset()` function to add caching headers manually. It will be served
|
||||
with a cache lifetime of one year.
|
||||
|
||||
```tsx
|
||||
```tsx routes/about.tsx
|
||||
import { asset } from "fresh/runtime";
|
||||
|
||||
// Adding caching headers manually
|
||||
<a href={asset("/brochure.pdf")}>View brochure</a>;
|
||||
export default function About() {
|
||||
// Adding caching headers manually
|
||||
return <a href={asset("/brochure.pdf")}>View brochure</a>;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user