Gleb Bahmutov PRO
JavaScript ninja, image processing expert, software quality fanatic
NodeFest Tokyo 2016
A DATA ANALYTICS AND MACHINE INTELLIGENCE COMPANY
I love solving AngularJS performance problems
Using CDN, parallel downloads, caching, small images, above the fold, etc.
nice
what?
app
HTML
data
loads
renders
same
same
app
HTML
data
loads
Cache
Cache
renders
There is a flash 😢
The page HTML arrives ready to be rendered by the browser
The browsers are REALLY REALLY good at rendering static pages
ServiceWorker
Server
browser
Web Workers
ServiceWorker
Transforms
the response
Transforms
the request
Server
browser
service
worker
navigator.serviceWorker.register(
'app/my-service-worker.js')
Chrome, Opera, Firefox
Must be https
self.addEventListener('install', ...)
self.addEventListener('activate', ...)
self.addEventListener('message', ...)
self.addEventListener('push', ...)
self.addEventListener('fetch', function (event) {
console.log(event.request.url)
event.respondWith(...)
})
// Cache API
Server
browser
ServiceWorker
<html>
<body>
<header>...</header>
<div id="app"></div>
<footer>...</footer>
</body>
</html>
<html>
<body>
<header>...</header>
<div id="app">
<ul>
<li>Clean my room</li>
...
</ul>
</div>
<footer>...</footer>
</body>
</html>
<ul>
<li>Clean my room</li>
...
</ul>
Cache
Inserts cached HTML fragment into response body HTML
Demos: https://instant-todo.gleb-demos.com, https://glebbahmutov.com/bottle-service
The library: bahmutov/bottle-service
Blog post with examples: Instant web application
If the server goes down in the forest ...
Stop treating browser like a child
browser
ServiceWorker
Request
Response
Server
Server
browser
ServiceWorker
Request
Response
express.js
http.ClientRequest
JavaScript
http.ServerResponse
JavaScript
browser
ServiceWorker
Server
express.js
Server
browser
ServiceWorker
express.js
http.ClientRequest(Request)
http.ServerResponse(Response)
express.js + middleware
ServiceWorker env
browserify
const expressService = require('express-service')
const app = require('todomvc-express')
const urls = ['/', 'app.css']
const cacheName = require('./package.json').name
expressService(app, urls, cacheName)
"app" is a regular Express.js application
Demo at: https://express-service.gleb-demos.com
The library: github.com/bahmutov/express-service
Blog post with examples: Run Express server in your browser
There is no client-side* JavaScript :)
<link rel="serviceworker" scope="/" href="/sw.js">
*after first ServiceWorker registration
$ npm i -g nativefier
$ nativefier https://express-service.gleb-demos.com/
Packaging app for platform darwin x64 using electron v1.1.3
App built to /Users/gleb/git/ExpressService-darwin-x64
Express-service stand alone desktop app
Mozilla SW recipes serviceworke.rs
Jake Archibald (Google) - Using ServiceWorker in Chrome today, The ServiceWorker is coming, look busy
Matt Gaunt (Google) - Unit testing ServiceWorker
Instant apps
Server in the browser
Code transpiling, coverage, testing
By Gleb Bahmutov
The line between the server and the modern browser is blurry. Transpiling code, rendering static markup, smart caching were traditionally done on the server. These features made the jump to the browser by using ServiceWorkers.
JavaScript ninja, image processing expert, software quality fanatic