diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index 820d26e7de9a..8ae3d348ecf6 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -148,6 +148,10 @@ cli .option('-p, --port ', 'use specified port (default: 3000)') .option('--build', 'build website before serving (default: false)') .option('-h, --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), ); diff --git a/packages/docusaurus/src/commands/serve.ts b/packages/docusaurus/src/commands/serve.ts index 7777d448f734..0eb24b677f1b 100644 --- a/packages/docusaurus/src/commands/serve.ts +++ b/packages/docusaurus/src/commands/serve.ts @@ -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'; @@ -19,6 +20,7 @@ export type ServeCLIOptions = HostPortOptions & Pick & { dir?: string; build?: boolean; + open?: boolean; }; export async function serve( @@ -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); + } } diff --git a/website/docs/cli.md b/website/docs/cli.md index e19840601722..c1b79ddd9d87 100644 --- a/website/docs/cli.md +++ b/website/docs/cli.md @@ -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}