Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow different host through CLI flag #1475

Merged
merged 7 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ This command 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. |
|`--host <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.:
Expand Down
6 changes: 4 additions & 2 deletions packages/docusaurus-1.x/lib/server/liveReloadServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-1.x/lib/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 === '/'
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions packages/docusaurus-1.x/lib/server/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-1.x/lib/start-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ if (env.versioning.enabled && env.versioning.missingVersionsPage) {
program
.option('--port <number>', 'Specify port number')
.option('--no-watch', 'Toggle live reload file watching')
.option('--host <host>', 'use specified host (default: localhost)')
.parse(process.argv);

startDocusaurus().catch(ex => {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down