Next.js Discord

Discord Forum

How to retreive subdomain

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I have set up my middleware to work with subdomains, however how can I retrieve the subdomain so taht I can conditionally render components?
Answered by joulev
retrieve it from the hostname header

// middleware:
request.headers.get("hostname")

// server components:
headers().get("hostname")
View full answer

10 Replies

@American black bear I have set up my middleware to work with subdomains, however how can I retrieve the subdomain so taht I can conditionally render components?
retrieve it from the hostname header

// middleware:
request.headers.get("hostname")

// server components:
headers().get("hostname")
Answer
American black bearOP
I hate how easy that was. Thanks
And for client? xd
@American black bear And for client? xd
window.location.hostname
American black bearOP
Thanks
American black bearOP
Last question I promise, how can I send the domain name as a props to any file (client or server)? Instead of having the logic in each page
Maybe from the middleware?
Hmm I don’t think you can send anything to be globally available. Best you can do is abstract the above to a function, say, getSubdomain() and use it wherever you need it
American black bearOP
Yea this was my thought process but since I am beginner I thought maybe there is a cleaner way. Thank you so much
You’re welcome