Solid Start Route for simple redirect

I've got a Start route thats sole purpose is to generate a 301 redirect to a dynamic image url I make in createServerData. It's working fine and it does redirect to the image, however if I curl -v I can see that I also get the <!DOCTYPE html><html lang="en" ><head ><title etc...; and my entire apps CSS also delivered, which is a 156kB! This is basically it:
export function routeData(props: RouteDataArgs) {
return createServerData$(async (_, { request }) => {
// Snipped out the actual db call.
// Gets the dynamic url from the db and redirect
return redirect(dbResponse, 301);
});
}

export default function generateEmbedThumb() {
return null;
}
export function routeData(props: RouteDataArgs) {
return createServerData$(async (_, { request }) => {
// Snipped out the actual db call.
// Gets the dynamic url from the db and redirect
return redirect(dbResponse, 301);
});
}

export default function generateEmbedThumb() {
return null;
}
Ryan says "it is going to stick the page in the the HTML as it is going through Solid's rendering. I'd just make a server function outside of routing or maybe an API route.. the whole routeData stuff is setup is the reactive router.. client package basically. It wouldn't work a different way." Sooo, while an API call does what I want, it has to prefix the path with /api (AFAIK?), and this url needs to be a path right off the root as it's replacing a current url which is embedded in loads of client websites. How does one make a server function outside of routing that responds when a route is hit? i.e. mydomain.com/embedimage returning a 301 to an actual url of a jpg. This is what I currently get when using a default route... 156k of html + css, then a redirect to the 11k image!
No description
3 Replies
ryansolid
ryansolid3mo ago
It does not need to be under /api. You can set any path you want.. it will be the same name as the file.
Chronove
Chronove3mo ago
If the html/css part is an issue, why not straight up use an api route? Ex. src/routes/getRandom.ts
export function GET(){
const id = Math.floor(Math.random() * 1000);
return Response.redirect(`/api/randomImg?${id}`, 301);
}
export function GET(){
const id = Math.floor(Math.random() * 1000);
return Response.redirect(`/api/randomImg?${id}`, 301);
}
API Routes should run on the server afaik
Some Call Me Tim
derp, thanks both. Looks like a case of RTFM. I read an old thread that said /api was for api routes, then skimmed the api route handler example and it used /api as well. Didn't realize the magic was in the function name, not the file location.