I am new to create-react-app
.
I want to know why opening the index.html
form a build does not work ??
I know to serve a build serve -s build
should be used, but I want to know why a build react app can’t be started without serving.
I will explain further: What I do is...
create-react-app helloworld
npm run build
or yarn run build
... Now I will have my ./build
directory created with index.html
in it.index.html
in browser and it won't work.I want to understand why this does not work? Why I have to serve the build to make it work?
You only have to conditionally change the code of your public/index. html file based on the current environment, as follows: What happens is that the two sections written with the special syntax presented above will be included in the final HTML file only when process.
To fix this problem go to your build folder then you will see manifest.json file, open it and you will see your manifest.json have: before your react project build go to your package.json file and specify homepage property with . value or maybe with your site base url, see example:
I server will be started from which you can access your react app Show activity on this post. If nothing of above works. Maybe the problem is that you are using react-router-dom and the routes needs a special compilation generating individual htmls for each page that exists in your router config.
It's important to understand the things that need to run a React app. But due to it's Only one build dependency advantage, a beginner might think that react-scripts is the only dependency needed to run react apps and might not know that transpiler (babel), bundler (webpack) are the key dependencies which are used under the hood by react-scripts.
http://myreact-app/build/index.html => White blank screen. This is the build folder which has been created after run npm run build. And this is the index.html
Following answer was given on similar topic by some guy on reddit
The project was built assuming it is hosted at the server root.
To override this, specify the homepage in your package.json.
I wouldn't recommended it but you can add the following to package.json to run it directly from your file server -
"homepage":"./"
https://www.reddit.com/r/reactjs/comments/5iya27/why_open_up_indexhtml_wont_workcreatereactapp/
also, You may want so serve the app on some kind of server more info here: https://create-react-app.dev/docs/deployment and here: https://create-react-app.dev/docs/production-build
For react-router apps, there is a hack. Just make a <Route/>
with path "/index.html" going to a suitable component.
<Route exact path="/" component={Home} />
<Route path="/index.html" component={Home} />
For those using react-router-dom, you can use <HashRouter>
instead of <BrowserRouter>
. It should look like this:
<HashRouter>
<Switch>
<Route path="/" exact component={ Main } />
</Switch>
</HashRouter>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With