Skip to content

Commit

Permalink
fix(v2): respect baseUrl in serving command (#4924)
Browse files Browse the repository at this point in the history
* fix(v2): respect baseUrl in serving command

* update yarnlock

* add lighter loadSiteConfig method

* suggest another implementation

* do the cleanup

* do the cleanup

* remove useless returns

Co-authored-by: slorber <lorber.sebastien@gmail.com>
  • Loading branch information
lex111 and slorber authored Jun 9, 2021
1 parent a024d23 commit a5d3d28
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
32 changes: 28 additions & 4 deletions packages/docusaurus/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import serveHandler from 'serve-handler';
import boxen from 'boxen';
import chalk from 'chalk';
import path from 'path';

import {loadSiteConfig} from '../server';
import build from './build';
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
import {ServeCLIOptions} from '@docusaurus/types';
Expand All @@ -22,6 +22,7 @@ export default async function serve(
let dir = path.isAbsolute(cliOptions.dir)
? cliOptions.dir
: path.join(siteDir, cliOptions.dir);

if (cliOptions.build) {
dir = await build(
siteDir,
Expand All @@ -40,17 +41,40 @@ export default async function serve(
process.exit();
}

const {
siteConfig: {baseUrl},
} = await loadSiteConfig({
siteDir,
customConfigFilePath: cliOptions.config,
});

const servingUrl = `http://${cliOptions.host}:${cliOptions.port}`;

const server = http.createServer((req, res) => {
// Automatically redirect requests to /baseUrl/
if (!req.url?.startsWith(baseUrl)) {
res.writeHead(302, {
Location: baseUrl,
});
res.end();
return;
}

// Remove baseUrl before calling serveHandler
// Reason: /baseUrl/ should serve /build/index.html, not /build/baseUrl/index.html (does not exist)
req.url = req.url?.replace(baseUrl, '/');

serveHandler(req, res, {
cleanUrls: true,
public: dir,
});
});

console.log(
boxen(
`${chalk.green(`Serving ${cliOptions.dir}!`)}\n\n- Local: http://${
cliOptions.host
}:${port}`,
`${chalk.green(
`Serving "${cliOptions.dir}" directory at ${servingUrl + baseUrl}`,
)}`,
{
borderColor: 'green',
padding: 1,
Expand Down
29 changes: 22 additions & 7 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ export type LoadContextOptions = {
localizePath?: boolean; // undefined = only non-default locales paths are localized
};

export async function loadSiteConfig({
siteDir,
customConfigFilePath,
}: {
siteDir: string;
customConfigFilePath?: string;
}) {
const siteConfigPathUnresolved =
customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME;

const siteConfigPath = path.isAbsolute(siteConfigPathUnresolved)
? siteConfigPathUnresolved
: path.resolve(siteDir, siteConfigPathUnresolved);

const siteConfig = await loadConfig(siteConfigPath);
return {siteConfig, siteConfigPath};
}

export async function loadContext(
siteDir: string,
options: LoadContextOptions = {},
Expand All @@ -55,13 +73,10 @@ export async function loadContext(
? GENERATED_FILES_DIR_NAME
: path.resolve(siteDir, GENERATED_FILES_DIR_NAME);

const siteConfigPathUnresolved =
customConfigFilePath ?? DEFAULT_CONFIG_FILE_NAME;
const siteConfigPath = path.isAbsolute(siteConfigPathUnresolved)
? siteConfigPathUnresolved
: path.resolve(siteDir, siteConfigPathUnresolved);

const initialSiteConfig: DocusaurusConfig = loadConfig(siteConfigPath);
const {siteConfig: initialSiteConfig, siteConfigPath} = await loadSiteConfig({
siteDir,
customConfigFilePath,
});
const {ssrTemplate} = initialSiteConfig;

const baseOutDir = customOutDir
Expand Down

0 comments on commit a5d3d28

Please sign in to comment.