Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

subdomain react app

let host = window.location.host;
let protocol = window.location.protocol;
let parts = host.split(".");
let subdomain = "";
// If we get more than 3 parts, then we have a subdomain
// INFO: This could be 4, if you have a co.uk TLD or something like that.
if (parts.length >= 3) {
  subdomain = parts[0];
  // Remove the subdomain from the parts list
  parts.splice(0, 1);
  // Set the location to the new url
  window.location = protocol + "//" + parts.join(".") + "/" + subdomain;
}
Comment

subdomain react app

import React from 'react';

import {MainApplication} from './Main';

function subdomainApplications (map) {
  let main = map.find((item)=> item.main);
  if (!main) {
    throw new Error('Must set main flag to true on at least one subdomain app');
  }

  return function getComponent () {
    const parts = window.location.hostname.split('.');

    let last_index = -2;
    const last = parts[parts.length - 1];
    const is_localhost = last === 'localhost';
    if (is_localhost) {
      last_index = -1;
    }

    const subdomain = parts.slice(0, last_index).join('.');

    if (!subdomain) {
      return main.application;
    }

    const app = map.find(({subdomains})=> subdomains.includes(subdomain));
    if (app) {
      return app.application;
    } else {
      return main.application;
    }
  }
}

const getApp = subdomainApplications([
  {
    subdomains: ['www'],
    application: function () {
      return 'Main!'
    }
    main: true
  },
  {
    subdomains: ['foo'],
    application: function () {
      return 'Foo!';
    }
  },
  {
    subdomains: ['bar', 'baz.bar'],
    application: function () {
      return 'Bar!';
    }
  }
]);

export default function Application () {
  const App = getApp();
  return (
    <App className="Application" />
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: django restframework jquery post 
Javascript :: how to filter on a hidden column datatables 
Javascript :: vs 2019 how to publish angular environment prod 
Javascript :: i need to keep track of quantity in inventory using JavaScript backend 
Javascript :: js camelcase 
Javascript :: nodejs validate bnb wallet address 
Javascript :: use global variable in anonymous function 
Javascript :: javascript expressions JSX 
Javascript :: iterate over all check box in a div 
Javascript :: Run a second function only after the first function is completely finished 
Javascript :: redux merge array of objects 
Javascript :: json patch 
Javascript :: filter data from database for specific user in js 
Javascript :: js regex to find string but not another 
Javascript :: ggufhca spingnift cpwjirbgi bshhv 3 bvvvslit nevkdhaer nhdydg kllojb n 
Javascript :: cli run js 
Javascript :: srcset vue 
Javascript :: halt button from submitting js 
Javascript :: let scores = [80, 90, 70]; for (const score of scores) { console.log(score); } 
Javascript :: iron_to_nugget.json 
Javascript :: where to set cdvMinSdkVersion 
Javascript :: making js local function globally accessible 
Javascript :: node close rabbitmq connection 
Javascript :: json decoding and optionals 
Javascript :: routing vue with meta tag firebase 
Javascript :: função que retorna uma media aritmética javascript 
Javascript :: how to transpose json data 
Javascript :: Target type ieee.std_logic_1164.STD_LOGIC_VECTOR in variable assignment is different from expression type ieee.std_logic_1164.STD_ULOGIC. 
Javascript :: how to create nav tab with javascript with validation to move to the next tab 
Javascript :: if property is same group javscript 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =