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: make docusaurus serve automatically open in browser #7500

Merged
merged 5 commits into from
May 27, 2022
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
4 changes: 4 additions & 0 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ cli
.option('-p, --port <port>', 'use specified port (default: 3000)')
.option('--build', 'build website before serving (default: false)')
.option('-h, --host <host>', 'use specified host (default: localhost)')
.option(
'--no-open',
'do not open page in the browser (default: false, or true in CI)',
)
.action(async (siteDir, options) =>
serve(await resolveDir(siteDir), options),
);
Expand Down
11 changes: 8 additions & 3 deletions packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path from 'path';
import logger from '@docusaurus/logger';
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
import serveHandler from 'serve-handler';
import openBrowser from 'react-dev-utils/openBrowser';
import {loadSiteConfig} from '../server/config';
import {build} from './build';
import {getHostPort, type HostPortOptions} from '../server/getHostPort';
Expand All @@ -19,6 +20,7 @@ export type ServeCLIOptions = HostPortOptions &
Pick<LoadContextOptions, 'config'> & {
dir?: string;
build?: boolean;
open?: boolean;
};

export async function serve(
Expand Down Expand Up @@ -76,8 +78,11 @@ export async function serve(
});
});

logger.success`Serving path=${buildDir} directory at: url=${
servingUrl + baseUrl
}`;
const url = servingUrl + baseUrl;
logger.success`Serving path=${buildDir} directory at: url=${url}`;
server.listen(port);

if (cliOptions.open && !process.env.CI) {
openBrowser(url);
}
}
1 change: 1 addition & 0 deletions website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Serve your built website locally.
| `--build` | `false` | Build website before serving |
| `--config` | `undefined` | Path to docusaurus config file, default to `[siteDir]/docusaurus.config.js` |
| `--host` | `localhost` | Specify a host to use. For example, if you want your server to be accessible externally, you can use `--host 0.0.0.0`. |
| `--no-open` | `false` locally, `true` in CI | Do not open a browser window to the server location. |

### `docusaurus clear [siteDir]` {#docusaurus-clear-sitedir}

Expand Down