Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix dev locale file serving (#1330)

Graham Neubig48be1de389bf
diff --git a/vite.config.ts b/vite.config.ts
index 31d83e086f..d52f5d812d 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -2,7 +2,8 @@
/// <reference types="vite-plugin-svgr/client" />
import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
-import { resolve, dirname } from "node:path";
+import { readFile } from "node:fs/promises";
+import { resolve, dirname, relative, isAbsolute } from "node:path";
import { defineConfig, loadEnv } from "vite";
import svgr from "vite-plugin-svgr";
import { reactRouter } from "@react-router/dev/vite";
@@ -33,6 +34,7 @@ const EXTENSIONS_SKILLS_DIR = resolve(
dirname(_require.resolve("@openhands/extensions/package.json")),
"skills",
);
+const PUBLIC_LOCALES_DIR = resolve(process.cwd(), "public", "locales");
const appBuildConfig = {
rolldownOptions: {
@@ -87,12 +89,57 @@ export default defineConfig(({ mode }) => {
server.middlewares.use(
"/.well-known/appspecific/com.chrome.devtools.json",
(_req, res) => {
- res.statusCode = 204;
+ res.writeHead(204);
res.end();
},
);
},
},
+ {
+ name: "serve-generated-i18n-locales",
+ apply: "serve",
+ configureServer(server) {
+ server.middlewares.use(async (req, res, next) => {
+ const method = req.method ?? "GET";
+ if (method !== "GET" && method !== "HEAD") {
+ next();
+ return;
+ }
+
+ const pathname = new URL(req.url ?? "", "http://localhost")
+ .pathname;
+ if (
+ !pathname.startsWith("/locales/") ||
+ !pathname.endsWith(".json")
+ ) {
+ next();
+ return;
+ }
+
+ const requestedPath = decodeURIComponent(
+ pathname.slice("/locales/".length),
+ );
+ const filePath = resolve(PUBLIC_LOCALES_DIR, requestedPath);
+ const relativePath = relative(PUBLIC_LOCALES_DIR, filePath);
+ if (relativePath.startsWith("..") || isAbsolute(relativePath)) {
+ res.writeHead(403);
+ res.end();
+ return;
+ }
+
+ try {
+ const content = await readFile(filePath);
+ res.writeHead(200, {
+ "Content-Type": "application/json; charset=utf-8",
+ "Cache-Control": "no-cache",
+ });
+ res.end(method === "HEAD" ? "" : content);
+ } catch {
+ next();
+ }
+ });
+ },
+ },
!process.env.VITEST && !isLibraryBuild && reactRouter(),
svgr(),
tailwindcss(),