mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-06 10:20:09 +02:00
fix(core): docusaurus serve
redirects should include the site /baseUrl/
prefix (#10090)
This commit is contained in:
parent
0b49e6c5a2
commit
3ee776050e
1 changed files with 21 additions and 4 deletions
|
@ -12,11 +12,19 @@ import logger from '@docusaurus/logger';
|
||||||
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
|
import {DEFAULT_BUILD_DIR_NAME} from '@docusaurus/utils';
|
||||||
import serveHandler from 'serve-handler';
|
import serveHandler from 'serve-handler';
|
||||||
import openBrowser from 'react-dev-utils/openBrowser';
|
import openBrowser from 'react-dev-utils/openBrowser';
|
||||||
|
import {applyTrailingSlash} from '@docusaurus/utils-common';
|
||||||
import {loadSiteConfig} from '../server/config';
|
import {loadSiteConfig} from '../server/config';
|
||||||
import {build} from './build';
|
import {build} from './build';
|
||||||
import {getHostPort, type HostPortOptions} from '../server/getHostPort';
|
import {getHostPort, type HostPortOptions} from '../server/getHostPort';
|
||||||
import type {LoadContextParams} from '../server/site';
|
import type {LoadContextParams} from '../server/site';
|
||||||
|
|
||||||
|
function redirect(res: http.ServerResponse, location: string) {
|
||||||
|
res.writeHead(302, {
|
||||||
|
Location: location,
|
||||||
|
});
|
||||||
|
res.end();
|
||||||
|
}
|
||||||
|
|
||||||
export type ServeCLIOptions = HostPortOptions &
|
export type ServeCLIOptions = HostPortOptions &
|
||||||
Pick<LoadContextParams, 'config'> & {
|
Pick<LoadContextParams, 'config'> & {
|
||||||
dir?: string;
|
dir?: string;
|
||||||
|
@ -62,15 +70,24 @@ export async function serve(
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
// Automatically redirect requests to /baseUrl/
|
// Automatically redirect requests to /baseUrl/
|
||||||
if (!req.url?.startsWith(baseUrl)) {
|
if (!req.url?.startsWith(baseUrl)) {
|
||||||
res.writeHead(302, {
|
redirect(res, baseUrl);
|
||||||
Location: baseUrl,
|
return;
|
||||||
});
|
}
|
||||||
res.end();
|
|
||||||
|
// We do the redirect ourselves for a good reason
|
||||||
|
// server-handler is annoying and won't include /baseUrl/ in redirects
|
||||||
|
const normalizedUrl = applyTrailingSlash(req.url, {trailingSlash, baseUrl});
|
||||||
|
if (req.url !== normalizedUrl) {
|
||||||
|
redirect(res, normalizedUrl);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove baseUrl before calling serveHandler, because /baseUrl/ should
|
// Remove baseUrl before calling serveHandler, because /baseUrl/ should
|
||||||
// serve /build/index.html, not /build/baseUrl/index.html (does not exist)
|
// serve /build/index.html, not /build/baseUrl/index.html (does not exist)
|
||||||
|
// Note server-handler is really annoying here:
|
||||||
|
// - no easy way to do rewrites such as "/baseUrl/:path" => "/:path"
|
||||||
|
// - no easy way to "reapply" the baseUrl to the redirect "Location" header
|
||||||
|
// See also https://github.com/facebook/docusaurus/pull/10090
|
||||||
req.url = req.url.replace(baseUrl, '/');
|
req.url = req.url.replace(baseUrl, '/');
|
||||||
|
|
||||||
serveHandler(req, res, {
|
serveHandler(req, res, {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue