Skip to content

Commit 47caccd

Browse files
AyoubElkairhorns
authored andcommitted
update readme
1 parent 45ff8f7 commit 47caccd

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,27 @@ Note that the route which renders the component is a normal route that doesn't n
142142
To register a component, you can do the following:
143143

144144
```js
145-
server.registerRenderable(require.resolve('./ImperativelyRenderablePage'))
145+
// The return value needs to be passed down to the reply.render() function
146+
const Renderable = server.registerRenderable(require.resolve('./ImperativelyRenderablePage'))
146147
```
147148

148149
And then you can render it imperatively in your routes:
149150

151+
```js
152+
server.get('/imperative', async (request, reply) => {
153+
return reply.render(Renderable, {
154+
hostname: os.hostname(),
155+
requestIP: request.ip,
156+
})
157+
})
158+
```
159+
160+
A big reason why you might want to imperatively render routes is for conditional rendering, where you only want to render if the user has permission or if some header is correctly passed. Imperative rendering works fine for routes that only sometimes use `reply.render`, and otherwise do normal `reply.send`s:
161+
150162
```js
151163
server.get('/imperative/:bool', async (request: FastifyRequest<{ Params: { bool: string } }>, reply) => {
152164
if (request.params.bool == 'true') {
153-
return reply.render(require.resolve('./ImperativelyRenderablePage'), {
165+
return reply.render(Renderable, {
154166
hostname: os.hostname(),
155167
requestIP: request.ip,
156168
})

0 commit comments

Comments
 (0)