Simple example of how to do streaming server-rendering. You will not believe how easy it is!
Using:
- parcel-bundler
- react
- react-router-dom
- react-helmet-async (SEO)
- react-imported-component (Code Splitting)
- styled-components (Streaming CSS-in-JS)
Handy Extras
- gzip compression
- eslint
- favicon example
- Code split example
- Relative Typography scale based on em's
- CSS Reset
- "system" font-family
- differential bundling - automatically serve modern bundles to modern browsers
npm run dev
- Run the development server with hot reload but no server-renderingnpm run dev-server
- Run the server with nodemon to rebuild with every changenpm run build
- Build for production, required before runningnpm start
- Start the production server
To do proper server-rendering, the code is bundled in two version: one for the browser and one for Node.js.
The browser version is in the dist/client
folder and the Node.js version is in dist/server
. However, they both share the same public path for their files: /dist
and it points to the browser version. Go read the code, it's pretty straightforward!
- package.json - Start by reading the
scripts
section to understand how the build process works. Yes it's that dead simple! - app/index.html - Your client-side HTML file (for development)
- server/template.js - Your server-side HTML file template (for server rendering)
- app/client.js - Entry point for your browser version
- app/App.jsx - Your main application component shared between your browser and Node.js version
- app/pages/HelloWorld.jsx - Dead simple hello world
- app/pages/HelloWorld2.jsx - A fun code split example
- app/codeSplitAssets/NyanCat.scss - Example of SCSS
- app/styles.js - Global Styles for styled-components - renders in stream - fast! Don't overuse - only put critical styles here!
- server/index.js - Entry point for your Node.js version
- server/ssr.js - Middleware taking care of server-side-rendering
- server/pageData.js - Gets generated bundle links from
dist/index.html
as the template with cheerio. Exports a singleton to as an optimization.