-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ssr_server.tsx
70 lines (67 loc) · 2.71 KB
/
ssr_server.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import App from "../pages/app.tsx";
import Hello from "../pages/hello.tsx";
import Register from "../pages/register.tsx";
import User from "../pages/user.tsx";
import fastro, { render } from "../server/mod.ts";
// define a hello component to render
// attach this to page declaration
const hello = render(<Hello />);
const app = render(<App />);
const user = render(<User />);
const register = render(<Register />);
const f = fastro()
// set static endpoint url
// default folder is at ./public
// this is a place where hydrated files generated
.static("/static")
// set endpoint for app page
.page("/", hello, (req, res) => {
// you can access the http request object
// console.log(req.url);
return res.ssr(hello)
// add css
.link(
`rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"`,
)
// you can inject react props from server
.render();
})
.page("/:user", user, (req, res) => {
// console.log(req.match);
// you can access the http request object
const u = req.match?.pathname.groups.user;
return res.ssr(user)
// you can inject react props from server
.props({ data: u })
.link(
`rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"`,
)
.render();
})
// set endpoint for hello page
.page("/:user/count", app, (req, res) => {
const user = req.match?.pathname.groups.user;
return res.ssr(app)
// and set the html title and other meta data
// for seo purpose
.title(`Hello ${user}`)
.link(
`rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"`,
)
.props({ data: user })
.meta(`name="viewport" content="width=device-width"`)
.render();
})
.page("/app/register", register, (req, res) => {
return res.ssr(register)
// and set the html title and other meta data
// for seo purpose
.title(`Register`)
.link(
`href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous"`,
)
.link(`/static/app.css" rel="stylesheet"`)
.meta(`name="viewport" content="width=device-width"`)
.render();
});
await f.serve();