60frames / webpack-hot-server-middleware

:fire: Hot reload webpack bundles on the server
MIT License
324 stars 50 forks source link

url driven serverRendererOptions data? #54

Closed valorloff closed 6 years ago

valorloff commented 6 years ago

Hi! whether it is possible connect serverRendererOptions with fetched data?

app.use('/urlPath/:id',webpackHotServerMiddleware(compiler, {
    serverRendererOptions: {
        data: fetchedData // how to?
    }
}));

like this:

app.get('/path/:id', (req, res) => {
    firebaseDatabase.getData(req.params.id).then(resp => {
res.send(resp)
......
richardscarrott commented 6 years ago

It looks to me like you want to share a DB connection between the serverRenderer middleware and the main node app?

Would passing the actual database connection into the middleware work for you, e.g.

// dev-server.js
app.use('/urlPath/:id',webpackHotServerMiddleware(compiler, {
    serverRendererOptions: {
        firebaseDatabase: firebaseDatabase
    }
}));

Then in your server renderer you could do:

// server-renderer.js
module.exports = ({ firebaseDatabase }) => (req, res) => firebaseDatabase.getData(req.params.id).then((resp) => res.send(resp));
valorloff commented 6 years ago

Yes! you guessed, thanks!