T
TanStack5mo ago
extended-salmon

Redirecting /api to homepage

Hey! I'm using TS Router (via Start) and am trying to redirect /api to the homepage. I made a routes/api/index.ts that redirects to /, but it doesn't seem to ever get hit. How can I fix this?
11 Replies
fascinating-indigo
fascinating-indigo5mo ago
how do you redirect?
extended-salmon
extended-salmonOP5mo ago
return new Response(null, {
status: 301,
headers: { Location: "/" }
})
return new Response(null, {
status: 301,
headers: { Location: "/" }
})
(I can't use redirect because of this bug)
fascinating-indigo
fascinating-indigo5mo ago
not a bug really just not supported right now however, your way should work can you provide a complete minimal example repo?
extended-salmon
extended-salmonOP5mo ago
Here's my full redirect code:
// src/routes/api/index.ts
import { createAPIFileRoute } from '@tanstack/react-start/api'

export const APIRoute = createAPIFileRoute('/api')({
GET: () => {
return new Response(null, {
status: 301,
headers: { Location: "/" }
})
},
})
// src/routes/api/index.ts
import { createAPIFileRoute } from '@tanstack/react-start/api'

export const APIRoute = createAPIFileRoute('/api')({
GET: () => {
return new Response(null, {
status: 301,
headers: { Location: "/" }
})
},
})
Give me a few minutes whilst I make the example repo
extended-salmon
extended-salmonOP5mo ago
GitHub
GitHub - SkyfallWasTaken/tanstack-start-redirect-api-route
Contribute to SkyfallWasTaken/tanstack-start-redirect-api-route development by creating an account on GitHub.
fascinating-indigo
fascinating-indigo5mo ago
hm seems like a bug in the api routes matching since we are currently reworking API routes and they wont be bound to the /api prefix any more, this will be resolved once we have released that
extended-salmon
extended-salmonOP5mo ago
got it! should I file an issue?
fascinating-indigo
fascinating-indigo5mo ago
it shouldnt be necessary
extended-salmon
extended-salmonOP5mo ago
👍 in the meantime, is it possible to mount hono or some other API framework by any chance?
extended-salmon
extended-salmonOP5mo ago
nice! thank you :) For future searchers, if you set src/api.ts to:
import { createStartAPIHandler } from "@tanstack/react-start/api";
import app from "~/routes/api"; // Or wherever you're exporting your Hono instance

export default createStartAPIHandler(async ({ request }) => {
return app.fetch(request);
});
import { createStartAPIHandler } from "@tanstack/react-start/api";
import app from "~/routes/api"; // Or wherever you're exporting your Hono instance

export default createStartAPIHandler(async ({ request }) => {
return app.fetch(request);
});
You can then use the api directory for your routes instead of server/api or some other directory!

Did you find this page helpful?