Next.js Discord

Discord Forum

Access subdomain in server component with output: export

Unanswered
Bloodhound posted this in #help-forum
Open in Discord
BloodhoundOP
Im using NextJS 14 with App router, and I have the output: 'export' option in my next.config.mjs file. Now I want to access the subdomain of my url inside my page.tsx (server component by default). Reason being I am fetching some data inside page file, and I required subdomain to put inside the request headers.

I've tried several methods, like using middleware and headers from next/headers. Using both these makes next js switch to dynamic rendering, which the output: 'export' doesn't allow, resulting in errors.

Fetching data is not possible on client side, because SEO is pretty important for us, so we gotta do this server side, and we have to put output: 'export' inside the config file.

6 Replies

BloodhoundOP
bump
@joulev Impossible. Unless: your hosting platform allows something like configuring all requests to foo.example.com to be forwarded to example.com/foo. In that case you can use a dynamic route to render /[subdomain].
BloodhoundOP
hmm... with dynamic routes, we have generateStaticParams, so we will be able to generate different files for different /[subdomain].

We won't be doing this, but maybe with some build script, we can generate different directories for different subdomains
but then issue will be that how will I access the subdomain in server component.
By using generateStaticParams, We can get params object in page.tsx, but I won't get that if I do a custom manual build for different subdomains
@Bloodhound but then issue will be that how will I access the subdomain in server component. By using `generateStaticParams`, We can get params object in page.tsx, but I won't get that if I do a custom manual build for different subdomains
You can’t access the subdomain in the server component. Unless you built different server components for different subdomains, which is precisely the dynamic route method I described above. No other way if server side rendering is required.
BloodhoundOP
Yeah..