Preact-cli: Custom server support

Created on 29 May 2017  路  7Comments  路  Source: preactjs/preact-cli

Instead of having a static production-like server when building for production. It would definitely be awesome to have some kind of "custom server" type of output like next.js has.

Most helpful comment

@developit I've created a PR for exposing a compiler instance, let's continue the discussion there.

All 7 comments

@robinvdvleuten I think preact-cli and next.js are 2 different beasts. You can serve the static build with any server. So no need for support from preact-cli. Just build your own :D

@thangngoc89 I am aware of that, but the preact-cli also has supports for prerendering. It would be nice if there is some sort of exposed API for this so that you still can rely on the webpack / babel configuration of this project :)

@robinvdvleuten I think the prerendering is done in build step. Check out index.html, you'll see the rendered code of your index route. No need for a server here

If we end up with a UMD or CommonJS bundle output from something like #71, you could use preact-cli as an opaque compiler within a Node server. I'm not sure it'd fit perfectly into preact-cli itself, but certainly I could see supporting something like this:

import { h } from 'preact';
import express from 'express';
import { build } from 'preact-cli';
import renderToString from 'preact-render-to-string';

const app = build('./client', { format: 'umd' }).then( output => require(output.main) );

const app = express();
app.get( (req, res) => {
  app.then( App => {
    let body = renderToString(<App url={req.path} />);
    res.send(`<!doctype html><html lang="en">${body}`);
  });
});
app.listen(process.env.PORT);

Or, maybe we expose an full API where you could trigger and handle a client build, or invoke a high level API for SSR like prerender({ url }).

@developit that's exactly the kind of interface I meant! I do not expect that preact-cli becomes some kind of prext.js, but it would be great if it provides some kind of interface / functionality to build this kind of tooling upon. The only thing the preact-cli should do extra I guess is to expose the build process. Is there any way I can help with this?

Any experimentation you have time to do would be appreciated. The API we can solidify later, but I'd love to get some experiments done around returning a reusable "preact compiler" instance.

@developit I've created a PR for exposing a compiler instance, let's continue the discussion there.

Was this page helpful?
0 / 5 - 0 ratings