-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 3b: save the day by making webpack to render server-side code
To save our issue with CSS modules, we make Webpack to render both our client and our server side code. The best way to do it is to use Webpack's abillity to handle array of configs. With first config we transform our client-side code (`client.js`), just as we were doing before. But with the second config we transform `handleRender` function that we have extracted into `serverEntry.js`, so now our server-side rendering code gets processed by Webpack too. There we use css-loader/locals as a CSS loader, to just get class names from CSS modules, as that's all we need to render html on the server. Also notice how we use `target:node` and `nodeExternals`. Great! Now our build is fixed, so we can use CSS modules both during client and server rendering.
- Loading branch information
Showing
5 changed files
with
66 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import fs from 'fs'; | ||
import React from 'react'; | ||
import ReactDOMServer from 'react-dom/server'; | ||
import Hello from './Hello.js'; | ||
|
||
function handleRender(req, res) { | ||
const html = ReactDOMServer.renderToString( | ||
<Hello name="World" /> | ||
); | ||
fs.readFile('./index.html', 'utf8', function (err, file) { | ||
if (err) { | ||
return console.log(err); | ||
} | ||
const document = file.replace(/<div id="app"><\/div>/, `<div id="app">${html}</div>`); | ||
res.send(document); | ||
}); | ||
} | ||
|
||
export default handleRender; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters