reload
Automatically refresh and reload your code in your browser when your code changes. No browser plugins required.
Table Of Contents
Why?
Restarting your HTTP server and refreshing your browser is annoying.
How does it work?
Reload works in two different ways depending on if you're using it:
- In an existing Express application in which it creates a server side route for reload or,
- As a command line tool which starts its own Express application to monitor the file you're editing for changes and to serve
reload-client.js
to the browser.
Once reload-server and reload-client are connected, the client side code opens a WebSocket to the server and waits for the WebSocket to close, once it closes, reload waits for the server to come back up (waiting for a socket on open event), once the socket opens we reload the page.
Installation
npm install [-g] [--save-dev] reload
Two ways to use reload
There are two different ways to use reload.
- In an Express application, allowing your whole project to utilize reload when the code is altered
- As a command line application to serve up static HTML files and be able to reload when the code is altered
Using reload in Express
When used with Express reload creates a new Express route for reload. When you restart the server, the client will detect the server being restarted and automatically refresh the page.
Reload can be used in conjunction with tools that allow for automatically restarting the server such as supervisor (recommended), nodemon, forever, etc.
Express Example
server.js
:
var express = require('express')
var http = require('http')
var path = require('path')
var reload = require('reload')
var bodyParser = require('body-parser')
var logger = require('morgan')
var app = express()
var publicDir = path.join(__dirname, 'public')
app.set('port', process.env.PORT || 3000)
app.use(logger('dev'))
app.use(bodyParser.json())
app.get('/', function (req, res) {
res.sendFile(path.join(publicDir, 'index.html'))
})
var server = http.createServer(app)
reload(app).then(function () {
server.listen(app.get('port'), function () {
console.log('Web server listening on port ' + app.get('port'))
})
}).catch(function (err) {
console.error('Reload could not start, could not start server/sample app', err)
})
public/index.html
:
<!doctype html>
<html>
<head>
<title>Reload Express Sample App</title>
</head>
<body>
<h1>Reload Express Sample App</h1>
<script src="/reload/reload.js"></script>
</body>
</html>
Refer to the reload express sample app for this working example.
Manually firing server-side reload events
You can manually call a reload event by calling reload()
yourself. An example is shown below:
reloadServer = reload(app);
watch.watchTree(__dirname + "/public", function (f, curr, prev) {
reloadServer.reload();
});
API for Express
Reload returns a promise. The API takes a required express application and an optional options object.
Consult the migration guide for help updating reload across major versions.
Parameters
Table of reload parameters
Parameter Name | Type | Description | Optional |
---|
app | object | The app. It may work with other frameworks, or even with Connect. At this time, it's only been tested with Express. | |
opts | object | An optional object of options for reload. Refer to table below on possible options | ✓ |
Table of options for reload opts parameter
Parameter Name | Type | Description | Optional | Default |
---|
port | number | Port to run reload on. | ✓ | 9856 |
webSocketServerWaitStart | boolean | When enabled will delay starting and opening WebSocket server when requiring reload. After enabling use the startWebSocketServer function returned in the object provided by the API to start the WebSocket. Note: Failing to call the returned function with this option enabled will cause reload not to work. See return API for more information | ✓ | FALSE |
route | string | Route that reload should use to serve the client side script file. Changing the route will require the script tag URL to change. Reload will always strip any occurrence of reload.js and append reload.js for you. This is to ensure case, order, and use of / is correct. For example specifying newRoutePath as the route will give reload a route of newRoutePath/reload.js. (Recommend not modifying). | ✓ | reload |
forceWss | boolean | Forces reload client connections to always use wss (secure websockerts) even when the window location is HTTP | ✓ | FALSE |
https | object | HTTP options object. When defined runs reload in HTTPS mode | ✓ | {} |
https.certAndKey | object | Object that holds configuration for HTTPS key and cert configuration | ✓ | {} |
https.certAndKey.key | string | File path to HTTP key (not optional when defining an HTTPS object) | | null |
https.certAndKey.cert | string | File path to HTTP cert (not optional when defining an HTTPS object) | | null |
https.p12 | object | Object that holds configuration for HTTPS P12 configuration | ✓ | {} |
https.p12.p12Path | string | File path or file contents as string (Not optional when using P12 configuration | | null |
https.p12.passphrase | string | P12 passphrase | ✓ | null |
verbose | boolean | If set to true, will show logging on the server and client side. | ✓ | FALSE |
Returns
An object containing:
Name | Type | Description |
---|
reload | function | A function that when called reloads all connected clients. For more information see manually firing server-side reload events. |
startWebSocketServer | function | Returns a promise. Starts and opens the WebSocket server required for reload. Only defined when using the optional parameter webSocketServerWaitStart . Read the parameters for more information |
closeServer | function | Returns a promise. Closes Reload WebSocket server |
wss | object | Web socket server |
Using reload as a command line application
There are two ways to use the command line application.
- In a directory serving blank static HTML files or
- In a project with a
package.json
file
Each will require different modes of installing.
In case one you should install reload globally with npm install reload -g
. Also with reload installed globally you can go to any directory with an HTML file and use the command reload to constantly watch it and reload it while you make changes.
In case two you should install locally with npm install --save-dev
, since this tool is to aid in development you should install it as a dev dependency.
Navigate to your html directory:
reload -b
This will open your index.html
file in the browser. Any changes that you make will now reload in the browser. You don't need to modify your HTML at all.
Usage for Command Line Application
Usage: reload [options]
Options:
-h, --help output usage information
-V, --version output the version number
-b, --browser Open in the browser automatically.
-n, --hostname [hostname] If -b flag is being used, this allows for custom hostnames. Defaults to localhost.
-d, --dir [dir] The directory to serve up. Defaults to current dir.
-w, --watch-dir [watch-dir] The directory to watch. Defaults the serving directory.
-e, --exts [extensions] Extensions separated by commas or pipes. Defaults to html,js,css.
-p, --port [port] The port to bind to. Can be set with PORT env variable as well. Defaults to 8080
-s, --start-page [start-page] Specify a start page. Defaults to index.html
-f, --fallback [fallback] Fallback to the start page when route is not found
-v, --verbose [verbose] Turning on logging on the server and client side. Defaults to false
License
(MIT License)
Copyright 2019
Orginal Author:
JP Richardson jprichardson@gmail.com
Owned by:
Alexander J. Lallier mralexlallier@gmail.com
Version 3.0.0
Consult Migration Guide for help with updating from Version 2.x to 3.x
Breaking/Removed
- Removed deprecated parameters (Reload no longer takes the server argument and will error if you provide it)
- Removed support for Node versions 4, 5, 6, 7, 8, and 9
Breaking/Added
- Reload now returns a promise
- Functions in the return API also return promises
closerServer
startWebSocketServer
- Reload returns errors in promises
Added
- Added unit tests. (See: https://github.com/alallier/reload/issues/42)
- Added coverage analyzer
- Coverage 100% on
reload.js
file
- Added node 10 and 11 to the official supported list
- Added support for HTTPS
- Cert and Key or PFX/P12
- Note: This was available in version 1 and then was dropped in Version 2
- Added sample app README (See: https://github.com/alallier/reload/issues/45)
- Added MIGRATION_GUIDE to help with migrating across major versions of reload
- Added force wss option
- CI jobs now use npm ci isntall (See: https://github.com/alallier/reload/issues/158)
Changed
Closed these issues
- HTTPS - https://github.com/alallier/reload/issues/143
- Unit tests - https://github.com/alallier/reload/issues/42
- Document Fallback flag - https://github.com/alallier/reload/issues/169
- Drop Node 4 support - https://github.com/alallier/reload/issues/156
- Documentation on how to use sample app - https://github.com/alallier/reload/issues/45
- Use npm ci install - https://github.com/alallier/reload/issues/158
2.4.0 / 2018-12-02
- Added new
-f
or --fallback
command-line flag. See: MR https://github.com/alallier/reload/pull/167. Issue: https://github.com/alallier/reload/issues/164 - Allow HTML pages to be routed with
.html
. See: MR https://github.com/alallier/reload/pull/167. Issue: https://github.com/alallier/reload/issues/166
2.3.1 / 2018-08-06
- Fixed url-parse vulnerability. See: https://github.com/alallier/reload/pull/160
2.3.0 / 2018-06-11
- Added wss to return API. See: https://github.com/alallier/reload/pull/148
- Added watch flag to command line. See: https://github.com/alallier/reload/pull/155
- Security updates
- Updated finalhandler from
~1.0.3
to ~1.1.1
. See: https://github.com/alallier/reload/pull/154 - Replaced open with opn. See: https://github.com/alallier/reload/pull/154
- Updated serve-static from
~1.12.3
to 1.13.2
. See: https://github.com/alallier/reload/pull/154 - Updated ws from
~3.0.0
to ~5.2.0
. See: https://github.com/alallier/reload/commit/3310a66f80e04e48247e5c2ca4a2f4f12780294f - Updated standard from
^10.0.2
to ^11.0.1
. See: https://github.com/alallier/reload/commit/073e91b33a00dcb37c7eb5fa7601cd71f7ea34e9
2.2.2 / 2017-08-20
Fixed bug that caused HTML files to not be served when using directory flag (reload command line). See: https://github.com/alallier/reload/pull/139
2.2.1 / 2017-08-05
Fixed regression causing reload command line to only serve HTML files. See: https://github.com/alallier/reload/pull/134
2.2.0 / 2017-07-27
- Dropped express as a dependency (in reload command line). Reload now uses a vanilla node http server to achieve the same result. This update for the command line offers no changes to the end user and simply modifies the underlying code. See: https://github.com/alallier/reload/pull/132
2.1.0 / 2017-07-25
- Added the ability to have the WebSocket server start wait. (For more information read about the
webSocketServerWaitStart
parameter) See: https://github.com/alallier/reload/pull/130
2.0.1 / 2017-07-20
- Adjusted wording in README. See: https://github.com/alallier/reload/pull/123
- Removed MacOS Travis Building. See: https://github.com/alallier/reload/pull/125
- Fixed bug when running command line on port 80. See: https://github.com/alallier/reload/pull/128
2.0.0 / 2017-07-09
See V2.0.0 PR https://github.com/alallier/reload/pull/118
Added
- Added object based parameters (Issue #77 / Originally solved in PR #101 and refactored in #104)
- Added port configuration (Issue #60 / Originally solved in PR #68 and refactored in #104)
- Added timestamp to reload command line reloading (Issue #7 / PR #78)
- Added node 8 support (Issue #106 / PR #119)
- Added table of contents to README (Issue #103 / PR #105)
- Added return API to README (PR #121)
Modified
- Abstracted reload call to an index.js file. Index file now calls
reload.js
source file. This is to abstract the reload command line calling with a third argument that is now private and not apart of the public API (PR #117) - Update dependencies to latest and add package-lock.json files (PR #109)
- Audited and refactored return API (Issue #120 / PR #121)
Removed
- Drop support for server and just use ports (Issue #102 / PR #104)
- Removed support of node 0.1 and 0.12 (Issue #73 / PR #86)
- Separate server and app initialization into two parts. (This was originally fixed in PR #71 but was reversed in PR #104 when the decision to drop server was made.)
API Breaking Changes
This version makes breaking changes to the reload API. The only required argument to reload now is app
. This makes reload a lot easier to use. Reload takes a maximum of two arguments app
and an opts
(options) object with the following optional parameters, port
, route
, and verbose
. Reload runs on default port 9856
unless otherwise specified in the opts
object.
How to upgrade from Version 1 to Version 2
Before Version 2 reload always attached to your server's port by passing the server in a argument to reload. We have now dropped support for server and reload runs on ports only. Reload now has one required parameter app
and one optional parameter opts
an object of reload options. Below are two upgrade examples for the only two possible 1.x configurations.
Upgrade with required arguments: reload(server, app)
becomes reload(app)
Upgrade with both required arguments and the one optional argument: reload(server, app, true)
becomes reload(app, {verbose: true})
It is important to note that reload only uses ports now. So upgrading using the examples above will have reload run on it's default port 9856
. If you want to run reload on a different port you need to specify a port in the opts
object like: reload(app, {port: 9852})
Most people can just use the default settings, allowing reload(app)
to work in most cases.
Please refer to the full API in the README.
1.1.7 / 2017-06-28
Repository ownership was transfered from jprichardson to alallier
- Updated Travis badge after ownership change. See: https://github.com/alallier/reload/pull/116
- Updated AppVeyor badge after ownership change. See: https://github.com/alallier/reload/pull/112
- Updated README after owernship change. See: https://github.com/alallier/reload/pull/115
- Changed style of npm badge to match the others. See: https://github.com/alallier/reload/pull/111
1.1.6 / 2017-06-18
Add Mac building in Travis. See https://github.com/jprichardson/reload/pull/98
1.1.5 / 2017-05-13
Fixed standard call so that our bin file also got tested. See https://github.com/jprichardson/reload/pull/85
1.1.4 / 2017-05-13
Added AppVeyor to build our tests in an Windows environment. See https://github.com/jprichardson/reload/pull/92
1.1.3 / 2017-04-28
Upgrade Standard to ~10.0.2
in order for the build to pass node 0.1
and 0.12
Also removed depricated fs.exists
and replaced with fs.access
See: https://github.com/jprichardson/reload/pull/75
1.1.2 / 2017-04-16
Fix multiple websockets at once when using reload.reload(); See: https://github.com/jprichardson/reload/pull/57
1.1.1 / 2017-01-28
Fixed undefined error log on send message. See: https://github.com/jprichardson/reload/pull/59
1.1.0 / 2016-11-12
Added client end web socket support for https. See: https://github.com/jprichardson/reload/pull/54
1.0.2 / 2016-10-31
Added error handling to websocket send. See: https://github.com/jprichardson/reload/pull/49
1.0.1 / 2016-07-15
Fixed onbeforeunload event not firing in reload-client See: https://github.com/jprichardson/reload/pull/46
1.0.0 / 2016-06-24
Added
- Two new badges to the README (code-style and npm version)
- Verbose mode as option for both Express and command line usage
- A sample app for express
Modified
- Re-wrote the README to reflect all of these changes
- Updated dependencies to their latest version’s
- Fixed race condition that caused reload to spam the server when using sockets for automatic reloading
Removed
- All delays (wait, normal, and socket) (Reload is now all automatic using web sockets (no delays at all))
- Client side sockjs web sockets (removed sockjs) (Now using native web sockets on the client side and ws on server side)
See: https://github.com/jprichardson/reload/pull/41
0.8.2 / 2016-06-24
- Fixed regression caused by Windows line endings. See: https://github.com/jprichardson/reload/pull/40
0.8.1 / 2016-06-05
- Allow reload from node server. See: https://github.com/jprichardson/reload/pull/38
0.8.0 / 2015-12-21
- fixed
hostname
flag. See: https://github.com/jprichardson/reload/pull/34 - use
exts
from command line. See: https://github.com/jprichardson/reload/pull/32
0.7.0 / 2015-10-21
- fixed
wait
flag: https://github.com/jprichardson/reload/pull/27
0.6.0 / 2015-10-12
- added
hostname/ -h
flag. See: https://github.com/jprichardson/reload/issues/14 and https://github.com/jprichardson/reload/pull/28
0.5.0 / 2015-09-28
- renamed
delay
flag to reloadDelay
. See: https://github.com/jprichardson/reload/pull/26 - added
wait
flag. See: https://github.com/jprichardson/reload/pull/26
0.4.0 / 2015-08-17
- add
true
option to delay
so that it waits indefinitely until server is up https://github.com/jprichardson/reload/pull/21 - express 4 routes, https://github.com/jprichardson/reload/pull/24
0.3.0 / 2015-07-17
- added option for start page. See: https://github.com/jprichardson/reload/pull/20
0.2.0 / 2015-06-29
0.1.0 / 2013-09-30
- silence sockjs
- created
reload
bin that is useful for browser/html development
0.0.2 / 2013-03-14
- fixed bug that caused failure on hashbang urls
- set proper mime type on reload.js client side script
0.0.1 / 2013-03-13