How can I get the host name in server component?
Unanswered
Black carp posted this in #help-forum
Black carpOP
I use multiple domains for multitenant app and I need to know which tenant is making the request in order to provide them with their version of the app, for example in homepage but also in robots.txt and sitemap.xml pages, I couldn't find a straightforward approach to do this and when things get complex for something simple it usually means I'm not supposed to do that, is that so?
So far the best solution I could come up with (haven't tested it yet) is to put all my routes inside a
Any suggestion is appreciated 🙂
So far the best solution I could come up with (haven't tested it yet) is to put all my routes inside a
/[siteId]/... directory and add a middleware that takes the referrer or some other header and extract the domain out of it and rewrites that domain/siteId into that route, so the client wouldn't actually see or know about the siteId being in the URL but internally nextjs would know and I'd be able to get that siteId in any server component that I need...Any suggestion is appreciated 🙂
8 Replies
Plott Hound
im guessing using the params prop is not an option since you need to get the host name
https://nextjs-faq.com/get-pathname-in-rsc
https://nextjs-faq.com/get-pathname-in-rsc
as far as i know this would be the only way:
https://github.com/vercel/next.js/issues/43704#issuecomment-1411186664
https://github.com/vercel/next.js/issues/43704#issuecomment-1411186664
@Plott Hound as far as i know this would be the only way:
https://github.com/vercel/next.js/issues/43704#issuecomment-1411186664
Black carpOP
The second link is what I was thinking of doing, I found out about nextjs platforms project which aims to do exactly what my project is doing and therfore has the same need for knowing the hostname, they did it exactly how I described above by wrapping the app with
[domain] directory and in the middleware getting the domain and rewriting it to the path param: https://github.com/vercel/platforms/blob/main/middleware.tsBut I'm not sure hoe the robots.txt and sitemap.xml would work like that sinc they need to be in the root of the project, but I'll test and see
@Plott Hound im guessing using the params prop is not an option since you need to get the host name
https://nextjs-faq.com/get-pathname-in-rsc
Black carpOP
I just ran into another probelm, I have pathname based localization on my app, so this is my path:
/[locale], for example /en/... or /es/... etc.. and if there's no locale in the URL I add english by default (/en) to the pathname by redirecting to it from the middleware, and now I added an even higher level of pathname which is the /[domain] so my path now look something like this: /[domain]/[locale] so now I need to rewrite the domain into the domain path and redirect the default locale to the locale path (so that the domain wouldn't be shown to the client pathname but the locale would show to the client), and I can't figure out how to either do both rewrite and redirect from the same middleware or how to chain middlewares (BTW I'm using the app router)@Black carp But I'm not sure hoe the robots.txt and sitemap.xml would work like that sinc they need to be in the root of the project, but I'll test and see
Brown bear
Hey not sure if you still need help here but I encountered something similar and have something working:
I have the
then inside your
I have the
sitemap.xml working in my [domain] folder — I had to nest it and add an entry in the matcher array inside middleware.tsmatcher: [
"/sitemap.xml",
... your other matchers
]then inside your
sitemap.ts you can grab the domain in the same way you do in middleware with headers().get("host")...@Brown bear Hey not sure if you still need help here but I encountered something similar and have something working:
I have the `sitemap.xml` working in my `[domain]` folder — I had to nest it and add an entry in the `matcher` array inside `middleware.ts`
matcher: [
"/sitemap.xml",
... your other matchers
]
then inside your `sitemap.ts` you can grab the domain in the same way you do in middleware with `headers().get("host")...`
Black carpOP
so you nest it in
I have a few questions about your setup:
1. Doesn't your approach means your pathname now includes domain?, meaning instead of being
2. Using headers in your sitemap means it's never being cached for you, right?
3. In your case can't you just access the pathname params as if the sitemap was a
/[domain] and in the middleware add the URL from the request object to a custom header and then access that header wherever you need?I have a few questions about your setup:
1. Doesn't your approach means your pathname now includes domain?, meaning instead of being
example-2.com/sitemap.xml it's example-1.com/example-2.com/sitemap.xml2. Using headers in your sitemap means it's never being cached for you, right?
3. In your case can't you just access the pathname params as if the sitemap was a
page.tsx file because you already have the domain in the pathname?Brown bear
Not exactly, I'm just using the same redirect as they do the platforms/middleware example — but I needed to add
1. nah just
2. yeah in my case I don't want it to be cached since there is user generated content that should be indexed.
3. No pathname params are passed to the sitemap function.
I forgot what I ran into but I had some issues with just having the sitemap.ts in the root
/sitemap.xml to the matcher. host is not a custom header? Sorry I think I may have confused you a bit. 1. nah just
example-1/sitemap.xml2. yeah in my case I don't want it to be cached since there is user generated content that should be indexed.
3. No pathname params are passed to the sitemap function.
I forgot what I ran into but I had some issues with just having the sitemap.ts in the root
