score:1
Accepted answer
since you are using create-react-app
for this, there's no need to eject the webpack.config.js
(since this is irreversible, i hope you have a git commit you can revert). so here's the general gist:
- you create a file called
chatloader.js
outside of your react project (if you don't intend to learn how to configure this in the same webpack config, which might get a little tricky) and add babel transpilation and minification by yourself. - this file contains something like (untested)
/**
* loads a javascript file asynchronously
* @param {string} url must be valid in <script async src=
* @returns {promise<htmlscriptelement>}
*/
function loadscriptasync(url) {
return new promise((resolve, reject) => {
const script = document.createelement("script");
script.onload = () => resolve(script);
script.onerror = () => reject(new error(`could not load script ${url}`));
script.async = true;
script.src = url;
document.getelementsbytagname("head")[0].appendchild(script);
});
}
function loadstyleasync(src) {
return new promise((resolve, reject) => {
const link = document.createelement("link");
link.href = src;
link.rel = "stylesheet";
link.onload = () => resolve(link);
link.onerror = () => reject(new error(`style load error for ${src}`));
document.head.append(link);
});
}
async function fetchmanifest() {
// todo: add error handling for production code!
const res = await fetch("https://your-url/path/asset-manifest.json");
const text = await res.text();
return json.parse(text)["entrypoints"]; // we only want to load the entrypoints from this file
}
(async() => {
// this is the main function
const entries = await fetchmanifest();
for(const entry of entries){
// this is loading all entries at once, not waiting for the first entry to be loaded before taking the next one
if(entry.endswith(".css")){
loadstyleasync(`https://your-url/path/${entry}`)
} else if (entry.endswith(".js")) {
loadscriptasync(`https://your-url/path/${entry}`)
}
}
})();
you can (when you get the chatloader.js
to work) just embed this one file into your client's code and it should preload the latest build of your react app.
Source: stackoverflow.com
Related Query
- How to deploy a react app to a website once and forget about it
- How to create and deploy a React App properly?
- How to deploy React app with docker and serve -s build
- How do I deploy a React app and a Rails API to Heroku?
- How do I deploy an react app to azure and change the default page
- How to deploy and host react app on Apache server
- How to deploy Spring boot and React application together on GCP App Engine?
- How to deploy React app and Nodejs backend on the same directory in the subdomain?
- How to merge React App and Express App to deploy on single project?
- How to programmatically add a className and a onLoad function to all the images in the project at once in React app without Jqyery
- How to deploy a React App on Apache web server
- How to fetch data only once in a Next.js app and make it accesible to all the app, both in server and client
- How to deploy a react app on cPanel?
- How to inject port and host using ENV variable in Create React App Proxy settings?
- How to replace %PUBLIC_URL% in react app on an azure website
- How to run useEffect once even if there're dependencies? And why ESLint is complaining about it?
- how to share environment variables between react app and express js server hosting it as static site
- How to Deploy Django Rest Framework and React on AWS
- How to run and debug a react app directly from VSCode?
- How to analyze create react app build size and reduce it?
- How to embed React App into another website
- How to deploy a react app to mutiple environments without multiple builds?
- How to read console.log from a mounted component with Enzyme and Jest in Create React App
- How to set React app and API on same port?
- How to reduce react app build time and understanding behaviour of webpack when bundling
- how to deploy successfully React app with nextjs to AWS Amplify?
- How to avoid very long paths and use absolute paths within a Create React App project?
- How to build react app using Dockerfile.dev and Yarn
- How can I start my node server and react app at the same time?
- How to detect screen orientation in mobile website when using react js and orientationchange event?
More Query from same tag
- React sending async state from parent to child component after fetch in a functional component
- I can't access the second level of a json after passing the data for props, in react js
- Is there a way to expose route params outside <Route> component?
- React Js component rendering only once after state change
- How to hide and show list contents: ReactJS
- ReactJS Header and Footer
- React: Inputs not updating different values
- Cannot access a nested array within an object in react-redux
- How to center items in a div without losing height of parent?
- How to send axios put request with jwt token in reactjs
- How to change the size/height/width of Modal inside React-Bootstrap?
- React JS: Why am I getting error at componentdidMount()?
- Updating a State Array in React
- Ion Tabs not appearing / acting as they should even on a blank page
- How to set POST fileupload parameter in ReactJS
- How to Use css selectors in jest unit testing while using css modules
- How to debounce a callback in functional component using hooks
- How to pass auth token that I can access API?(react question)
- How to test if element(tag) rendered with jest/enzyme?
- GET http://localhost:3000/api/house-details/[object%20Object]602ac5039b39c90015b3dc44 400 (Bad Request)
- How to implement MobX in Create-React-App?
- Updating the state of object values in React
- How to break loop after 2nd element on react js inside map function
- React Event Listeners for isolated components
- Buttons and Images not rendering wight height in safari
- Unselect MUI Multi select with initial value
- How to make sure user enters valid Regex on an HTML form input?
- Material Design Components Toggle effect not working
- trying to run react and node.js concurrently
- pages.map is not a function in gatsby nodejs using axios to fetch data