All Projects → 5tefan → Universal React Demo

5tefan / Universal React Demo

Licence: mit
ES6 demo of a simple but scalable React app with react-router, code splitting, server side rendering, and tree shaking.

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects
es2015
71 projects

Projects that are alternatives of or similar to Universal React Demo

Push Starter
React Redux Starter with SSR 🤖
Stars: ✭ 43 (-14%)
Mutual labels:  webpack4, universal, isomorphic, react-router-v4
Universal
Seed project for Angular Universal apps featuring Server-Side Rendering (SSR), Webpack, CLI scaffolding, dev/prod modes, AoT compilation, HMR, SCSS compilation, lazy loading, config, cache, i18n, SEO, and TSLint/codelyzer
Stars: ✭ 669 (+1238%)
Mutual labels:  starter, universal, isomorphic, server-side-rendering
Reactql
Universal React+GraphQL starter kit: React 16, Apollo 2, MobX, Emotion, Webpack 4, GraphQL Code Generator, React Router 4, PostCSS, SSR
Stars: ✭ 1,833 (+3566%)
Mutual labels:  webpack4, universal, server-side-rendering
universal-react-relay-starter-kit
A starter kit for React in combination with Relay including a GraphQL server, server side rendering, code splitting, i18n, SEO.
Stars: ✭ 14 (-72%)
Mutual labels:  isomorphic, universal, server-side-rendering
react-ssr-hydration
Example of React Server Side Rendering with Styled Components and Client Side Hydration
Stars: ✭ 15 (-70%)
Mutual labels:  isomorphic, universal, server-side-rendering
Beidou
🌌 Isomorphic framework for server-rendered React apps
Stars: ✭ 2,726 (+5352%)
Mutual labels:  universal, isomorphic, server-side-rendering
Egg Vue Webpack Boilerplate
Egg Vue Server Side Render (SSR) / Client Side Render (CSR)
Stars: ✭ 1,302 (+2504%)
Mutual labels:  webpack4, isomorphic, server-side-rendering
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+894%)
Mutual labels:  isomorphic, universal, server-side-rendering
Universal React Router4
Demo app showing how to use react-router v4 for server- and client-side rendering
Stars: ✭ 216 (+332%)
Mutual labels:  isomorphic, demo, server-side-rendering
webpack-isomorphic-compiler
A compiler that makes your life easier if you are building isomorphic webpack powered apps, that is, single page applications with server-side rendering
Stars: ✭ 16 (-68%)
Mutual labels:  isomorphic, universal, server-side-rendering
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-12%)
Mutual labels:  webpack4, isomorphic, server-side-rendering
Universal React
A universal react starter, with routing, meta, title, and data features
Stars: ✭ 247 (+394%)
Mutual labels:  universal, isomorphic, server-side-rendering
Razzle Material Ui Styled Example
Razzle Material-UI example with Styled Components using Express with compression
Stars: ✭ 117 (+134%)
Mutual labels:  universal, isomorphic, server-side-rendering
Koa React Universal
lightweight React-Koa2 universal boilerplate, only what is essential
Stars: ✭ 112 (+124%)
Mutual labels:  universal, isomorphic, react-router-v4
Go Starter Kit
[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Stars: ✭ 2,855 (+5610%)
Mutual labels:  universal, isomorphic, server-side-rendering
Helium.js
Automating Universal React Applications
Stars: ✭ 63 (+26%)
Mutual labels:  isomorphic, universal, react-router-v4
isomorphic-react-redux-saga-ssr
Isomorphic, React, Redux, Saga, Server Side rendering, Hot Module Reloading, Ducks, Code Splitting
Stars: ✭ 19 (-62%)
Mutual labels:  isomorphic, server-side-rendering, react-router-v4
React Server Example Tsx
⚛️ Boilerplate for isomorphic web app with React server-side rendering in TypeScript
Stars: ✭ 256 (+412%)
Mutual labels:  isomorphic, jsx, server-side-rendering
Awesome Nextjs
📔 📚 A curated list of awesome resources : books, videos, articles about using Next.js (A minimalistic framework for universal server-rendered React applications)
Stars: ✭ 6,812 (+13524%)
Mutual labels:  universal, isomorphic, server-side-rendering
Catberry
Catberry is an isomorphic framework for building universal front-end apps using components, Flux architecture and progressive rendering.
Stars: ✭ 793 (+1486%)
Mutual labels:  universal, isomorphic

Universal-react-demo

This is the example application I wish I had when I started writing React.

I've found that many template/demo/starters give you too much. It's hard to start when there's lots of moving pieces. I've tried to err on the side of giving you a minimal skeleton instead of a feature rich boilerplate.

You should be able to understand every piece of this demo, and doing so will give you a strong foundation on which to start building larger apps. I believe this is a better approach than starting with a monsterous boilerplate where you're left with either feature bloat or to take things out before you even know what they do.

Features:

Up and running

Getting started

Clone the repo, then run npm install to install the dependencies listed in package.json.

Development view

To start the development server on http://localhost:8000 to preview the application, execute npm run dev.

This runs webpack-dev-server --hot which will serve the bundle and update the bundle on file changes.

Note: webpack-dev-server does not watch the webpack config file for changes.

webpack-dev-server

Build the client bundle

Run npm run build:client to bundle all assets into a build/ dir. This step must be done before server side rendering is possible.

Since all our assets are in jsx, and es6, we have to transpile them to regular Javascript for today's browsers to understand. This is where webpack works it's wonders, including static analysis to determine unused pieces of code that don't need to be included. This is called tree-shaking and can dramatically reduce the size of the resulting code you need to send to a user.

Server side rendering bundle

The file server.js implements server side rendering. It uses ES6+JSX syntax, so again, we need to convert it to regular Javascript for Node to execute.

Run npm run build:server to do this. Webpack will bundle it into ./build/server.js.

Now, you can run node build/server.js to view the server side rendered version of your application at http://localhost:8000.

A shortcut to build both the client and server, and then run the server is: npm run start:prod.

To really see what server side rendering is up to, try loading the page with Javascript disabled. You'll still get the full experience. This is amazing!!

Webpack

Webpack is a bundler. It does a lot, but at the core, it looks for import and require statements and processes them into assets for the browser. We are using Webpack 2.0 which supports tree-shaking, which eliminates code that is never used, resulting in smaller bundles.

Loaders

You'll notice in the code, we have things like

import styles from './App.scss';

Why are we importing scss into a jsx file?!

Because we want to apply the styles inside App.scss to the component in our current file. Webpack handles the internals of processing the scss file into css, and giving you an object with keys that refer to string names of the styles you set.

In the Webpack configuration file webpack.client.config.js, we specify

{
    test: /\.scss$/,
    loader: 'style-loader!css-loader?modules&loacalIdentName=[name]--[local]--[hash:base64:5]!sass-loader'
}

This tells Webpack to process assets ending in .scss using first

  1. sass-loader which converts scss to css, then pipe it through:
  2. css-loader which does some namespacing and other stuff(read their README :),
  3. Finally through style-loader

Adds CSS to the DOM by injecting a <style> tag

The other loader, testing for jsx, is responsible for transpiling ES6 and JSX to ES5 (native browser Javascript).

Plugins

We're using a couple of plugins for convenience.

HtmlWebpackPlugin

The plugin will generate an HTML5 file for you that includes all your webpack bundles in the body using script tags.

From html-webpack-plugin

Using this plugin, we don't have to worry about our entry points and making sure the index.html page includes them. The index.html page under app/ serves as the template for this Single Page App. So, if you want to set the title, add meta tags, or whatever else... that's the place to do it.

FaviconsWebpackPlugin

The variety of favicon sizes and filenames devices expect (see favicons-best-practices SO question). is bordering on ridiculous. Luckily we can automatically create all of these with the FaviconsWebpackPlugin. This plugin automatically generates all of them from the file specified, app/images/favicon.png and, in conjunction with the HtmlWebpackPlugin, places them in the head of our templateindex.html file on build.

Code splitting

One traditional problem with Single Page Applications is that all the code had to be loaded at once on initial page load, even if it wasn't all needed. This often means seconds of blank screen and unnecessary data transfer. Webpack is able to split your code into "chunks" that can be loaded on demand. Only the code for the visible page needs to be fetched from the server and successive page visits fetch new chunks as needed.

See Webpack code splitting documentation.

In this demo, see any route specification like app/routes/About/index.js

if (ONSERVER) {
    module.exports = require('./components/About.jsx').default;
} else {
    module.exports = () => (<Bundle load={ () => import('./components/About.jsx') }/>);
}

In this Demo, we use import for fetching react-router components. This results in chunks for for our About page which are only fetched when the client visits /about. The Bundle component is a convenience wrapper which displays a loading component while the Promise returned by System.import is resoliving. Once resolved, Bundle changes it's state to display the requested component.

About the ONSERVER switch: This constant is defined in the webpack config file webpack.server.config.js as true and false in the client config. Although admittedly not the sexy, it seems to be the most straightforward approach and is necessary at the moment because the server side renderToString doesn't wait for Promises (ie. chunking) to resolve. Instead, we have to disable the chunking/asyc module fetch by switching to a synchronous require(). We could just use require() everywhere (server side and client) but then we'd loose chunking!

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].