• This repository has been archived on 02/Dec/2021
  • Stars
    star
    287
  • Rank 143,874 (Top 3 %)
  • Language
    TypeScript
  • Created about 6 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimum sample of Server-Side-Rendering, Single-Page-Application and Progressive Web App

A minimum sample of Server-Side-Rendering, Single-Page-Application, and Progressive Web App

What's this project?

This project introduces how to implement SSR, SPA, and PWA.

Articles

only Japanese

Feature

This project shows several implementations like below.

  • Server Side Rendering
  • Single Page Application
  • Progressive Web App
  • GraphQL
  • RESTful API
  • Security (CSP, GraphQL)
  • Testing
  • Infra like the building tools
  • Measuring performance

Libraries

Dependencies

Name Purpose CSR SSR Note
react view yes yes
redux architecure yes yes
react-router routing yes yes
react-helmet head tag yes yes
redux-saga side effects yes yes
styled-components CSS in JS yes yes
loadable-components dynamic import yes yes
apollo-boost GraphQL yes yes
express server side framework N/A yes
nanoid Creating a random hash N/A N/A

DevDependencies

Name Purpose Note
typescript Alt
webpack a bundler for client side
babel transpile typescript and loadable-components
storybook preview
storyshots snapshot tests
jest test runner
testing-library a helper to test react
nodemon a watcher for server side
prettier formatter
typescript-eslint linter
workbox service worker
clinic performance profiling
autocannon benchmarking tool

Pages

See the router: src/client/router/.

This application has 3 pages and creates SPA based on redux and redux-saga.
Saga page and Apollo page use same components so you can compare each implementation.

Top

This page reads README.md using babel-plugin-macro.

src: src/client/components/pages/Top

Saga

This page runs just redux-saga application.

page src: src/client/components/pages/Saga

Apollo

This page runs just apollo application.

page src: src/client/components/pages/Apollo

Control SSR and SPA

design concept: gist
src: src/client/sagas/pages.ts

All pages fork saga processes.

  • appProcess
    • a common processing to execute on all pages(e.g. confirming login, sending to GA, etc...)
  • pages
    • loadTopPage, loadingApolloPage
      • just stop saga when it ran at a server
    • loadSagaPage
      • fetching data and then stopping if it ran at a server

appProcess and pages run in parallel, also they run the same code in a server and client.

Need to call END when running on Node.js

If you do SSR using redux-saga, you have to stop redux-saga process when all processes are finished.

try {
  // fetch...

  yield put(success());
} catch (err) {
  yield put(failure(err));
} finally {
  if (!process.env.IS_BROWSER) {
    yield put(END);
  }
}

Global Variables

src: src/server/controllers/renderer/renderer.tsx.

Use the following variables to pass data acquired by a server to the client side.

data-json

This script tag has state and data which are fetched via redux-saga, etc at the server.

<script id="initial-data" type="text/plain" data-json=...></script>.

window.__APOLLO_STATE__

This variable has GraphQL data which are fetched at the server.

Lighthouse

lighthouse

If you want to get 100 point for Best Practices, you need to set a reverse proxy server like Nginx because Express hasn't implemented http/2 yet.(also Performance)

Setup

$ git clone [email protected]:hiroppy/ssr-sample.git
$ cd ssr-sample
$ npm i

Development

$ npm start
$ open http://localhost:3000

# GraphQL Playground
$ open http://localhost:3000/graphql

Storybook

$ npm run start:storybook
$ open http://localhost:6006

Test

$ npm test

Production

$ npm run build             # npm run build:client + npm run build:server
$ npm run start:prod        # run server and use 3000
$ open http://localhost:8080

Deploy

$ npm run deploy:storybook

Performance

$ npm run build
$ npm run start:prod
$ npm run benchmark # rps

Running 10s test @ http://localhost:8080
100 connections

┌─────────┬────────┬────────┬────────┬─────────┬───────────┬───────────┬────────────┐
│ Stat    │ 2.5%   │ 50%    │ 97.5%  │ 99%     │ Avg       │ Stdev     │ Max        │
├─────────┼────────┼────────┼────────┼─────────┼───────────┼───────────┼────────────┤
│ Latency │ 161 ms │ 406 ms │ 829 ms │ 1277 ms │ 413.26 ms │ 191.69 ms │ 2649.38 ms │
└─────────┴────────┴────────┴────────┴─────────┴───────────┴───────────┴────────────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬─────────┬────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg     │ Stdev  │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼────────┼─────────┤
│ Req/Sec   │ 210     │ 210     │ 233     │ 264     │ 236.6   │ 18.87  │ 210     │
├───────────┼─────────┼─────────┼─────────┼─────────┼─────────┼────────┼─────────┤
│ Bytes/Sec │ 3.16 MB │ 3.16 MB │ 3.51 MB │ 3.98 MB │ 3.56 MB │ 284 kB │ 3.16 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴─────────┴────────┴─────────┘

Req/Bytes counts sampled once per second.

$ npm run benchmark:flame # flamegraph

flamegraph

Note

This repository shows how to write and so does not introduce Atomic Design.

More Repositories

1

fusuma

✍️Fusuma makes slides with Markdown easily.
JavaScript
5,320
star
2

slides

My slides
CSS
68
star
3

the-sample-of-module-bundler

You will know how to make a javascript bundler
JavaScript
34
star
4

demobook

Feel free to publish files from anywhere💕
TypeScript
26
star
5

garoogle

GaroonとGoogle Calendarの予定を同期します
JavaScript
25
star
6

site

🏡
MDX
22
star
7

convert-keys

Convert object keys to camelCase or snakeCase.
TypeScript
19
star
8

pwa-sample

JavaScript
16
star
9

chakra-ui-optimization-loader

Optimize your bundle size of chakra-ui
JavaScript
15
star
10

scuba

UI Components with a color unity that built by React. https://abouthiroppy.github.io/scuba/
JavaScript
13
star
11

dotfiles

my dotfiles
Vim Script
12
star
12

dynamic-rendering-sample

This repo introduces how to realize dynamic rendering.
Dockerfile
9
star
13

sweetpack

sweetpack helps eliminate time to suffer from webpack and babel😋
JavaScript
8
star
14

hiroppy

JavaScript
7
star
15

cli-boost

The easiest way to get started for creating CLI
TypeScript
7
star
16

bot-house

A bot lover
JavaScript
6
star
17

freee-kintai-kun

JavaScript
6
star
18

apollo-link-state-sample

TypeScript
4
star
19

generator-ts

My generator to create typescript project!
JavaScript
4
star
20

babel-init

Setup .babelrc using command.
JavaScript
3
star
21

the-minimal-example-of-graphql

JavaScript
3
star
22

i18n-json-plugins

TypeScript
3
star
23

check-global-variables

Get a list of window's variables.
JavaScript
3
star
24

node-committer

a commit formatter for node
JavaScript
3
star
25

apollo-link-state-sample-for-slide

TypeScript
2
star
26

express-routes-list

Confirm routing on the terminal.
JavaScript
2
star
27

fusuma-sample

https://hiroppy.github.io/fusuma-sample/
CSS
2
star
28

node-metrics

JavaScript
2
star
29

node-esm-example

JavaScript
2
star
30

webpack-mfe-sample

JavaScript
2
star
31

measurer

Simple performance helper for the Browser and Node.js.
TypeScript
2
star
32

bazero

#0CJS(zero config) for Babel
JavaScript
2
star
33

njo

njo creates JSON objects on command line.
JavaScript
2
star
34

nature-remo-bot

nature-remo
JavaScript
2
star
35

bluehigh.zsh-theme

zsh-theme
Shell
2
star
36

react-scalable

Fit your element with the outer frame
JavaScript
2
star
37

apollo-next.js-sample

how to handle an error
JavaScript
2
star
38

sample-box

a monorepo using lerna
JavaScript
1
star
39

timecard-slack-bot

TypeScript
1
star
40

niconico-player-extension

ニコニコ動画のHTML5プレイヤーの拡張
JavaScript
1
star
41

keybinding-decorator

Decorator for keybinding
TypeScript
1
star
42

nico-resident

nico-resident is a support tools for Nicovideo's ranking .
JavaScript
1
star
43

web-app-template

A template for minimal web app
TypeScript
1
star
44

cav

A supporter for making your CLI.
JavaScript
1
star
45

node-book

https://www.gitbook.com/book/abouthiroppy/node
1
star
46

grpc-sample

JavaScript
1
star
47

placehold-cli

Creates dummy image via https://placehold.jp/.
JavaScript
1
star
48

about.hiroppy

my module
JavaScript
1
star
49

sunmanure

This site will help you to fill the contributions of Github.
JavaScript
1
star
50

mu-law

Convert using μ-law algorithm.
JavaScript
1
star
51

javascript-study-for-beginners

https://www.gitbook.com/book/abouthiroppy/javascript-study-for-beginners/details
HTML
1
star
52

pick-member-action

1
star
53

honyaku

Translate japanese to english or english to japanese.
JavaScript
1
star
54

flatten-package-lock-deps

Flatten all dependencies in package-lock.json.
TypeScript
1
star
55

MainPage

my main site!
HTML
1
star
56

babel-plugin-styled-components-autoprefixer

autoprefixer for styled-components
JavaScript
1
star
57

downpile-node-modules-webpack-loader

I really really dislike IE😭
TypeScript
1
star