Skip to content
This repository was archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
feat: allow postProcess to return a promise (#243)
Browse files Browse the repository at this point in the history
* feat: allow postProcess to return a promise

* docs: add async postProcess example

* refactor: undo changes to postProcessHtml
  • Loading branch information
isidrok authored and JoshTheDerf committed Aug 31, 2018
1 parent a501698 commit 30dfeaa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ In the interest of transparency, there are some use-cases where prerendering mig
| staticDir | String | Yes | None | The root path to serve your app from. |
| ouputDir | String | No | None | Where the prerendered pages should be output. If not set, defaults to staticDir. |
| indexPath | String | No | `staticDir/index.html` | The index file to fall back on for SPAs. |
| postProcess | Function(Object context): Object | No | None | See the [Using the postProcess Option](#using-the-postprocess-option) section. |
| postProcess | Function(Object context): [Object \| Promise] | No | None | See the [Using the postProcess Option](#using-the-postprocess-option) section. |
| minify | Object | No | None | Minifies the resulting HTML using [html-minifier](https://github.com/kangax/html-minifier). Full list of options available [here](https://github.com/kangax/html-minifier#options-quick-reference). |
| server | Object | No | None | App server configuration options (See below) |
| renderer | Renderer Instance or Configuration Object | No | `new PuppeteerRenderer()` | The renderer you'd like to use to prerender the app. It's recommended that you specify this, but if not it will default to `@prerenderer/renderer-puppeteer`. |
Expand All @@ -296,7 +296,7 @@ In the interest of transparency, there are some use-cases where prerendering mig

#### Using The postProcess Option

The `postProcess(Object context): Object` function in your renderer configuration allows you to adjust the output of `prerender-spa-plugin` before writing it to a file. It is called once per rendered route and is passed a `context` object in the form of:
The `postProcess(Object context): Object | Promise` function in your renderer configuration allows you to adjust the output of `prerender-spa-plugin` before writing it to a file. It is called once per rendered route and is passed a `context` object in the form of:

```javascript
{
Expand All @@ -315,7 +315,7 @@ The `postProcess(Object context): Object` function in your renderer configuratio

You can modify `context.html` to change what gets written to the prerendered files and/or modify `context.route` or `context.outputPath` to change the output location.

You are expected to adjust those properties as needed, then return the context object, like so:
You are expected to adjust those properties as needed, then return the context object, or a promise that resolves to it like so:

```javascript
postProcess(context) {
Expand All @@ -327,6 +327,14 @@ postProcess(context) {

return context
}

postProcess(context) {
return someAsyncProcessing(context.html)
.then((html) => {
context.html = html;
return context;
});
}
```

---
Expand Down
4 changes: 2 additions & 2 deletions es5-autogenerated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ PrerenderSPAPlugin.prototype.apply = function (compiler) {
})
// Run postProcess hooks.
.then(function (renderedRoutes) {
return _this2._options.postProcess ? renderedRoutes.map(function (renderedRoute) {
return _this2._options.postProcess ? Promise.all(renderedRoutes.map(function (renderedRoute) {
return _this2._options.postProcess(renderedRoute);
}) : renderedRoutes;
})) : renderedRoutes;
})
// Check to ensure postProcess hooks returned the renderedRoute object properly.
.then(function (renderedRoutes) {
Expand Down
2 changes: 1 addition & 1 deletion es6/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ PrerenderSPAPlugin.prototype.apply = function (compiler) {
)
// Run postProcess hooks.
.then(renderedRoutes => this._options.postProcess
? renderedRoutes.map(renderedRoute => this._options.postProcess(renderedRoute))
? Promise.all(renderedRoutes.map(renderedRoute => this._options.postProcess(renderedRoute)))
: renderedRoutes
)
// Check to ensure postProcess hooks returned the renderedRoute object properly.
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30dfeaa

Please sign in to comment.