Slide 1

Slide 1 text

PWA w/React.js & Firebase

Slide 2

Slide 2 text

+JimmyMoon @ragingwind

Slide 3

Slide 3 text

- Simple PWA with React.js - App Shell Architecture - Web Manifest - Service Worker - Firebase - Tuning on PRPL - Auditing by Lighthouse

Slide 4

Slide 4 text

- create-react-app - (react-scripts < 0.9.5) - Webpack > 2.4.x - PWA features - Minimal snippets - One of the app for guiding PWA

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

create-react-app

Slide 7

Slide 7 text

> yarn init && tree . └── package.json

Slide 8

Slide 8 text

> touch public/index.html && tree . └── public └── index.html

Slide 9

Slide 9 text

./public/index.html

Slide 10

Slide 10 text

> mkdir -p src/components/ > touch ./src/components/App.js \ ./src/main.js

Slide 11

Slide 11 text

> tree . └── src ├── components │ └── App.js └── main.js

Slide 12

Slide 12 text

> yarn add react react-dom

Slide 13

Slide 13 text

import React from 'react'; import ReactDOM from 'react-dom'; import App from './App.js'; ReactDOM.render(, document.getElementById('app')); ./src/main.js

Slide 14

Slide 14 text

import React from 'react'; class App extends React.Component { render() { return (

Hello World !!

); } } ./src/components/App.js

Slide 15

Slide 15 text

...
... ./public/index.htm

Slide 16

Slide 16 text

> yarn add --dev webpack \ webpack-dev-server > touch webpack.config.js

Slide 17

Slide 17 text

module.exports = { entry: { main: ['./src/main.js'], }, output: { path: path.resolve( __dirname, './build'), filename: '[name].js' } ... }; ./webpack.config.js

Slide 18

Slide 18 text

module.exports = { ... devServer: { contentBase: './public', inline: true, host: 'localhost', port: 8080 } }; ./webpack.config.js

Slide 19

Slide 19 text

... ... ./public/index.html

Slide 20

Slide 20 text

> yarn add --dev babel-core \ babel-loader \ babel-preset-react-app > touch .babelrc

Slide 21

Slide 21 text

{ // babel preset for react app // made by facebook "presets": ["react-app"] } ./.babelrc

Slide 22

Slide 22 text

module.exports = { ... module: { loaders: [{ test: /\.(js|jsx)$/, include: path.resolve( __dirname, './src'), loaders: 'babel-loader' }] } }; ./webpack.config.js

Slide 23

Slide 23 text

{ ... // add dev server and build script to package.json "scripts": { "start": "NODE_ENV=development webpack-dev-server", "build": "rm -rf build && NODE_ENV=production webpack -p" }, ... } ./package.json

Slide 24

Slide 24 text

> yarn start

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

App Shell 
 Architecture

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

> yarn add material-ui \ react-tap-event-plugin > touch ./src/components/AppShell.js

Slide 30

Slide 30 text

import reactTabEventPlugin from 'react-tap-event-plugin'; reactTabEventPlugin(); ./src/main.js

Slide 31

Slide 31 text

class AppShell extends React.Component { render() { return (
{React.cloneElement(this.props.children)}
); } }; ./src/components/AppShell.js

Slide 32

Slide 32 text

import {MuiThemeProvider, getMuiTheme} from 'material-ui/styles'; import {AppBar, Drawer, MenuItem} from 'material-ui'; ./src/components/AppShell.js

Slide 33

Slide 33 text

constructor(props) { // drawer open flag this.state = {open: false}; } ./src/components/AppShell.js

Slide 34

Slide 34 text

render() { return (
...
); } ./src/components/AppShell.js

Slide 35

Slide 35 text

... handleDrawerToggle = () => this.setState({open: !this.state.open}) handleRequestChange = open => this.setState({open: open}) ... ./src/components/AppShell.js

Slide 36

Slide 36 text

class App extends React.Component { render() { return (

Hello World !!

); } } ./src/components/AppShell.js

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Web Manifest

Slide 39

Slide 39 text

> npm install -g pwa-manifest-cli > pwa-manifest ./public \ --icons=ICONPATH_LOCAL_OR_HTTP

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

> tree -L 3 ./public ├── favicon.ico ├── icon-144x144.png ├── icon-192x192.png ├── icon-512x512.png ├── index.html └── manifest.json

Slide 44

Slide 44 text

{ "name": "react-pwa", "short_name": "react-pwa", "icons": [ { "src": "icon-144x144.png", "sizes": "144x144", "type": "image/png" }, ... ], "start_url": "./?utm_source=web_app_manifest", "display": "standalone", "orientation": "natural", "background_color": "#FFFFFF", "theme_color": "#3F51B5" } ./public/manifest.json

Slide 45

Slide 45 text

./public/index.html

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Service Worker

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

> yarn add --dev \ sw-precache-webpack-plugin-loader \ copy-webpack-plugin

Slide 50

Slide 50 text

const CopyWebpackPlugin = require('copy-webpack-plugin'); const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin-loader'); ./webpack.config.json

Slide 51

Slide 51 text

plugins: [ new CopyWebpackPlugin([{ context: './public', from: '*.*' }]) ] ./webpack.config.json

Slide 52

Slide 52 text

plugins: [ new SWPrecacheWebpackPlugin({ staticFileGlobs: [CACHED_FILES_GLOBS], logger: function () {}, filename: 'sw.js' }) ] ./webpack.config.json

Slide 53

Slide 53 text

if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('sw.js'); }); } ./public/index.html

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

More views, React Routing

Slide 56

Slide 56 text

> yarn add react-router-dom > touch ./src/components/Home.js > tree ./src/components ├── App.js ├── AppShell.js └── Home.js

Slide 57

Slide 57 text

import {Card, CardTitle, CardText} from 'material-ui/Card'; ./src/components/Home.js

Slide 58

Slide 58 text

class Home extends React.Component { render () { return ( ... ) } } ./src/components/Home.js

Slide 59

Slide 59 text

import {HashRouter as Router, Route} from 'react-router-dom'; import Home from './Home'; ./src/components/App.js

Slide 60

Slide 60 text

class App extends React.Component { render() { return (

Hello World !!

); } } ./src/components/App.js

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

> touch ./src/components/Users.js > touch ./src/components/Notification.js > tree ./src/components ├── App.js ├── AppShell.js ├── Home.js ├── Users.js └── Notification.js

Slide 63

Slide 63 text

import React from 'react'; class Users extends React.Component { render() { return (
Users
); } } export default Users; ./src/components/Users.js

Slide 64

Slide 64 text

import React from 'react'; class Notification extends React.Component { render() { return (
Notification
); } } export default Notification; ./src/components/Notification.js

Slide 65

Slide 65 text

import Users from './Users'; import Notification from './Notification'; ./src/components/App.js

Slide 66

Slide 66 text

./src/components/App.js

Slide 67

Slide 67 text

import {Link} from 'react-router-dom'; ... } onClick={this.handleDrawerToggle} /> } onClick={this.handleDrawerToggle} /> ./src/components/AppShell.js

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

Firebase Hosting with HTTPS

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

> npm install -g firebase-tools

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

> tree -L 1 ./ -a -I node_modules ├── .babelrc ├── .firebaserc ├── firebase.json ├── package.json ├── public ├── src ├── webpack.config.js └── yarn.lock

Slide 74

Slide 74 text

{ "hosting": { "public": "build" } } ./firebase.json

Slide 75

Slide 75 text

{ "projects": { "default": "YOUR-PROJECT-NAME" } } ./.firebaserc

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

> yarn build > tree -L 1 ./ -a -I node_modules ├── favicon.ico ├── icon-144x144.png ├── icon-192x192.png ├── icon-512x512.png ├── index.html ├── main.js ├── manifest.json ├── notification.js └── sw.js

Slide 78

Slide 78 text

Firebase Realtime Database

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

class Users extends React.Component { constructor() { super(); this.state = { users: [] }; } } ./src/components/Users.js

Slide 83

Slide 83 text

componentDidMount() { const databaseURL = 'https: //YOUR_DATABASE_URL'; fetch(`${databaseURL}/data.json/`).then(res => { if (res.status !== 200) { throw new Error(res.statusText); } return res.json(); }).then(users => this.setState({users: users})) } ./src/components/Users.js

Slide 84

Slide 84 text

render() { const users = () => { return Object.keys(this.state.users).map(id => { const user = this.state.users[id]; return ( ); }); } return (
{users()}
); } ./src/components/Users.js

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

plugins: [ new SWPrecacheWebpackPlugin({ ... runtimeCaching: [{ urlPattersn: /https:\/\/.+.firebaseio.com/, handler: 'networkFirst' }] }) ] ./webpack.config.js

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

Firebase Cloud Messaging

Slide 90

Slide 90 text

> yarn add firebase

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

{ gcm_sender_id: "103953800507", ... }; ./manifest.json

Slide 93

Slide 93 text

import firebase from 'firebase'; ./src/components/Notification.js

Slide 94

Slide 94 text

var config = { apiKey: "AIzasdakagskgei@#9412i8123WWEeFwyuOk4af3vhYFw", authDomain: "YOURPROJECT.firebaseapp.com", databaseURL: "https: //YOURPROJECT.firebaseio.com", projectId: "YOURPROJECT", storageBucket: "YOURPROJECT.appspot.com", messagingSenderId: "2595534347235" }; ./src/components/Notification.js

Slide 95

Slide 95 text

class Notification extends React.Component { static firebaseApp; constructor(props) { super(props); if (!Notification.firebaseApp) { firebase.initializeApp(config); } } } ./src/components/Notification.js

Slide 96

Slide 96 text

class Notification extends React.Component { constructor(props) { ... this.state = { token: '', message: '' }; } } ./src/components/Notification.js

Slide 97

Slide 97 text

componentDidMount() { const messaging = firebase.messaging(); messaging.onMessage(this.handlePushMessage); messaging.requestPermission() .then(() => messaging.getToken()) .then(token => this.setState({token: token})) .catch(err => { throw new Error(err); }); } ./src/components/Notification.js

Slide 98

Slide 98 text

handlePushMessage = noti => { this.setState({ message: `${noti.title}: ${noti.body}` }); } ./src/components/Notification.js

Slide 99

Slide 99 text

render() { return (
); } ./src/components/Notification.js

Slide 100

Slide 100 text

> touch ./src/firebase-messaging-sw.js > tree -L 1 ./src -I node_modules ├── components ├── firebase-messaging-sw.js └── main.js

Slide 101

Slide 101 text

importScripts('https: // www.gstatic.com/firebasejs/3.5.2/firebase-app.js'); importScripts('https: // www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js'); ./src/firebase-messaging-sw.js

Slide 102

Slide 102 text

var firebaseConfig = { ... }; firebase.initializeApp(firebaseConfig); const messaging = firebase.messaging(); messaging.setBackgroundMessageHandler(({data} = {}) => { const title = data.title || 'Title'; const opts = Object.assign({body: data.body || 'Body'}, data); return self.registration.showNotification(title, opts); }); ./src/firebase-messaging-sw.js

Slide 103

Slide 103 text

plugins: [ new CopyWebpackPlugin([{ ... }, { from: './src/firebase-messaging-sw.js', to: 'firebase-messaging-sw.js', }]), ] ./webpack.config.js

Slide 104

Slide 104 text

> npm install -g fcm-cli

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

> fcm send --server-key SERVER_KEY:bKFlxhMqS_PYGlCw4VTV \ --to TOKEN:A91bFmi_DEQFPQ2FMf_7V8NcEGGqkAZEV- \ --notification.title hi \ --notification.body 'Hello World'

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Tuning on PRPL Pattern

Slide 111

Slide 111 text

(P)ush critical resources for the initial route (R)ender initial route and get it interactive as soon as possible, (P)re-cache components for the remaining routes (L)azy-load, and create next routes on demand by user

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Kicking Mega Bundle

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Breaking down Common Chunks

Slide 118

Slide 118 text

module.exports = { entry: { main: ['./src/main.js'], vendor: ['react', 'react-dom'] }, plugins: [ new webpack.optimize.CommonsChunkPlugin({ name: 'vendor' }), ] } ./webpack.config.js

Slide 119

Slide 119 text

./public/index.html

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

Routes based Code-splitting

Slide 123

Slide 123 text

> yarn add --dev babel-plugin-syntax- dynamic-import

Slide 124

Slide 124 text

{ "plugins": [ "syntax-dynamic-import" ] } ./.babelrc

Slide 125

Slide 125 text

export default (getComponent) => ( class AsyncComponent extends React.Component { componentWillMount() { if (!this.state.Component) { getComponent().then(Component => { AsyncComponent.Component = Component this.setState({ Component }) }); } }. ... } ); ./src/components/AsyncComponent.js

Slide 126

Slide 126 text

import asyncComponent from './AsyncComponent'; const Home = asyncComponent(() => { return import( /* webpackChunkName: "home" */ './Home') .then(module => module.default); }); const Users = ...' const Notification = ...; ./src/components/App.js

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

Tree-shaking, Dead-code elimination Live-code Importing

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

import {Card, CardHeader} from 'material-ui'; import {Card, CardHeader} from 'material-ui/Card';

Slide 132

Slide 132 text

> yarn add --dev babel-preset-es2015 babel- plugin-transform-imports

Slide 133

Slide 133 text

{ "presets": [["es2015", {"modules": false}], "react-app"] } ./.babelrc

Slide 134

Slide 134 text

"plugins": [ [ "transform-imports", { "material-ui": { "transform": "material-ui/${member}", "preventFullImport": true }, "react-router-dom": { ...}, "lodash": { ...} } ] ] ./.babelrc

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

494 kB

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

Extract common parts Shared Common Chunk

Slide 139

Slide 139 text

plugins: [ new webpack.optimize.CommonsChunkPlugin({ children: true, async: 'common', minChunks: 2 }), ] ./webpack.config.js

Slide 140

Slide 140 text

./public/index.html

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

Control resources pushing Preload, Prefetch

Slide 146

Slide 146 text

> yarn add --dev html-webpack-plugin preload-webpack-plugin

Slide 147

Slide 147 text

const HtmlWebpackPlugin = require('html-webpack-plugin'); const PreloadWebpackPlugin = require('preload-webpack-plugin'); ./webpack.config.js

Slide 148

Slide 148 text

plugins: [ new HtmlWebpackPlugin({ filename: 'index.html', template: './public/index.html', favicon: './public/favicon.ico' }), new PreloadWebpackPlugin({ include: ['vendor', 'main', 'common', 'home'] }), ] ./webpack.config.js

Slide 149

Slide 149 text

./public/index.html

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

plugins: [ new PreloadWebpackPlugin({ rel: 'prefetch', include: ['users', 'notification'] }), ] ./webpack.config.js

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

More Optimization

Slide 155

Slide 155 text

class LazySidebarDrawer extends React.Component { componentDidMount() { let frameCount = 0; const open = () => (frameCount ++ > 0) ? this.props.onMounted() : requestAnimationFrame(open); requestAnimationFrame(open); } render() { return ( ... ); } }

Slide 156

Slide 156 text

render() { return ( ... {} ... ); } ./src/components/AppShell.js

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

> 144 kB

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

> yarn add react-lite

Slide 162

Slide 162 text

More plugins for Webpack

Slide 163

Slide 163 text

Auditing by Lighthouse

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

And more ...

Slide 166

Slide 166 text

No content