How to isomorphically handle subdomain with react + router + redux?
Asked Answered
P

0

6

I'm building a service where other companies can host a community at companyname.mydomain.com. I have a Redux reducer called company that holds certain state about that company. I'd like to be able to isomorphically fetch data about that company based on the subdomain requested.

If I have a function getSubdomain:

function getSubdomain(host) {
  const hostWithoutPort = host.split(':')[0];
  return hostWithoutPort.split('.').slice(0, -2).join('.');
}

Then on the client I'd get the subdomain by doing:

const subdomain = getSubdomain(window.location.host);

On the server, since window isn't available and the host is different per request, I'd do:

const subdomain = getSubdomain(request.headers.host);

What's the right strategy to properly isomorphically fetch company data based on this subdomain? I'm thinking maybe I could initialize my store with the subdomain set in the state?

Paluas answered 13/12, 2016 at 19:12 Comment(3)
Not sure what the correct tag is here, sorry if the router tags are irrelevant. :/Paluas
what are you using under the hood to process requests? express?Levee
@lfender6445: vanilla nodePaluas

© 2022 - 2024 — McMap. All rights reserved.