diff --git a/docs/api-commands.md b/docs/api-commands.md index 09bd87a5952b..42ae1ed8fb17 100644 --- a/docs/api-commands.md +++ b/docs/api-commands.md @@ -146,6 +146,7 @@ This command will build the static website, apply translations if necessary, and | Options | Default | Description | | ----------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------ | | `--port ` | `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. | +|`--host `|`localhost`|Specify a host to use. E.g., if you want your server to be accessible externally, you can use --host 0.0.0.0.| | `--watch` | - | Whether to watch the files and live reload the page when files are changed. Defaults to true. Disable this by using `--no-watch`. | You can specify the browser application to be opened by setting the `BROWSER` environment variable before the command, e.g.: diff --git a/packages/docusaurus-1.x/lib/server/liveReloadServer.js b/packages/docusaurus-1.x/lib/server/liveReloadServer.js index 9f17e9208cb7..4f64462345d7 100644 --- a/packages/docusaurus-1.x/lib/server/liveReloadServer.js +++ b/packages/docusaurus-1.x/lib/server/liveReloadServer.js @@ -7,6 +7,7 @@ const gaze = require('gaze'); const tinylr = require('tiny-lr'); +const program = require('commander'); const readMetadata = require('./readMetadata.js'); function start(port) { @@ -26,10 +27,11 @@ function start(port) { }, ); } - const getReloadScriptUrl = () => { const port = process.env.LIVERELOAD_PORT; - return `http://localhost:${port}/livereload.js`; + const host = program.host || 'localhost'; + + return `http://${host}:${port}/livereload.js`; }; module.exports = { diff --git a/packages/docusaurus-1.x/lib/server/server.js b/packages/docusaurus-1.x/lib/server/server.js index 4273e988f370..d6288e595dcb 100644 --- a/packages/docusaurus-1.x/lib/server/server.js +++ b/packages/docusaurus-1.x/lib/server/server.js @@ -7,7 +7,7 @@ /* eslint-disable no-cond-assign */ -function execute(port) { +function execute(port, host) { const extractTranslations = require('../write-translations'); const metadataUtils = require('./metadataUtils'); const blog = require('./blog'); @@ -355,7 +355,7 @@ function execute(port) { // for example, request to "blog" returns "blog/index.html" or "blog.html" app.get(routing.noExtension(), (req, res, next) => { const slash = req.path.toString().endsWith('/') ? '' : '/'; - const requestUrl = `http://localhost:${port}${req.path}`; + const requestUrl = `http://${host}:${port}${req.path}`; requestFile(`${requestUrl + slash}index.html`, res, () => { requestFile( slash === '/' @@ -374,7 +374,7 @@ function execute(port) { next(); return; } - requestFile(`http://localhost:${port}${req.path}.html`, res, next); + requestFile(`http://${host}:${port}${req.path}.html`, res, next); }); app.listen(port); diff --git a/packages/docusaurus-1.x/lib/server/start.js b/packages/docusaurus-1.x/lib/server/start.js index 626b03e4a11d..08ba69d5044c 100644 --- a/packages/docusaurus-1.x/lib/server/start.js +++ b/packages/docusaurus-1.x/lib/server/start.js @@ -23,12 +23,13 @@ function startLiveReloadServer() { function startServer() { const initialServerPort = parseInt(program.port, 10) || process.env.PORT || 3000; + const host = program.host || 'localhost'; const promise = portFinder .getPortPromise({port: initialServerPort}) .then(port => { - server(port); + server(port, host); const {baseUrl} = require(`${CWD}/siteConfig.js`); - const serverAddress = `http://localhost:${port}${baseUrl}`; + const serverAddress = `http://${host}:${port}${baseUrl}`; console.log('Docusaurus server started on port %d', port); openBrowser(serverAddress); }); diff --git a/packages/docusaurus-1.x/lib/start-server.js b/packages/docusaurus-1.x/lib/start-server.js index 578d310abe4b..d33e4922e6a7 100755 --- a/packages/docusaurus-1.x/lib/start-server.js +++ b/packages/docusaurus-1.x/lib/start-server.js @@ -42,6 +42,7 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) { program .option('--port ', 'Specify port number') .option('--no-watch', 'Toggle live reload file watching') + .option('--host ', 'use specified host (default: localhost)') .parse(process.argv); startDocusaurus().catch(ex => { diff --git a/website/docs/cli.md b/website/docs/cli.md index c91a33bed1ec..3559d5eaefa7 100644 --- a/website/docs/cli.md +++ b/website/docs/cli.md @@ -46,7 +46,7 @@ Builds and serves the static site with [Webpack Dev Server](https://webpack.js.o |Options|Default|Description| |-|-|-| |`--port`|`3000`|Specifies the port of the dev server| -|`--host`|`localhost`|Specifie a host to use. E.g., if you want your server to be accessible externally, you can use `--host 0.0.0.0`| +|`--host`|`localhost`|Specify a host to use. E.g., if you want your server to be accessible externally, you can use `--host 0.0.0.0`| |`--hot-only`|`false`|Enables Hot Module Replacement without page refresh as fallback in case of build failures. More information [here](https://webpack.js.org/configuration/dev-server/#devserverhotonly).| ### `docusaurus build`