Create API route at /sitemap.xml or /manifest.json
I'm trying to figure out how to create a handler that will response to /sitemap.xml or /manifest.json
The difficulties I'm running in to are:
- Wanting /api routes (no root, no layout, not html content-type) at a route other than /api
- Wanting to include the
. in the route itself - sitemap.xml.ts maps to /sitemap/xml in Tanstack file routing
Has anyone figured this out?11 Replies
exotic-emerald•10mo ago
Just a hunch, I haven't tried it myself at all - what happens if you do
(sitemap.xml).ts?modern-tealOP•10mo ago
That throws an error -
extended-salmon•10mo ago
I'd also want this to work. As a workaround, I'm adding custom routers to config (in app.config.ts), for example:
modern-tealOP•10mo ago
Tbh, this is great - explicit exceptions to the norm are totally good with me.
What does you
app/sitemap.ts look like to actually serve a response?
Am I just falling back to Vinxi at that point?extended-salmon•10mo ago
This is stripped down version of sitemap.ts:
You're basically working with NitroJS handlers. I'm not sure which parts are part of Vinxi and which is just Nitro.
modern-tealOP•10mo ago
For some reason when I curl localhost:3000/sitemap.xml, I get:
My app.config
My sitemap at
app/routes/sitemap.ts
This happens if I move it out of app/routes and to app/sitemap.ts as wellextended-salmon•10mo ago
Hmm, I just noticed that. This did work for me with an older version. I fixed it by setting
base: "/sitemap" w/o the xml extension, then fetching /sitemap.xml works, but so does any other subpath. I can check even.path === '.xml' in the event handler and throw 404 otherwise.unwilling-turquoise•10mo ago
cc @Sean Cassiere
modern-tealOP•10mo ago
Hm, I still get the redirect, even with
base: /sitemaplike-gold•10mo ago
This is flaw in how the base works. Currently, each Vinxi router is tied to a
base (ie `/, /_build, /_server, /api - https://github.com/TanStack/router/blob/8f7c193ac68ce4ffaeae11b0a56c3cbd1a2baf29/packages/start/src/config/index.ts#L362).
Because of this, at the moment anything hitting /* that doesn't match the others, gets send to the SSR router/handler which would try to render a React app.
This'll likely see a big shake up with Tanner's plans for directly using Nitro, since we wouldn't be working with Vinxi's "router" concept, rather using whatever Nitro and Vite's Environment API provides.GitHub
router/packages/start/src/config/index.ts at 8f7c193ac68ce4ffaeae11...
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router
like-gold•10mo ago
For now, I'm not too sure what's the best way to add this sitemap route in. Possibly, the suggested way of adding in a Vinxi router.
It's not perfect, but this workaround should be fine for now till Tanner's completed the big migrations that Start is currently going through.