Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

reactjs - React, load main scripts dynamically depending on the domain

We have more than 5 domains and use one server. And we need to load scripts using CDN. For example, site1.com should load from cdn.site1.com. site2.com should load from cdn.site2.com

The problem: reactjs generate files and URLs in building

How can I solve it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can load script dynamically by creating the script element at run time.

Example

 componentDidMount () {
     if (location.host === 'google.com') {
         this.loadScript("script.js");
         this.loadScript("www.google.com/script.js");
     }
     else if... // here you can add conditions for other domain
   }


   loadScript(src) {
        let script = document.createElement('script');
        script.src = src;    
        document.body.append(script); // appending script to body
    }
    
    

You can find more here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...