diff --git a/starlight_help/astro.config.mjs b/starlight_help/astro.config.mjs index d19f6b1b25..2dbe98744d 100644 --- a/starlight_help/astro.config.mjs +++ b/starlight_help/astro.config.mjs @@ -11,6 +11,7 @@ import Icons from "unplugin-icons/vite"; * @returns {import("vite").PluginOption} */ function createRedirectPlugin() { + const proxyPort = process.env.ZULIP_WEB_APP_PROXY_PORT || "9991"; // Astro and starlight middlewares run after astro's vite middleware, // which gives error before our logic here could run, so the only option // left with us was to use a vite plugin. @@ -33,10 +34,10 @@ function createRedirectPlugin() { route: "", /** * @param {import("http").IncomingMessage} req - * @param {import("http").ServerResponse} _res + * @param {import("http").ServerResponse} res * @param {Function} next */ - handle(req, _res, next) { + handle(req, res, next) { // Canonical URL for the root of the help center is /help/, // but for all other help URLs, there should be no trailingSlash. // We have set trailingSlash to never in astro. Setting it to ignore @@ -46,6 +47,20 @@ function createRedirectPlugin() { if (req.url === "/help/") { req.url = "/help"; } + + // Help center dev server always runs on a port different than + // the web app. We have relative URLs pointing to the web app + // in the help center, but they are not on the port help center + // is running on. We redirect here to our web app proxy port. + if (req.url && !req.url.startsWith("/help")) { + const host = req.headers.host || "localhost"; + const redirectUrl = new URL(req.url, `http://${host}`); + redirectUrl.port = proxyPort; + res.writeHead(302, {Location: redirectUrl.toString()}); + res.end(); + return; + } + next(); }, }); diff --git a/tools/run-dev b/tools/run-dev index 44a472024e..934fd77791 100755 --- a/tools/run-dev +++ b/tools/run-dev @@ -306,6 +306,9 @@ async def forward(upstream_port: int, request: web.Request) -> web.StreamRespons def run_help_center_dev_server( external_host: str, port: int = help_center_port ) -> "subprocess.Popen[bytes]": + env = os.environ.copy() + env["ZULIP_WEB_APP_PROXY_PORT"] = str(proxy_port) + return subprocess.Popen( [ "/usr/local/bin/corepack", @@ -316,6 +319,7 @@ def run_help_center_dev_server( f"--allowed-hosts={urlsplit(external_host).hostname}", ], cwd="starlight_help", + env=env, )