3

In theory this should work, adding at the / folder the file /static.json that makes everything go to the React index.html.

(related)

React Routing works in local machine but not Heroku

https://github.com/heroku/heroku-buildpack-static#configuration

In my case it doesn't work, I get a Cannot GET /hello instead of receiving the index.html. Surprisingly, if I go directly to /index.html I get the page but no route is activated.

What is happening and how can I fix it?

UPDATE: tried without luck /static.jason /src/static.jason /client/static.jason /client/src/static.jason

/static.json

{
  "routes": {
    "/**": "index.html"
  }
}

/client/src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import FileUpload from './FileUpload';

ReactDOM.render(
  <div><FileUpload /></div>,
  document.getElementById('root')
);

/client/src/FileUpload.mjs

import React, { Component } from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';

class TextFileReader extends Component {
 render() {
  return (
   <Router>
    <div>
     <Route path="/heyyo" render={() => <span>heyyo</span>} />
     <Route exact path="/" render={() => <div>resting</div>} />
     <Route path="/what" render={() => <div>what</div>} />
      NO ROUTES HERE
    </div>
   </Router>
  )
 }
}

export default TextFileReader;
GWorking
  • 4,011
  • 10
  • 49
  • 90

1 Answers1

2

Finally, re-reading better this React Routing works in local machine but not Heroku I've added this line to my Express

app.get('/*', (req, res) => {
  let url = path.join(__dirname, '../client/build', 'index.html');
  if (!url.startsWith('/app/')) // we're on local windows
    url = url.substring(1);
  res.sendFile(url);
});

And once deployed to Heroku, it works :)

GWorking
  • 4,011
  • 10
  • 49
  • 90
  • Not working Here is the link - [click](https://home-services-housedeck.herokuapp.com/). Can you provide another solution i tried static.json and this method too. – Yash-Sharma2002 Apr 19 '22 at 17:23