1
1
import { Elysia , t } from 'elysia'
2
2
import { html } from '../src'
3
+ import { Suspense } from '@kitajs/html/suspense'
3
4
4
5
function page ( { name } : { name : string } ) : string {
5
6
return `
6
- <html lang="en">
7
+ <html lang="en" style="background-color: black; color: white;" >
7
8
<head>
8
9
<title>Hello ${ name } !</title>
9
10
</head>
@@ -15,33 +16,52 @@ function page({ name }: { name: string }): string {
15
16
}
16
17
17
18
// https://elysiajs.com/plugins/html.html#jsx
18
- function tsxPage ( { name } : { name : string } ) : string {
19
+ function tsxPage ( { name } : { name : string } ) : JSX . Element {
19
20
return (
20
- < html lang = "en" >
21
+ < html lang = "en" style = { { backgroundColor : 'black' , color : 'white' } } >
21
22
< head >
22
- < title > Hello $ { name } !</ title >
23
+ < title > Hello { name } !</ title >
23
24
</ head >
24
25
< body >
25
- < h1 > Hello $ { name } !</ h1 >
26
+ < h1 > Hello { name } !</ h1 >
26
27
</ body >
27
28
</ html >
28
29
)
29
30
}
30
31
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
+ }
38
37
38
+ function asyncPage ( rid : number , { name } : { name : string } ) : JSX . Element {
39
39
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 >
46
51
)
47
52
}
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