Skip to content

Commit 188036b

Browse files
committed
docs(readme): clarify use of reply.viewAsync
1 parent 1e57bbd commit 188036b

File tree

1 file changed

+18
-79
lines changed

1 file changed

+18
-79
lines changed

README.md

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ _Note that at least Fastify `v2.0.0` is needed._
2828

2929
## Recent Changes
3030

31-
_Note: `reply.viewAsync` added as a replacement for `reply.view` and `fastify.view`. See [Migrating from view to viewAsync](#migrating-from-view-to-viewAsync)._
32-
3331
_Note: [`ejs-mate`](https://github.com/JacksonTian/ejs-mate) support [has been dropped](https://github.com/fastify/point-of-view/pull/157)._
3432

3533
_Note: [`marko`](https://markojs.com/) support has been dropped. Please use [`@marko/fastify`](https://github.com/marko-js/fastify) instead._
@@ -88,7 +86,8 @@ fastify.get("/", (req, reply) => {
8886

8987
// asynchronous handler:
9088
fastify.get("/", async (req, reply) => {
91-
return reply.viewAsync("index.ejs", { name: "User" });
89+
// Note the return statement
90+
return reply.view("index.ejs", { name: "User" });
9291
})
9392

9493
fastify.listen({ port: 3000 }, (err) => {
@@ -218,8 +217,23 @@ fastify.view("/templates/index.ejs", { text: "text" }, (err, html) => {
218217
});
219218
```
220219

221-
If called within a request hook and you need request-global variables, see [Migrating from view to viewAsync](#migrating-from-view-to-viewAsync).
220+
If called within a request hook and you need request-global variables, use `reply.viewAsync`:
222221

222+
```js
223+
// synchronous handler:
224+
fastify.get("/", (req, reply) => {
225+
reply.viewAsync("/templates/index.ejs", { text: "text" })
226+
.then((html) => reply.type("application/html").send(html))
227+
.catch((err) => reply.send(err))
228+
})
229+
230+
// asynchronous handler:
231+
fastify.get("/", async (req, reply) => {
232+
const data = await something();
233+
const html = await reply.viewAsync("/templates/index.ejs", { data });
234+
return html;
235+
})
236+
```
223237

224238
## Registering multiple engines
225239

@@ -813,81 +827,6 @@ fastify.view.clearCache();
813827

814828
<a name="note"></a>
815829

816-
### Migrating from `view` to `viewAsync`
817-
818-
The behavior of `reply.view` is to immediately send the HTML response as soon as rendering is completed, or immediately send a 500 response with error if encountered, short-circuiting fastify's error handling hooks, whereas `reply.viewAsync` returns a promise that either resolves to the rendered HTML, or rejects on any errors. `fastify.view` has no mechanism for providing request-global variables, if needed. `reply.viewAsync` can be used in both sync and async handlers.
819-
820-
#### Sync handler
821-
Previously:
822-
```js
823-
fastify.get('/', (req, reply) => {
824-
reply.view('index.ejs', { text: 'text' })
825-
})
826-
```
827-
Now:
828-
```js
829-
fastify.get('/', (req, reply) => {
830-
return reply.viewAsync('index.ejs', { text: 'text' })
831-
})
832-
```
833-
#### Async handler
834-
Previously:
835-
```js
836-
// This is an async function
837-
fastify.get("/", async (req, reply) => {
838-
const data = await something();
839-
reply.view("/templates/index.ejs", { data });
840-
return
841-
})
842-
```
843-
844-
Now:
845-
```js
846-
// This is an async function
847-
fastify.get("/", async (req, reply) => {
848-
const data = await something();
849-
return reply.viewAsync("/templates/index.ejs", { data });
850-
})
851-
```
852-
#### fastify.view (when called inside a route hook)
853-
Previously:
854-
```js
855-
// Promise based, using async/await
856-
fastify.get("/", async (req, reply) => {
857-
const html = await fastify.view("/templates/index.ejs", { text: "text" });
858-
return html
859-
})
860-
```
861-
```js
862-
// Callback based
863-
fastify.get("/", (req, reply) => {
864-
fastify.view("/templates/index.ejs", { text: "text" }, (err, html) => {
865-
if(err) {
866-
reply.send(err)
867-
}
868-
else {
869-
reply.type("application/html").send(html)
870-
}
871-
});
872-
})
873-
```
874-
Now:
875-
```js
876-
// Promise based, using async/await
877-
fastify.get("/", (req, reply) => {
878-
const html = await fastify.viewAsync("/templates/index.ejs", { text: "text" });
879-
return html
880-
})
881-
```
882-
```js
883-
fastify.get("/", (req, reply) => {
884-
fastify.viewAsync("/templates/index.ejs", { text: "text" })
885-
.then((html) => reply.type("application/html").send(html))
886-
.catch((err) => reply.send(err))
887-
});
888-
})
889-
```
890-
891830
## Note
892831

893832
By default, views are served with the mime type `text/html`, with the charset specified in options. You can specify a different `Content-Type` header using `reply.type`.

0 commit comments

Comments
 (0)