├── public ├── shell-end.html ├── shell-start.html └── sw.js ├── index.js ├── DemoComponent.js ├── package.json ├── README.md ├── .gitignore ├── LICENSE └── server.js /public/shell-end.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require('babel-core/register')({ 2 | presets: ['es2015', 'react'] 3 | }) 4 | require("./server"); -------------------------------------------------------------------------------- /DemoComponent.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default function DemoComponent(props, context) { 4 | return
{Array(10000).fill(1).map((i,j) =>
{j}
)}
; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /public/shell-start.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Yeah. From serviceWorker! 6 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sw-react-stream", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "start" : "node index.js" 8 | }, 9 | "engines": { 10 | "node": "4.1.1" 11 | }, 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "babel": "^6.3.26", 16 | "babel-core": "^6.4.5", 17 | "babel-preset-es2015": "^6.3.13", 18 | "babel-preset-react": "^6.3.13", 19 | "express": "^4.13.4", 20 | "react": "^0.14.7", 21 | "react-dom": "^0.14.7", 22 | "react-dom-stream": "^0.4.1" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sw-react-stream 2 | Streaming server rendered react response inside Service Workers! 3 | 4 | All credit to [this article](https://jakearchibald.com/2016/streams-ftw/) by [Jake Archibald](https://github.com/jakearchibald) 5 | 6 | 7 | ##### Note: You'll need Chrome Canary with chrome://flags/#enable-experimental-web-platform-features enabled. 8 | [Live Demo](https://sw-react-stream.herokuapp.com/) 9 | 10 | For running locally 11 | 12 | ``` 13 | git clone https://github.com/mayankchd/sw-react-stream.git 14 | cd sw-react-stream 15 | npm install 16 | npm start 17 | ``` 18 | go to 19 | ``` 20 | localhost:3000 21 | ``` 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | .DS_Store 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mayank Chandola 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | import express from 'express'; 2 | 3 | import React from 'react'; 4 | import ReactDOM from 'react-dom'; 5 | import ReactDOMStream from "react-dom-stream/server"; 6 | import DemoComponent from './DemoComponent'; 7 | 8 | var app = express(); 9 | app.use(express.static('public')); 10 | 11 | app.get('/', (req, res) => { 12 | res.write(` 13 | 14 | Server Streamed 15 | 26 | 27 | 30 | 31 | 32 | 33 |
`) 34 | var stream = ReactDOMStream.renderToString(); 35 | stream.pipe(res, {end: false}); 36 | stream.on("end", function() { 37 | res.write(`
`); 38 | res.end(); 39 | }); 40 | }); 41 | 42 | app.get('/sw-stream-render', (req, res) => { 43 | var stream = ReactDOMStream.renderToString(); 44 | stream.pipe(res, {end: false}); 45 | stream.on("end", function() { 46 | res.end(); 47 | }); 48 | }); 49 | 50 | var port = process.env.PORT || 3000; 51 | app.listen(port, (error) => { 52 | console.log(error) 53 | }); -------------------------------------------------------------------------------- /public/sw.js: -------------------------------------------------------------------------------- 1 | var version = 2; 2 | var cacheName = `static-cache-${version}`; 3 | 4 | self.addEventListener('install', event => { 5 | event.waitUntil( 6 | caches.open(cacheName).then(cache => { 7 | return cache.addAll(['/shell-start.html', '/shell-end.html']); 8 | }) 9 | ) 10 | }); 11 | 12 | self.addEventListener('fetch', event => { 13 | event.respondWith(streamContent()); 14 | }); 15 | 16 | function streamContent() { 17 | try { 18 | new ReadableStream({}); 19 | } 20 | catch(e) { 21 | return new Response("Streams not supported"); 22 | } 23 | const stream = new ReadableStream({ 24 | start(controller) { 25 | const startFetch = caches.match('/shell-start.html'); 26 | const contentFetch = fetch('/sw-stream-render').catch(() => new Response("Failed, soz")); 27 | const endFetch = caches.match('/shell-end.html'); 28 | 29 | function pushStream(stream) { 30 | const reader = stream.getReader(); 31 | function read() { 32 | return reader.read().then(result => { 33 | if (result.done) return; 34 | controller.enqueue(result.value); 35 | return read(); 36 | }); 37 | } 38 | return read(); 39 | } 40 | 41 | startFetch 42 | .then(response => pushStream(response.body)) 43 | .then(() => contentFetch) 44 | .then(response => pushStream(response.body)) 45 | .then(() => endFetch) 46 | .then(response => pushStream(response.body)) 47 | .then(() => controller.close()); 48 | } 49 | }); 50 | 51 | return new Response(stream, { 52 | headers: {'Content-Type': 'text/html'} 53 | }) 54 | } 55 | 56 | 57 | self.addEventListener('activate', event => event.waitUntil(self.clients.claim())); 58 | 59 | --------------------------------------------------------------------------------