Skip to content

Commit b03cb36

Browse files
docs: updated example to cover more cases
1 parent 67b7616 commit b03cb36

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

example/index.tsx

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Elysia, t } from 'elysia'
22
import { html } from '../src'
3+
import { Suspense } from '@kitajs/html/suspense'
34

45
function page({ name }: { name: string }): string {
56
return `
6-
<html lang="en">
7+
<html lang="en" style="background-color: black; color: white;">
78
<head>
89
<title>Hello ${name}!</title>
910
</head>
@@ -15,33 +16,52 @@ function page({ name }: { name: string }): string {
1516
}
1617

1718
// https://elysiajs.com/plugins/html.html#jsx
18-
function tsxPage({ name }: { name: string }): string {
19+
function tsxPage({ name }: { name: string }): JSX.Element {
1920
return (
20-
<html lang="en">
21+
<html lang="en" style={{ backgroundColor: 'black', color: 'white' }}>
2122
<head>
22-
<title>Hello ${name}!</title>
23+
<title>Hello {name}!</title>
2324
</head>
2425
<body>
25-
<h1>Hello ${name}!</h1>
26+
<h1>Hello {name}!</h1>
2627
</body>
2728
</html>
2829
)
2930
}
3031

31-
export function createApp() {
32-
// https://xelysiajs.com/concept/schema.html
33-
const indexSchema = {
34-
params: t.Object({
35-
name: t.String({ default: 'World' })
36-
})
37-
}
32+
async function FakeDatabase({ name }: { name: string }) {
33+
// Sleeps 1 second
34+
await new Promise((resolve) => setTimeout(resolve, 1000))
35+
return <h1>Hello {name}!</h1>
36+
}
3837

38+
function asyncPage(rid: number, { name }: { name: string }): JSX.Element {
3939
return (
40-
new Elysia()
41-
// https://elysiajs.com/plugins/html.html#options
42-
.use(html())
43-
.get('/', ({ params }) => page(params), indexSchema)
44-
.get('/tsx', ({ params }) => tsxPage(params), indexSchema)
45-
.listen(8080)
40+
<html lang="en" style={{ backgroundColor: 'black', color: 'white' }}>
41+
<head>
42+
<title>Hello {name}!</title>
43+
</head>
44+
<body>
45+
{/* https://github.com/kitajs/html#suspense-component */}
46+
<Suspense rid={rid} fallback={<h1>Loading...</h1>}>
47+
<FakeDatabase name={name} />
48+
</Suspense>
49+
</body>
50+
</html>
4651
)
4752
}
53+
54+
// https://xelysiajs.com/concept/schema.html
55+
const indexSchema = {
56+
query: t.Object({
57+
name: t.String({ default: 'World' })
58+
})
59+
}
60+
61+
new Elysia()
62+
// https://elysiajs.com/plugins/html.html#options
63+
.use(html())
64+
.get('/', ({ query }) => page(query), indexSchema)
65+
.get('/tsx', ({ query }) => tsxPage(query), indexSchema)
66+
.get('/async', ({ query, html }) => html(asyncPage, query), indexSchema)
67+
.listen(8080, () => console.log('Listening on http://localhost:8080'))

0 commit comments

Comments
 (0)