Giter VIP home page Giter VIP logo

khmmamed / ssr-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hiroppy/ssr-sample

0.0 0.0 0.0 2.45 MB

a sample of server side rendering using typescript + react + redux + react-router + redux-saga + react-helmet + loadable-components + recompose + styled-components + apollo + express + webpack etc..

Home Page: http://blog.hiroppy.me/entry/ssr-sample

TypeScript 93.79% JavaScript 5.98% HTML 0.23%

ssr-sample's Introduction

A sample of Server-Side-Rendering and Single-Page-Application

Travis Codecov

This repository shows you how to write SSR and SPA code using TypeScript.

Articles

業務で使える簡単な SSR + SPA のテンプレートを公開した

Feature

  • how to render at server side using Node.js (/src/server)
    • express
    • redux-saga (side effects)
    • styled-components (css)
    • react-helmet (head)
    • loadable-components (dynamic import)
  • how to realize single page application (/src/client)
    • react-router
    • recompose (error handling, etc...)
    • App Shell and Content (PWA)
  • how to use Apollo(GraphQL) with SSR and SPA
    • apollo-boost
    • apollo-server-express
    • react-apollo
  • how to write test code and storybook
    • express, react, redux, redux-saga, react-router, etc...
  • how to develop in the development env and how to run the production env
    • HMR of webpack
    • dotenv
    • manifest
    • load distribution
    • dynamic import
  • how to measure application performance
    • why-did-you-update
    • autocannon
    • clinic
  • Security
    • CSP(lv3, lv2)

Libraries

Name Purpose CSR SSR
react view yes yes
redux architecure yes yes
react-helmet head tag yes yes
recompose HOC 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
Name Purpose
typescript, ts-node language
webpack bundler
babel transpile typescript
storybook preview
storyshots snapshot tests
jest test runner
workbox service worker
autocannon benchmarking tool
clinic performance profiling

Global Variables

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

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

window.__LOADABLE_STATE__

an article: https://blog.hiroppy.me/entry/loadable-components

This variable is used by loadable-component.
It has a JS asset's path of a page used at the page's entry point.

window.__APOLLO_STATE__

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

data-json

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

Pages

src: src/client/components/pages/.

This application has 2 pages.

Top

This page runs just graphql application.(excluding header and base)

src: src/client/components/pages/Top

SSR

Check window.__APOLLO_STATE__ in HTML.

Orgs

This page runs just redux-saga application.

page src: src/client/components/pages/Orgs
saga src: src/client/sagas/orgs.ts

  • fetching using redux-saga

SSR

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

Sagas

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

These pages fork saga processes.

  • appProcess
    • a common processing to execute on all pages(e.g. confirming login)
  • pages
    • loadTopPage
      • just stop saga when it ran at a server
    • loadOrgsPage
      • 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.

Setup

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

Development

$ npm start                  # use 3000
$ open http://localhost:3000
$ npm run start:storybook    # use 6006
$ open http://localhost:6006

GraphQL Playground

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

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://localhost8080

Deploy

$ npm run deploy:storybook

Note

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.