Preact-cli: SERVE SSR BUILD

Created on 9 Jan 2020  路  6Comments  路  Source: preactjs/preact-cli

Need some more instructions on how to use ssr build for server side rendering

Most helpful comment

Docs always help!

Not sure if you're asking _me_ for docs about what's happening there, but just in case:

1) We make use of the url prop from preact-router, since it allows us to _pass in_ a specific URL to match: https://github.com/lukeed/preact-cli-ssr/blob/master/src/components/app.js#L23
2) We build the project, as normal: preact build
3) We create a new server.js file which loads the _built_ SSR bundle from step 1: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L8-L10
4) We require & keep a skeleton HTML file in memory, which has the correct asset paths (etc) for SSR usage: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L15
5) On every request (for this example) we use preact-render-to-string to render our App, while ensuring to pass in the url prop. This tells App what we're requesting AND so we're given the output for that page: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L26
6) We take that SSR output and inject it into the HTML template we loaded (from step 4) and send it as the response: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L28


Hope that helps! 馃槃

All 6 comments

// @developit @ForsakenHarmony how about just add a page about this on the documentation site?

Is it not goof to add some more clarity in documentation @lukeed ?

No I don't think there is a problem in adding a documentation. We would love a page about it

Docs always help!

Not sure if you're asking _me_ for docs about what's happening there, but just in case:

1) We make use of the url prop from preact-router, since it allows us to _pass in_ a specific URL to match: https://github.com/lukeed/preact-cli-ssr/blob/master/src/components/app.js#L23
2) We build the project, as normal: preact build
3) We create a new server.js file which loads the _built_ SSR bundle from step 1: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L8-L10
4) We require & keep a skeleton HTML file in memory, which has the correct asset paths (etc) for SSR usage: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L15
5) On every request (for this example) we use preact-render-to-string to render our App, while ensuring to pass in the url prop. This tells App what we're requesting AND so we're given the output for that page: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L26
6) We take that SSR output and inject it into the HTML template we loaded (from step 4) and send it as the response: https://github.com/lukeed/preact-cli-ssr/blob/master/server.js#L28


Hope that helps! 馃槃

Thank you @lukeed for the help!

Was this page helpful?
0 / 5 - 0 ratings