├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | # after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # choo-reload [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Asset reloading package for choo. Reloads CSS, refreshes the page on JS. 6 | 7 | ## Usage 8 | ```js 9 | var reload = require('choo-reload') 10 | var choo = require('choo') 11 | 12 | var app = choo() 13 | app.use(reload()) 14 | app.mount('body') 15 | ``` 16 | 17 | ## FAQ 18 | ### Why not replace all state on JS? 19 | Because you're bound to have memory leaks. Figuring out how to properly close 20 | all handlers is tricky - but perhaps we'll eventually get around to solving 21 | this (probably major version). Until then: here's the second best thing. 22 | 23 | ## API 24 | ### `reload([url])` 25 | Create a new instance of reload. `url` defaults to `sse`. 26 | 27 | ## License 28 | [MIT](https://tldrlegal.com/license/mit-license) 29 | 30 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 31 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 32 | [2]: https://img.shields.io/npm/v/choo-reload.svg?style=flat-square 33 | [3]: https://npmjs.org/package/choo-reload 34 | [4]: https://img.shields.io/travis/yoshuawuyts/choo-reload/master.svg?style=flat-square 35 | [5]: https://travis-ci.org/yoshuawuyts/choo-reload 36 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/choo-reload/master.svg?style=flat-square 37 | [7]: https://codecov.io/github/yoshuawuyts/choo-reload 38 | [8]: http://img.shields.io/npm/dm/choo-reload.svg?style=flat-square 39 | [9]: https://npmjs.org/package/choo-reload 40 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 41 | [11]: https://github.com/feross/standard 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var logger = require('nanologger') 2 | 3 | module.exports = reload 4 | 5 | function reload (url) { 6 | url = url || '/sse' 7 | return function (state, emitter) { 8 | if (typeof window === 'undefined') return 9 | 10 | var log = logger('sse') 11 | var source = new window.EventSource(url) 12 | 13 | source.addEventListener('open', function () { 14 | log.info('connected') 15 | }) 16 | 17 | source.addEventListener('message', function (event) { 18 | try { 19 | var ev = JSON.parse(event.data) 20 | } catch (e) { 21 | return log.error('error parsing event', e) 22 | } 23 | if (ev.type === 'css') loadCss() 24 | else if (ev.type === 'js') loadJs() 25 | else log.warn('unknown', event) 26 | }, false) 27 | 28 | source.addEventListener('error', function (event) { 29 | if (event.target.readyState === window.EventSource.CLOSED) { 30 | source.close() 31 | log.info('closed') 32 | } else if (event.target.readyState === window.EventSource.CONNECTING) { 33 | log.warn('reconnecting') 34 | } else { 35 | log.error('connection closed: unknown error') 36 | } 37 | }, false) 38 | 39 | function loadJs () { 40 | log.info('javascript', 'reloading') 41 | window.location.reload() 42 | } 43 | 44 | function loadCss (content) { 45 | var node = document.createElement('style') 46 | node.setAttribute('type', 'text/css') 47 | node.textContent = content 48 | 49 | log.info('stylesheet', node) 50 | 51 | var linkNode = document.querySelector('link') 52 | if (linkNode) linkNode.parentNode.removeChild(linkNode) 53 | 54 | var prevNode = document.querySelector('style') 55 | if (prevNode) prevNode.parentNode.replaceChild(node, prevNode) 56 | else document.head.appendChild(node) 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "choo-reload", 3 | "description": "Livereloading package for choo", 4 | "repository": "yoshuawuyts/choo-reload", 5 | "version": "1.1.1", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps", 10 | "coverage": "nyc report --reporter=text-lcov > coverage.lcov" 11 | }, 12 | "dependencies": { 13 | "nanologger": "^1.0.2" 14 | }, 15 | "devDependencies": { 16 | "dependency-check": "^2.8.0", 17 | "nyc": "^10.2.0", 18 | "standard": "^9.0.2", 19 | "tape": "^4.6.3" 20 | }, 21 | "keywords": [ 22 | "choo", 23 | "bankai", 24 | "reload", 25 | "hot-module-reloading", 26 | "live" 27 | ] 28 | } 29 | --------------------------------------------------------------------------------