Skip to content

Commit

Permalink
Add option to disable live reload server during development (#766)
Browse files Browse the repository at this point in the history
* add option to disable live reload server

* change live to watch, fix incorrect argument parsing code

* Update api-commands.md
  • Loading branch information
=^._.^= authored and yangshun committed Jun 15, 2018
1 parent 608e2c8 commit 62a2c7c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
1 change: 1 addition & 0 deletions docs/api-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ This script will build the static website, apply translations if necessary, and
| Options | Default | Description |
| ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `--port <number>` | `3000` | The website will be served from port 3000 by default, but if the port is taken up, Docusaurus will attempt to find an available one. |
| `--watch` | - | Whether to watch the files and live reload the page when files are changed. Defaults to true. Disable this by using `--no-watch`. |

---

Expand Down
59 changes: 31 additions & 28 deletions lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

function execute(port) {
function execute(port, options) {
const extractTranslations = require('../write-translations');

const metadataUtils = require('./metadataUtils');
Expand Down Expand Up @@ -568,35 +568,38 @@ function execute(port) {
});
});

// Start LiveReload server.
process.env.NODE_ENV = 'development';
const server = tinylr();
server.listen(constants.LIVE_RELOAD_PORT, function() {
console.log(
'LiveReload server started on port %d',
constants.LIVE_RELOAD_PORT
);
});
if (options.watch) startLiveReload();
app.listen(port);

// gaze watches some specified dirs and triggers a callback when they change.
gaze(
[
'../' + readMetadata.getDocsPath() + '/**/*', // docs
'**/*', // website
'!node_modules/**/*', // node_modules
],
function() {
// Listen for all kinds of file changes - modified/added/deleted.
this.on('all', function() {
// Notify LiveReload clients that there's a change.
// Typically, LiveReload will only refresh the changed paths,
// so we use / here to force a full-page reload.
server.notifyClients(['/']);
});
}
);
function startLiveReload() {
// Start LiveReload server.
process.env.NODE_ENV = 'development';
const server = tinylr();
server.listen(constants.LIVE_RELOAD_PORT, function() {
console.log(
'LiveReload server started on port %d',
constants.LIVE_RELOAD_PORT
);
});

app.listen(port);
// gaze watches some specified dirs and triggers a callback when they change.
gaze(
[
'../' + readMetadata.getDocsPath() + '/**/*', // docs
'**/*', // website
'!node_modules/**/*', // node_modules
],
function() {
// Listen for all kinds of file changes - modified/added/deleted.
this.on('all', function() {
// Notify LiveReload clients that there's a change.
// Typically, LiveReload will only refresh the changed paths,
// so we use / here to force a full-page reload.
server.notifyClients(['/']);
});
}
);
}
}

module.exports = execute;
7 changes: 5 additions & 2 deletions lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) {

const program = require('commander');

program.option('--port <number>', 'Specify port number').parse(process.argv);
program
.option('--port <number>', 'Specify port number')
.option('--no-watch', 'Toggle live reload file watching')
.parse(process.argv);

let port = parseInt(program.port, 10) || process.env.PORT || 3000;
let numAttempts = 0;
Expand All @@ -68,7 +71,7 @@ function checkPort() {
} else {
// start local server on specified port
const server = require('./server/server.js');
server(port);
server(port, program.opts());
const host = `http://localhost:${port}`;
console.log('Docusaurus server started on port %d', port);
openBrowser(host);
Expand Down

0 comments on commit 62a2c7c

Please sign in to comment.