All Projects → cullenjett → React Ssr Boilerplate

cullenjett / React Ssr Boilerplate

Boilerplate for React apps with routing, code splitting, & server side rendering

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Ssr Boilerplate

React Imported Component
✂️📦Bundler-independent solution for SSR-friendly code-splitting
Stars: ✭ 525 (+186.89%)
Mutual labels:  code-splitting, ssr, server-side-rendering
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-75.96%)
Mutual labels:  code-splitting, ssr, server-side-rendering
Loadable Components
The recommended Code Splitting library for React ✂️✨
Stars: ✭ 6,194 (+3284.7%)
Mutual labels:  code-splitting, ssr, server-side-rendering
React Loadable
⏳ A higher order component for loading components with promises.
Stars: ✭ 16,238 (+8773.22%)
Mutual labels:  code-splitting, ssr, server-side-rendering
React Redux Saucepan
A minimal and universal react redux starter project. With hot reloading, linting and server-side rendering
Stars: ✭ 86 (-53.01%)
Mutual labels:  boilerplate, ssr, server-side-rendering
React Router Server
Server Side Rendering library for React Router v4.
Stars: ✭ 443 (+142.08%)
Mutual labels:  code-splitting, ssr, server-side-rendering
Suicrux
🚀 Ultimate universal starter with lazy-loading, SSR and i18n. [not maintained]
Stars: ✭ 958 (+423.5%)
Mutual labels:  starter, boilerplate, ssr
React App
Create React App with server-side code support
Stars: ✭ 614 (+235.52%)
Mutual labels:  boilerplate, ssr, server-side-rendering
Mern
🎉 This is boilerplate for MERN stack with integrations like Redux and SSR 🎉
Stars: ✭ 77 (-57.92%)
Mutual labels:  boilerplate, ssr, server-side-rendering
React Universal Boiler
A bold way to begin your next great universal React application. Uses Webpack 3, React 16, Redux, and more for a great developer experience.
Stars: ✭ 65 (-64.48%)
Mutual labels:  boilerplate, ssr, server-side-rendering
Modern-Web-App
React PWA with SSR and Code splitting
Stars: ✭ 45 (-75.41%)
Mutual labels:  ssr, code-splitting, server-side-rendering
Razzle Material Ui Styled Example
Razzle Material-UI example with Styled Components using Express with compression
Stars: ✭ 117 (-36.07%)
Mutual labels:  boilerplate, ssr, server-side-rendering
isomorphic-react-redux-saga-ssr
Isomorphic, React, Redux, Saga, Server Side rendering, Hot Module Reloading, Ducks, Code Splitting
Stars: ✭ 19 (-89.62%)
Mutual labels:  ssr, code-splitting, server-side-rendering
React Firebase Starter
Boilerplate (seed) project for creating web apps with React.js, GraphQL.js and Relay
Stars: ✭ 4,366 (+2285.79%)
Mutual labels:  boilerplate, ssr, server-side-rendering
Hapi React Hot Loader Example
Simple React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-75.96%)
Mutual labels:  code-splitting, ssr, server-side-rendering
Koa React Universal
lightweight React-Koa2 universal boilerplate, only what is essential
Stars: ✭ 112 (-38.8%)
Mutual labels:  boilerplate, code-splitting, ssr
React Core Boilerplate
Powerful ASP.NET Core 3 templates with React, true server-side rendering and Docker support
Stars: ✭ 169 (-7.65%)
Mutual labels:  boilerplate, ssr, server-side-rendering
Angular9 Example App
Angular 13 Example App + Angular CLI + i18n + GraphQL
Stars: ✭ 1,769 (+866.67%)
Mutual labels:  boilerplate, ssr
React Universally
This starter kit contains all the build tooling and configuration you need to kick off your next universal React project, whilst containing a minimal "project" set up allowing you to make your own architecture decisions (Redux/MobX etc).
Stars: ✭ 1,704 (+831.15%)
Mutual labels:  boilerplate, server-side-rendering
Typescript React Native Starter
A highly scalable foundation with a focus on best pratices and simplicity to start your React Native project in seconds.
Stars: ✭ 141 (-22.95%)
Mutual labels:  starter, boilerplate

This project is not actively maintained

React Server Side Rendering Boilerplate ⚛️

Tools like create-react-app have made setting up client-side React apps trivial, but transitioning to SSR is still kind of a pain in the ass. Next.js is a powerhouse, and the Razzle tool looks like an absolute beast, but sometimes you just want to see the whole enchilada running your app. This is a sample setup for fully featured, server-rendered React applications.

What's included:

  • Server-side rendering with code splitting (via the excellent React Loadable package)
  • Server-side data fetching and client-side hydration
  • React Router
  • Conditionally load pollyfills -- only ship bloat to outdated browsers
  • React Helmet for dynamic manipulation of the document <head />
  • Dev server with hot reloading styles
  • Jest and react-testing-library ready to test the crap out of some stuff
  • CSS Modules, Sass, and autoprefixer
  • Run-time environment variables
  • Node.js clusters for improved performance under load (in production)
  • Prettier and ESLint run on commit
  • Docker-ized for production like a bawsss

Initial setup

  • npm install

Development

Production

  • npm run build && npm run start:prod
    • Bundle the JS and fire up the Express server for production
  • npm run docker
    • Build and start a local Docker image in production mode (mostly useful for debugging)

General architecture

This app has two main pieces: the server and the client code.

Server (server/)

A fairly basic Express application in server/app.js handles serving static assets (the generated CSS and JS code in build/ + anything in public/ like images and fonts), and sends all other requests to the React application via server/renderServerSideApp.js. That function delegates the fetching of server-side data fetching to server/fetchDataForRender, and then sends the rendered React application (as a string) injected inside the HTML-ish code in server/indexHtml.js.

During development the server code is run with @babel/register and middleware is added to the Express app (see scripts/start), and in production we bundle the server code to build/server and the code in scripts/startProd is used to run the server with Node's cluster module to take advantage of multiple CPU cores.

Client (src/)

The entrypoint for the client-side code (src/index.js) first checks if the current browser needs to be polyfilled and then defers to src/main.js to hydrate the React application. These two files are only ever called on the client, so you can safely reference any browser APIs here without anything fancy. The rest of the client code is a React application -- in this case a super basic UI w/2 routes, but you can safely modify/delete nearly everything inside src/ and make it your own.

As with all server-rendered React apps you'll want to be cautious of using browser APIs in your components -- they don't exist when rendering on the server and will throw errors unless you handle them gracefully (I've found some success with using if (typeof myBrowserAPI !== 'undefined') { ... } checks when necessary, but it feels dirty so I try to avoid when possible). The one exception to this is the componentDidMount() method for class components and useEffect() & useLayoutEffect() hooks, which are only run on the client.

"How do I ...?"

Fetch data on the server before rendering?

The client-side sample code to handle is a little experimental at the moment.

Sometimes you'll want to make API calls on the server to fetch data before rendering the page. In those cases you can use a static fetchData() method on any component. That method will be called with the req object from express, and it should return a Promise that resolves to an object, which will be merged with other fetchData() return values into a single object. That object of server data is injected into the server HTML, added to window.__SERVER_DATA__, and used to hydrate the client via the <ServerDataProvider /> context provider. Components can use the useServerData() hook to grab the data object. IMPORTANT: Your component must handle the case where the server data property it's reading from is undefined.

Check out src/components/Home.js for an example.

Add Redux?

Adding redux takes a few steps, but shouldn't be too painful; start by replacing the <ServerDataProvider /> with the <Provider /> from react-redux on both the server and the client. You can then pass the store as an argument to the static fetchData() method (in server/fetchDataForRender.js) and dispatch actions inside of fetchData(). Finally you'll need to pass the store's current state to the index.html generator function so you can grab it on the client and hydrate the client-side store.

Current Quirks

  • There are console message saying "componentWillMount has been renamed, and is not recommended for use." due to the react-loadable package. Hopefully React will support SSR with Suspense soon, but until then react-loadable works great and the console messages should not affect your app.
  • This project does not have a webpack configuration that allows for the use of url-loader or file-loader (so no import src from 'my-img.svg'). Instead it relies on serving static assets via the public/ directory. See src/components/about/About.js for a reference on how to work with assets in your app.

Roadmap

  • [ ] Run server via webpack in dev mode so we can use more loaders
  • [x] Intelligently resolve CSS modules by looking for a .module.s?css file extension
  • [ ] Add example app that handles authentication
  • [x] Migrate to react-testing-library instead of enzyme
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].