How can I get page's domain inside the page or layout without using headers?
Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Australian Freshwater CrocodileOP
I need to get page's domain inside the page and layout components. For example, if user opens https://example.com/hello-world, I need to get
But I don't want to use
Is there some kind of a workaround for this?
Thank you!
example.comBut I don't want to use
headers() because it will make my whole application to be dynamic. Also, I don't want to turn them into client components because my whole application will be rendered only on clientIs there some kind of a workaround for this?
Thank you!
39 Replies
Also, I don't want to turn them into client components because my whole application will be rendered only on client
I'm confused, WDYM by this?
Australian Freshwater CrocodileOP
I can use
window.location inside the page and layout components. But for this, I need to make them client componentsso, you aren't on client right?
you need your domain on server component?
Australian Freshwater CrocodileOP
Yes
I think you can use public environment variables
cause, without accessing the request/url. I don't really find a way you can get the domain.
Australian Freshwater CrocodileOP
I think it won't work because my app can get requests from different domains 😦
Australian Freshwater CrocodileOP
Yeah
😄
Then, headers is the only way I really can think of
Australian Freshwater CrocodileOP
But in this case, none of my API requests will be cached, right? 😦
welllll, you want to use the data from request sent by user, and I personally don't use cache on API routes. IDK
anyways, you're trying to access
headers on api routes right?Australian Freshwater CrocodileOP
Nope, in the page and layout components
I don't use API routes
I have a backend and I use it in order to retrieve the data
yeah, so you can use headers in RSC, it won't really change the much
Australian Freshwater CrocodileOP
Backend is a different project, it's not part of the Next.js application
@averydelusionalperson yeah, so you can use headers in RSC, it won't really change the much
it's still server component
Australian Freshwater CrocodileOP
Yes but it won't have caching for requests 🤔
Cache will always be
skipLike I'm using an app with a hard reload
hmm, I ain't expert on cache, but the cache is for the fetch function no?
how will the headers change it 

Australian Freshwater CrocodileOP
Yes, cache is for the fetch function
yeah, so how would using headers change it? I think fetch is still cached if I'm not wrong
Australian Freshwater CrocodileOP
Hm, let me experiment 🤔
Maybe I'm wrong
maybe I'm too
let me know after you've tried it
Australian Freshwater CrocodileOP
Sure!
Australian Freshwater CrocodileOP
@averydelusionalperson you were right, even though I'm using headers and also cookies, fetch requests are cached!
Australian Freshwater CrocodileOP
By the way, how would you get the domain from the headers? I'm using
host header right now but host may not be present in some cases which is bad =/I really don't think there is any other way,
. IDK, if the host is not available put default domain or something
. IDK, if the host is not available put default domain or something