showing results for - "subdomain react app"
Axel
11 Apr 2019
1import React from 'react';
2
3import {MainApplication} from './Main';
4
5function subdomainApplications (map) {
6  let main = map.find((item)=> item.main);
7  if (!main) {
8    throw new Error('Must set main flag to true on at least one subdomain app');
9  }
10
11  return function getComponent () {
12    const parts = window.location.hostname.split('.');
13
14    let last_index = -2;
15    const last = parts[parts.length - 1];
16    const is_localhost = last === 'localhost';
17    if (is_localhost) {
18      last_index = -1;
19    }
20
21    const subdomain = parts.slice(0, last_index).join('.');
22
23    if (!subdomain) {
24      return main.application;
25    }
26
27    const app = map.find(({subdomains})=> subdomains.includes(subdomain));
28    if (app) {
29      return app.application;
30    } else {
31      return main.application;
32    }
33  }
34}
35
36const getApp = subdomainApplications([
37  {
38    subdomains: ['www'],
39    application: function () {
40      return 'Main!'
41    }
42    main: true
43  },
44  {
45    subdomains: ['foo'],
46    application: function () {
47      return 'Foo!';
48    }
49  },
50  {
51    subdomains: ['bar', 'baz.bar'],
52    application: function () {
53      return 'Bar!';
54    }
55  }
56]);
57
58export default function Application () {
59  const App = getApp();
60  return (
61    <App className="Application" />
62  );
63}
64
Gaia
11 Jan 2020
1let host = window.location.host;
2let protocol = window.location.protocol;
3let parts = host.split(".");
4let subdomain = "";
5// If we get more than 3 parts, then we have a subdomain
6// INFO: This could be 4, if you have a co.uk TLD or something like that.
7if (parts.length >= 3) {
8  subdomain = parts[0];
9  // Remove the subdomain from the parts list
10  parts.splice(0, 1);
11  // Set the location to the new url
12  window.location = protocol + "//" + parts.join(".") + "/" + subdomain;
13}
14