Heroku with React-Router and Create-React-App
This question has been asked multiple times, I have been reading the similar questions and attempting to resolve this issue with no luck so I am posting my code to see if a fresh pair of eyes can help solve my problem.
My app works as expected on Heroku, however when refreshing the page or navigating directly to a route that's not the home page (example myapp.com/whatever). I am getting a 404 not found.
My routing works locally so I know it has to do with my app settings and Heroku. I have a MERN application with create-react-app client and express server backend
Folder Stucture
client/
server/
package.json
static.json
static.json
{
"root": "client/build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}
Router.js
<BrowserRouter basename="/">
<Fragment>
<Switch>
<Route exact path="/" component={Home} />
<Route extact path="/login" component={Login} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</Switch>
</Fragment>
</BrowserRouter>
Server.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client', 'build', 'index.html'));
});
}
Anybody see anything that I am missing?
reactjs heroku react-router-v4 react-router-dom
add a comment |
This question has been asked multiple times, I have been reading the similar questions and attempting to resolve this issue with no luck so I am posting my code to see if a fresh pair of eyes can help solve my problem.
My app works as expected on Heroku, however when refreshing the page or navigating directly to a route that's not the home page (example myapp.com/whatever). I am getting a 404 not found.
My routing works locally so I know it has to do with my app settings and Heroku. I have a MERN application with create-react-app client and express server backend
Folder Stucture
client/
server/
package.json
static.json
static.json
{
"root": "client/build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}
Router.js
<BrowserRouter basename="/">
<Fragment>
<Switch>
<Route exact path="/" component={Home} />
<Route extact path="/login" component={Login} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</Switch>
</Fragment>
</BrowserRouter>
Server.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client', 'build', 'index.html'));
});
}
Anybody see anything that I am missing?
reactjs heroku react-router-v4 react-router-dom
add a comment |
This question has been asked multiple times, I have been reading the similar questions and attempting to resolve this issue with no luck so I am posting my code to see if a fresh pair of eyes can help solve my problem.
My app works as expected on Heroku, however when refreshing the page or navigating directly to a route that's not the home page (example myapp.com/whatever). I am getting a 404 not found.
My routing works locally so I know it has to do with my app settings and Heroku. I have a MERN application with create-react-app client and express server backend
Folder Stucture
client/
server/
package.json
static.json
static.json
{
"root": "client/build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}
Router.js
<BrowserRouter basename="/">
<Fragment>
<Switch>
<Route exact path="/" component={Home} />
<Route extact path="/login" component={Login} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</Switch>
</Fragment>
</BrowserRouter>
Server.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client', 'build', 'index.html'));
});
}
Anybody see anything that I am missing?
reactjs heroku react-router-v4 react-router-dom
This question has been asked multiple times, I have been reading the similar questions and attempting to resolve this issue with no luck so I am posting my code to see if a fresh pair of eyes can help solve my problem.
My app works as expected on Heroku, however when refreshing the page or navigating directly to a route that's not the home page (example myapp.com/whatever). I am getting a 404 not found.
My routing works locally so I know it has to do with my app settings and Heroku. I have a MERN application with create-react-app client and express server backend
Folder Stucture
client/
server/
package.json
static.json
static.json
{
"root": "client/build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}
Router.js
<BrowserRouter basename="/">
<Fragment>
<Switch>
<Route exact path="/" component={Home} />
<Route extact path="/login" component={Login} />
<PrivateRoute path="/dashboard" component={Dashboard} />
</Switch>
</Fragment>
</BrowserRouter>
Server.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client', 'build', 'index.html'));
});
}
Anybody see anything that I am missing?
reactjs heroku react-router-v4 react-router-dom
reactjs heroku react-router-v4 react-router-dom
asked Nov 25 '18 at 15:59
Jason McFarlaneJason McFarlane
459210
459210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've had a lot of time spent on this issue also. If I remember correctly you have to create a new property in your package.json file that's called "homepage". You can place it last inside of the object in your package.json. The value for that property would be your URL where you host your App. So for my example it was:
{
"name": "react_movie_db_course",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"homepage" : "http://www.weibenfalk.com/react_rmdb/"
}
Don't think you need the basename on your component either if you're not starting from a subfolder.
It may even be so that you have to edit your .htaccess file on the server also. This is what my .htaccess file look like.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Fallback all other routes to index.html
RewriteRule ^ /react_rmdb/index.html [L]
I also know that it took maybe 24 hours before the routing started to work correctly on the server.
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469244%2fheroku-with-react-router-and-create-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've had a lot of time spent on this issue also. If I remember correctly you have to create a new property in your package.json file that's called "homepage". You can place it last inside of the object in your package.json. The value for that property would be your URL where you host your App. So for my example it was:
{
"name": "react_movie_db_course",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"homepage" : "http://www.weibenfalk.com/react_rmdb/"
}
Don't think you need the basename on your component either if you're not starting from a subfolder.
It may even be so that you have to edit your .htaccess file on the server also. This is what my .htaccess file look like.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Fallback all other routes to index.html
RewriteRule ^ /react_rmdb/index.html [L]
I also know that it took maybe 24 hours before the routing started to work correctly on the server.
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
add a comment |
I've had a lot of time spent on this issue also. If I remember correctly you have to create a new property in your package.json file that's called "homepage". You can place it last inside of the object in your package.json. The value for that property would be your URL where you host your App. So for my example it was:
{
"name": "react_movie_db_course",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"homepage" : "http://www.weibenfalk.com/react_rmdb/"
}
Don't think you need the basename on your component either if you're not starting from a subfolder.
It may even be so that you have to edit your .htaccess file on the server also. This is what my .htaccess file look like.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Fallback all other routes to index.html
RewriteRule ^ /react_rmdb/index.html [L]
I also know that it took maybe 24 hours before the routing started to work correctly on the server.
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
add a comment |
I've had a lot of time spent on this issue also. If I remember correctly you have to create a new property in your package.json file that's called "homepage". You can place it last inside of the object in your package.json. The value for that property would be your URL where you host your App. So for my example it was:
{
"name": "react_movie_db_course",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"homepage" : "http://www.weibenfalk.com/react_rmdb/"
}
Don't think you need the basename on your component either if you're not starting from a subfolder.
It may even be so that you have to edit your .htaccess file on the server also. This is what my .htaccess file look like.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Fallback all other routes to index.html
RewriteRule ^ /react_rmdb/index.html [L]
I also know that it took maybe 24 hours before the routing started to work correctly on the server.
I've had a lot of time spent on this issue also. If I remember correctly you have to create a new property in your package.json file that's called "homepage". You can place it last inside of the object in your package.json. The value for that property would be your URL where you host your App. So for my example it was:
{
"name": "react_movie_db_course",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-fontawesome": "^1.6.1",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"homepage" : "http://www.weibenfalk.com/react_rmdb/"
}
Don't think you need the basename on your component either if you're not starting from a subfolder.
It may even be so that you have to edit your .htaccess file on the server also. This is what my .htaccess file look like.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# Fallback all other routes to index.html
RewriteRule ^ /react_rmdb/index.html [L]
I also know that it took maybe 24 hours before the routing started to work correctly on the server.
edited Nov 25 '18 at 16:46
answered Nov 25 '18 at 16:31
weibenfalkweibenfalk
55617
55617
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
add a comment |
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
hmm, didn't seem to do the trick for me at the moment.. thanks for the response though, super frustrating issue
– Jason McFarlane
Nov 25 '18 at 16:37
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
I will try, thanks mate!
– Jason McFarlane
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
Edited my original anser instead so I could format the code correctly =)
– weibenfalk
Nov 25 '18 at 16:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469244%2fheroku-with-react-router-and-create-react-app%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown