Skip to content

Commit 23e2c58

Browse files
authored
docs: improve doc (#385)
* doc(vite_tabs): add tabs for js/ts selection * doc(vite_tabs): add more js/ts tabs, unified order (ts first), added filename as comments in code pieces
1 parent 826bf35 commit 23e2c58

File tree

9 files changed

+306
-155
lines changed

9 files changed

+306
-155
lines changed

packages/docs/api-reference/functions/notFound.md

+36-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ The `notFound` function allows you to render the [`404 page`](#404-page) within
1212

1313
Invoking the `notFound()` function throws a `NotFoundError` error and terminates rendering of the route segment in which it was thrown.
1414

15-
```jsx filename="src/pages/user/[id].tsx"
15+
:::tabs key:language
16+
==js
17+
18+
```js
19+
//src/pages/user/[id].jsx
1620
import type { RequestContext } from "brisa";
1721
import { notFound } from "brisa";
1822

@@ -22,7 +26,7 @@ async function fetchUser(id) {
2226
return res.json();
2327
}
2428

25-
export default async function UserProfile({}, req: RequestContext) {
29+
export default async function UserProfile({}, req) {
2630
const user = await fetchUser(req.route.params.id);
2731

2832
if (!user) {
@@ -33,17 +37,45 @@ export default async function UserProfile({}, req: RequestContext) {
3337
}
3438
```
3539

40+
==ts
41+
42+
```ts
43+
//src/pages/user/[id].tsx
44+
import type { RequestContext } from "brisa";
45+
import { notFound } from "brisa";
46+
47+
type UserType = {
48+
//...
49+
}
50+
51+
async function fetchUser(id: number | string) {
52+
const res = await fetch("https://...");
53+
if (!res.ok) return undefined;
54+
return res.json();
55+
}
56+
57+
export default async function UserProfile({}, req: RequestContext) {
58+
const user: UserType = await fetchUser(req.route.params.id);
59+
60+
if (!user) {
61+
notFound();
62+
}
63+
64+
// ...
65+
}
66+
```
67+
3668
Useful to control response status during streaming:
3769

3870
- **Before response streaming** (`middleware`, `responseHeaders`): It's returning the response with 404 status and the 404 page
3971
- **During response streaming** (`layout`, `page`, `components`): Adds the `meta` tag with `noindex`, stop rendering the page and sends a client script to replace the page to the 404 page. This redirect is for UX to display the 404 content, here the bots will no longer see that because it has the noindex. However, this soft redirect that is done on the client does not change the browsing history and does receive the 404 status. The browsers normally cache very well the pages that return status 404.
4072
- **During a [server action](/building-your-application/data-management/server-actions):** (server events captured with actions): as the rendering has already been done and it is a post-render action, the 404 in an action acts similarly as in the middle of the streaming. The same happens if in the action instead of calling `notFound()` directly you do a rerender and the component calls `notFound()`.
4173

42-
#### Parameters:
74+
#### Parameters
4375

4476
- `void`. _It does not support parameters._
4577

46-
#### Returns:
78+
#### Returns
4779

4880
- `Never` does not require you to use `return notFound()` due to using the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
4981

packages/docs/building-your-application/building/index.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ Running `brisa build` generates an optimized version of your application for pro
2020

2121
Brisa can be deployed to any hosting provider that supports Bun. Ensure your `package.json` has the `"build"` and `"start"` scripts:
2222

23-
```json filename="package.json"
23+
:::tabs key:language
24+
==package.json
25+
26+
```json
2427
{
2528
"scripts": {
2629
"dev": "brisa dev",
@@ -30,6 +33,8 @@ Brisa can be deployed to any hosting provider that supports Bun. Ensure your `pa
3033
}
3134
```
3235

36+
:::
37+
3338
Then, run `bun run build` to build your application. Finally, run `bun run start` to start the Bun server. This server supports all Brisa features.
3439

3540
## App Strategy (Static, Server, Desktop, Android, iOS)

packages/docs/building-your-application/components-details/web-components.md

+24-5
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,11 @@ Within the web components you can use other web components by writing them as if
861861
862862
Example consuming a Web Component inside a Web Component:
863863
864-
```tsx filename="src/web-components/using-web-component.tsx" switcher
864+
:::tabs key:language
865+
== ts
866+
867+
```tsx
868+
//src/web-components/using-web-component.tsx
865869
import { WebContext } from "brisa";
866870

867871
type Props = { name: string };
@@ -879,7 +883,10 @@ export function ServerComponent(
879883
}
880884
```
881885
882-
```js filename="src/web-components/using-web-component.js" switcher
886+
==js
887+
888+
```jsx
889+
//src/web-components/using-web-component.js
883890
import { WebContext } from "brisa";
884891

885892
export function ServerComponent({ name, children }, webContext) {
@@ -892,13 +899,19 @@ export function ServerComponent({ name, children }, webContext) {
892899
}
893900
```
894901
902+
:::
903+
895904
## Using Web Components in Server Components
896905
897906
We are not going to use any import, we can consume it directly as another HTML tag.
898907
899908
Example consuming a Web Component inside a Server Component:
900909
901-
```tsx filename="src/components/using-web-component.tsx" switcher
910+
:::tabs key:language
911+
== ts
912+
913+
```tsx
914+
//src/components/using-web-component.tsx
902915
import { RequestContext } from "brisa";
903916
import AnotherServerComponent from "./another";
904917

@@ -919,7 +932,10 @@ export function ServerComponent(
919932
}
920933
```
921934
922-
```js filename="src/components/using-web-component.js" switcher
935+
== js
936+
937+
```jsx
938+
//src/components/using-web-component.js
923939
import AnotherServerComponent from "./another";
924940

925941
export function ServerComponent({ name }, requestContext) {
@@ -934,6 +950,8 @@ export function ServerComponent({ name }, requestContext) {
934950
}
935951
```
936952
953+
:::
954+
937955
## Using Server Components in Web Components
938956
939957
It is not possible to use Server Components inside Web Components **directly** (with an import). However, it **is possible** to add Server Components within Web Components. But it can only be done through the prop [**children**](#children) or using [**slots**](#children).
@@ -1108,9 +1126,10 @@ All native web components can be located inside `web-components/_native` and con
11081126
Example:
11091127
11101128
:::tabs key:language
1111-
==web-components/\_native/some-native.ts
1129+
==ts
11121130
11131131
```tsx
1132+
//web-components/\_native/some-native.ts
11141133
export default class SomeNative extends HTMLElement {
11151134
constructor() {
11161135
super();

packages/docs/building-your-application/configuring/base-path.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ To deploy a Brisa application under a sub-path of a domain you can use the `base
99
`basePath` allows you to set a path prefix for the application. For example, to use `/docs` instead of `''` (an empty string, the default), open `brisa.config.ts` and add the `basePath` config:
1010

1111
:::tabs key:language
12-
==brisa.config.ts
12+
==ts
1313

1414
```ts
15+
//brisa.config.ts
1516
import type { Configuration } from "brisa";
1617

1718
export default {

packages/docs/building-your-application/routing/api-routes.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,38 @@ The request that arrives is an extension of the native [Request](https://develop
5353

5454
You can read the `Request` body using the standard Web API methods:
5555

56-
```ts filename="src/api/items/route.ts" switcher
56+
:::tabs key:language
57+
==ts
58+
59+
```ts
60+
//src/api/items/route.ts
5761
export async function POST(request: RequestContext) {
5862
const res = await request.json();
5963
return new Response(JSON.stringify({ res }));
6064
}
6165
```
6266

63-
```js filename="src/api/items/route.js" switcher
67+
==js
68+
69+
```js
70+
//src/api/items/route.js
6471
export async function POST(request) {
6572
const res = await request.json();
6673
return new Response(JSON.stringify({ res }));
6774
}
6875
```
6976

77+
:::
78+
7079
## Request Body FormData
7180

7281
You can read the `FormData` using the standard Web API methods:
7382

74-
```ts filename="src/api/items/route.ts" switcher
83+
:::tabs key:language
84+
==ts
85+
86+
```ts
87+
//src/api/items/route.ts
7588
export async function POST(request: RequestContext) {
7689
const formData = await request.formData();
7790
const name = formData.get("name");
@@ -80,7 +93,10 @@ export async function POST(request: RequestContext) {
8093
}
8194
```
8295

83-
```js filename="src/api/items/route.js" switcher
96+
==js
97+
98+
```js
99+
//src/api/items/route.js
84100
export async function POST(request) {
85101
const formData = await request.formData();
86102
const name = formData.get("name");
@@ -89,6 +105,8 @@ export async function POST(request) {
89105
}
90106
```
91107

108+
:::
109+
92110
Since `formData` data are all strings, you may want to use [`zod-form-data`](https://www.npmjs.com/zod-form-data) to validate the request and retrieve data in the format you prefer (e.g. `number`).
93111

94112
## Response

0 commit comments

Comments
 (0)