Dynamic hostname / Nitro

Hi, I've got a single nuxt 3 app that serves two websites. The sites are backed by Sanity. When a request comes in, I grab the domain from event?.node?.req?.headers?.host in my default layout, and ased on the domain, I'm able to set various bits of config, which all works great. I've just enabled the @nuxtjs/sitemap module and got everything working nicely when I hardcode a hostname. When I try to read it from the rquest, it just shows as localhost (in nitro). Normally, one would simply set your appUrl or baseUrl in your nuxt.config, but because this app serves two different domains I can't do that. I've tried everything I can think of - I've even tried using getRequestHost from h3, but I always get localhost. I can get the correct host in a nitro plugin and tried to set a custom header (and various other properties), but they always get overriden to localhost. I've even tried to figure out if there's a way to dynamically inject something into runtimeConfig, but the only way I can find to do that is via .env which won't work in this case. Is there anyone that does something similar or could shed some light, I'd be deeply grateful! Thanks!
2 Replies
woutercouvaras
woutercouvarasOP9mo ago
Is there no one else solving problems like this? I've got one project that serves 12 sites and another project that serves 2. Today, I tried setting a custom header in my api middleware (I can see the correct host there), but my custom header still ends up beging set to localhost when I access it in the /api/__sitemap__/urls.ts file. If I can't solve this I might have to build my own solution 😦
woutercouvaras
woutercouvarasOP9mo ago
In case this helps anyone else, I've ended up dropping the @nuxtjs/sitemap module in favour of simply creating my own api endpoint, and using this nodejs sitemap package to return the xml formatted doc: https://www.npmjs.com/package/sitemap
npm
sitemap
Sitemap-generating lib/cli. Latest version: 7.1.1, last published: 2 years ago. Start using sitemap in your project by running npm i sitemap. There are 362 other projects in the npm registry using sitemap.
Want results from more Discord servers?
Add your server