Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(server): dest-rewrite req.url keeps the original pathname, merging dest query

fudjiaf304fbc129cb
diff --git a/server/index.mjs b/server/index.mjs
index 0e18f69d5..b190297d0 100644
--- a/server/index.mjs
+++ b/server/index.mjs
@@ -423,15 +423,21 @@ app.use(async (req, res) => {
// Functions phase: anything routed under /api/ is a serverless handler.
if (currentPath.startsWith('/api/')) {
- // Vercel parity: a handler behind a dest rewrite sees the REWRITTEN url —
- // path and query — on req.url, not just req.query. Many handlers (e.g.
- // api/oracle-share.js, api/agent-share.js) parse new URL(req.url), so
- // without this a pretty route like /oracle/coin/<mint> →
- // /api/oracle-share?mint=$1 delivers an empty query and the page breaks.
+ // Vercel parity: a handler behind a dest rewrite sees the dest's query
+ // params on req.url (many handlers — api/oracle-share.js,
+ // api/agent-share.js — parse new URL(req.url) rather than req.query), but
+ // keeps the ORIGINAL request pathname. Using the dest path here breaks
+ // every handler behind a "[param].js"-style dest that routes by path
+ // segment: /api/marketplace/categories → dest
+ // /api/marketplace/[action]?action=categories made api/marketplace/[action].js
+ // parse the literal segment "[action]" as its action and 404
+ // (/marketplace, /skills, /economy all lost their category/theme/list
+ // calls). Merge the dest query onto the original pathname instead —
+ // both handler styles read what they expect.
if (Object.keys(extraQuery).length > 0) {
const merged = new URLSearchParams(url.search);
for (const [k, v] of Object.entries(extraQuery)) merged.set(k, v);
- req.url = `${currentPath}?${merged.toString()}`;
+ req.url = `${url.pathname}?${merged.toString()}`;
}
res.set(collected);
if (await dispatchApi(req, res, currentPath, extraQuery)) return;
diff --git a/tests/server-rewrite-query.test.js b/tests/server-rewrite-query.test.js
index 07c5c94fb..41700e9ec 100644
--- a/tests/server-rewrite-query.test.js
+++ b/tests/server-rewrite-query.test.js
@@ -73,4 +73,17 @@ describe('rewrite-dest query params reach handlers on req.url', () => {
expect([302, 307]).toContain(res.status);
expect(res.headers.get('location')).toBeTruthy();
}, 15000);
+
+ it('handlers behind a [param].js dest still see the ORIGINAL pathname on req.url', async () => {
+ // /api/marketplace/categories → dest /api/marketplace/[action]?action=categories.
+ // api/marketplace/[action].js routes by parsing req.url's path segments, so if
+ // req.url carried the dest's literal "[action]" segment the handler would 404
+ // "unknown marketplace action" (broke /marketplace, /skills, /economy). With no
+ // DATABASE_URL in this harness the healthy outcome is the handler's own 5xx
+ // degradation — never its unknown-action 404.
+ const res = await fetch(`${BASE}/api/marketplace/categories`);
+ const body = await res.text();
+ expect(body).not.toContain('unknown marketplace action');
+ expect(res.status).not.toBe(404);
+ }, 15000);
});