Skip to content

Commit 57bcea9

Browse files
AyoubElkairhorns
authored andcommitted
add docs
1 parent 6e8debe commit 57bcea9

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,33 @@ export const DefaultDocumentTemplate: Template = (data: TemplateData<any>) => te
133133
`
134134
```
135135

136+
## Imperatively rendering a component
137+
138+
Imperative rendering allows routes to dynamically render a component based on specific conditions, instead of always rendering the same component. To do so, we still require that the component is registered to allow Vite to bundle it.
139+
140+
Note that the route which renders the component is a normal route that doesn't need any special route options configuration.
141+
142+
To register a component, you can do the following:
143+
144+
```js
145+
server.registerRenderable(require.resolve('./ImperativelyRenderablePage'))
146+
```
147+
148+
And then you can render it imperatively in your routes:
149+
150+
```js
151+
server.get('/imperative/:bool', async (request: FastifyRequest<{ Params: { bool: string } }>, reply) => {
152+
if (request.params.bool == 'true') {
153+
return reply.render(require.resolve('./ImperativelyRenderablePage'), {
154+
hostname: os.hostname(),
155+
requestIP: request.ip,
156+
})
157+
} else {
158+
return reply.redirect('/not-found')
159+
}
160+
})
161+
```
162+
136163
## How it works
137164

138165
- mounts a `vite` server as a fastify plugin that knows how to transform code to be run server side.

packages/test-apps/simple-react/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const server = async () => {
4141
return reply.redirect('/not-found')
4242
}
4343
})
44+
4445
server.get('/*', { render: require.resolve('./NotFound') }, async (request) => {
4546
return { params: request.params }
4647
})

0 commit comments

Comments
 (0)