React build will not work properly without adding index.html in URL
P粉216203545
P粉216203545 2023-09-01 11:25:58
0
1
412

I'm using hash routing for a React JS application. The application runs fine in local environment.

For example https://stackoverflow.com/posts/ should be the URL of the application but it doesn't work and I have to use https://stackoverflow.com/posts/index.html to make it work.

After deploying, I noticed that it was downloading an empty file named "download" instead of serving the default index.html file in the build file . To run builds on the server, I use S3 and CloudFront architecture.

Right now, I can't tell if the HASH routing is misbehaving or if there's something wrong with the AWS deployment.

Code snippet using React JS

import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import store from './redux/store'; import App from './App'; import reportWebVitals from './reportWebVitals'; import { HashRouter } from 'react-router-dom/cjs/react-router-dom'; ReactDOM.render(      document.getElementById('root') );

Part of package.json content

{ "name": "loremipsum", "version": "0.1.0", "private": true, "homepage": "/loremipsum/" }

P粉216203545
P粉216203545

reply all (1)
P粉515066518

Does it need to be deployed in a subdirectory? If so, you will have to change the contents of thepackage.jsonfile to force the resource to be served relative to theindex.htmlfile.

{ "name": "loremipsum", "version": "0.1.0", "private": true, "homepage": ".", "scripts": { "build": "react-scripts build", "build-prd": "PUBLIC_URL=http://www.foo/relativepath react-scripts build" // ... } }

With this we will be able to move applications hosted inhttp://www.footohttp://www.foo/relativepathor evenhttps://www.foo/bar1/bar2/No need to rebuild.

You must also overridePUBLIC_URLto include the domain and subdirectory where you want the files to be hosted

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!