Is there a way to have Rewrites or Proxy? #2399
-
This might be a dumb question, but I couldn't find any docs for it, so I might as well ask. My team is moving away from nextjs (pages) and moving to TSR (tanstack router with start/vinxi). The reason this is a blocker for us is that we need to have the API set cookies from the same domain as the UI (due to multiple envs, legacy auth issue and others). FWIW, i tried the vite server proxy, but it seems that tanstack router intercepts the request before vite proxy can (assumption). In our // nextjs.config.ts
{
rewrites() {
return [
{ source: "/api/avatar/:path*", destination: `${API_URL}/members/:path*/avatar` },
{ source: "/api/:path*", destination: `${API_URL}/:path*` },
];
},
} We played around a bit and have a very hacky solution which is very obnoxious. We do send headers and other through, but not in this example. // routes/api/$.ts
// ...
function constructUrl(splat: any) {
return `${getApiBaseUrl()}/${
Array.isArray(splat) ? splat.join("/") : typeof splat === "object" ? Object.values(splat).join("/") : splat
}`;
}
const createResponse = (data: any, headers: Headers, status: number) =>
new Response(JSON.stringify(data), { headers, status });
export const Route = createAPIFileRoute("/api/$")({
GET: async ({ params }) => {
const { _splat } = params;
const url = constructUrl(_splat);
const res = await api.get(url);
return createResponse(res.data, res.headers, res.status);
},
POST: async ({ params, request }) => {
const { _splat } = params;
const body = await request.json();
const url = constructUrl(_splat);
const res = await api.post(url, body);
return createResponse(res.data, res.headers, res.status);
},
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
as of v1.58.7, we have the following: TanStack Start now exposes the example: import { defineConfig } from '@tanstack/start/config'
export default defineConfig({
server: {
routeRules: {
"/proxy/example": { proxy: { to: "https://example.com"} },
}
},
}) |
Beta Was this translation helpful? Give feedback.
as of v1.58.7, we have the following:
TanStack Start now exposes the
routeRules
config option from nitro: https://nitro.unjs.io/guide/routing#route-rulesexample: