react-router does not work in production and surge deployments












2















My react application is working fine on localhost but when after i deployed it in gh-pages or surge it does not give me to move among pages using URL.




  • This is the project repo link

  • Demo url here


users can go to signup page by clicking menuItem of top right corner. but If user use http://chat.susi.ai/signup/ URL, it gives 404 page



I tried several solutions from internet but didn't work.



Next question is : I created a 404 page to show up when user tries to move to broken links.It works fine in localhost. but not in production. I tried this solution but nothing changed.



this is part of my index.js file



const App = () => (
<Router history={hashHistory}>
<MuiThemeProvider>
<Switch>
<Route exact path="/" component={ChatApp} />
<Route exact path="/signup" component={SignUp} />
<Route exact path="/logout" component={Logout} />
<Route exact path="/forgotpwd" component={ForgotPassword} />
<Route exact path="*" component={NotFound} />

</Switch>
</MuiThemeProvider>
</Router>
);

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


If someone can suggest good solution with sample code for my problems that would be really helpful for me.










share|improve this question





























    2















    My react application is working fine on localhost but when after i deployed it in gh-pages or surge it does not give me to move among pages using URL.




    • This is the project repo link

    • Demo url here


    users can go to signup page by clicking menuItem of top right corner. but If user use http://chat.susi.ai/signup/ URL, it gives 404 page



    I tried several solutions from internet but didn't work.



    Next question is : I created a 404 page to show up when user tries to move to broken links.It works fine in localhost. but not in production. I tried this solution but nothing changed.



    this is part of my index.js file



    const App = () => (
    <Router history={hashHistory}>
    <MuiThemeProvider>
    <Switch>
    <Route exact path="/" component={ChatApp} />
    <Route exact path="/signup" component={SignUp} />
    <Route exact path="/logout" component={Logout} />
    <Route exact path="/forgotpwd" component={ForgotPassword} />
    <Route exact path="*" component={NotFound} />

    </Switch>
    </MuiThemeProvider>
    </Router>
    );

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


    If someone can suggest good solution with sample code for my problems that would be really helpful for me.










    share|improve this question



























      2












      2








      2


      3






      My react application is working fine on localhost but when after i deployed it in gh-pages or surge it does not give me to move among pages using URL.




      • This is the project repo link

      • Demo url here


      users can go to signup page by clicking menuItem of top right corner. but If user use http://chat.susi.ai/signup/ URL, it gives 404 page



      I tried several solutions from internet but didn't work.



      Next question is : I created a 404 page to show up when user tries to move to broken links.It works fine in localhost. but not in production. I tried this solution but nothing changed.



      this is part of my index.js file



      const App = () => (
      <Router history={hashHistory}>
      <MuiThemeProvider>
      <Switch>
      <Route exact path="/" component={ChatApp} />
      <Route exact path="/signup" component={SignUp} />
      <Route exact path="/logout" component={Logout} />
      <Route exact path="/forgotpwd" component={ForgotPassword} />
      <Route exact path="*" component={NotFound} />

      </Switch>
      </MuiThemeProvider>
      </Router>
      );

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


      If someone can suggest good solution with sample code for my problems that would be really helpful for me.










      share|improve this question
















      My react application is working fine on localhost but when after i deployed it in gh-pages or surge it does not give me to move among pages using URL.




      • This is the project repo link

      • Demo url here


      users can go to signup page by clicking menuItem of top right corner. but If user use http://chat.susi.ai/signup/ URL, it gives 404 page



      I tried several solutions from internet but didn't work.



      Next question is : I created a 404 page to show up when user tries to move to broken links.It works fine in localhost. but not in production. I tried this solution but nothing changed.



      this is part of my index.js file



      const App = () => (
      <Router history={hashHistory}>
      <MuiThemeProvider>
      <Switch>
      <Route exact path="/" component={ChatApp} />
      <Route exact path="/signup" component={SignUp} />
      <Route exact path="/logout" component={Logout} />
      <Route exact path="/forgotpwd" component={ForgotPassword} />
      <Route exact path="*" component={NotFound} />

      </Switch>
      </MuiThemeProvider>
      </Router>
      );

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


      If someone can suggest good solution with sample code for my problems that would be really helpful for me.







      javascript reactjs react-router react-router-dom






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 22 '17 at 11:29







      isuruAb

















      asked Jun 12 '17 at 3:54









      isuruAbisuruAb

      1,24531725




      1,24531725
























          3 Answers
          3






          active

          oldest

          votes


















          6














          This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.



          For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.





          Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.



          The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).






          share|improve this answer


























          • thank you it works. how simple solution it is :)

            – isuruAb
            Jun 15 '17 at 2:50











          • indeed, serving the index.html file when visiting the subpath works fine :-)

            – Carl Bosch
            Sep 7 '18 at 15:42



















          0














          Changing .htaccess file in the case of using apache as the web server in your build folder worked for me:



          Options -MultiViews
          RewriteEngine On
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^ index.html [QSA,L]





          share|improve this answer































            0














            I had the same problem. And i solved it very easily... :)



            I used react at my front-end(Create-React-App) and an express backend.
            In dev mode all things works great but when i switched to the production build there was some routing issues in my react routing. After a long survey i found that the problem is on the server side serving up the the index.html file.



            It should be after all the server side routing but before all the app.post handler. In other words the following line should be placed after all app.gets and before all app.posts



            // PRODUCTION
            App.get('*', (req, res)=>{
            res.sendFile(path.resolve(where, the, index.html, is))
            })





            share|improve this answer


























              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
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44491184%2freact-router-does-not-work-in-production-and-surge-deployments%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.



              For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.





              Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.



              The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).






              share|improve this answer


























              • thank you it works. how simple solution it is :)

                – isuruAb
                Jun 15 '17 at 2:50











              • indeed, serving the index.html file when visiting the subpath works fine :-)

                – Carl Bosch
                Sep 7 '18 at 15:42
















              6














              This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.



              For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.





              Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.



              The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).






              share|improve this answer


























              • thank you it works. how simple solution it is :)

                – isuruAb
                Jun 15 '17 at 2:50











              • indeed, serving the index.html file when visiting the subpath works fine :-)

                – Carl Bosch
                Sep 7 '18 at 15:42














              6












              6








              6







              This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.



              For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.





              Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.



              The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).






              share|improve this answer















              This is happening because users aren't being served the index.html which bootstraps your app when they visit those URLs directly.



              For surge, you can add a 200.html file which contains the same contents as the index.html you're deploying (or just rename it to 200.html) - this will then be served to users at any URL they visit which doesn't correspond to a static file, allowing your app to start running.





              Edit: looks like you're using create-react-app. This works when you're developing locally because create-react-app's react-scripts package handles serving your index.html as a fallback for these requests.



              The react-scripts user guide has sections on how to handle this when deploying to GitHub Pages and surge.sh (as above).







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jun 12 '17 at 4:05

























              answered Jun 12 '17 at 3:59









              Jonny BuchananJonny Buchanan

              50.2k10126141




              50.2k10126141













              • thank you it works. how simple solution it is :)

                – isuruAb
                Jun 15 '17 at 2:50











              • indeed, serving the index.html file when visiting the subpath works fine :-)

                – Carl Bosch
                Sep 7 '18 at 15:42



















              • thank you it works. how simple solution it is :)

                – isuruAb
                Jun 15 '17 at 2:50











              • indeed, serving the index.html file when visiting the subpath works fine :-)

                – Carl Bosch
                Sep 7 '18 at 15:42

















              thank you it works. how simple solution it is :)

              – isuruAb
              Jun 15 '17 at 2:50





              thank you it works. how simple solution it is :)

              – isuruAb
              Jun 15 '17 at 2:50













              indeed, serving the index.html file when visiting the subpath works fine :-)

              – Carl Bosch
              Sep 7 '18 at 15:42





              indeed, serving the index.html file when visiting the subpath works fine :-)

              – Carl Bosch
              Sep 7 '18 at 15:42













              0














              Changing .htaccess file in the case of using apache as the web server in your build folder worked for me:



              Options -MultiViews
              RewriteEngine On
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteRule ^ index.html [QSA,L]





              share|improve this answer




























                0














                Changing .htaccess file in the case of using apache as the web server in your build folder worked for me:



                Options -MultiViews
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteRule ^ index.html [QSA,L]





                share|improve this answer


























                  0












                  0








                  0







                  Changing .htaccess file in the case of using apache as the web server in your build folder worked for me:



                  Options -MultiViews
                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteRule ^ index.html [QSA,L]





                  share|improve this answer













                  Changing .htaccess file in the case of using apache as the web server in your build folder worked for me:



                  Options -MultiViews
                  RewriteEngine On
                  RewriteCond %{REQUEST_FILENAME} !-f
                  RewriteRule ^ index.html [QSA,L]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 6:40









                  Siamak A.MotlaghSiamak A.Motlagh

                  3,39443054




                  3,39443054























                      0














                      I had the same problem. And i solved it very easily... :)



                      I used react at my front-end(Create-React-App) and an express backend.
                      In dev mode all things works great but when i switched to the production build there was some routing issues in my react routing. After a long survey i found that the problem is on the server side serving up the the index.html file.



                      It should be after all the server side routing but before all the app.post handler. In other words the following line should be placed after all app.gets and before all app.posts



                      // PRODUCTION
                      App.get('*', (req, res)=>{
                      res.sendFile(path.resolve(where, the, index.html, is))
                      })





                      share|improve this answer






























                        0














                        I had the same problem. And i solved it very easily... :)



                        I used react at my front-end(Create-React-App) and an express backend.
                        In dev mode all things works great but when i switched to the production build there was some routing issues in my react routing. After a long survey i found that the problem is on the server side serving up the the index.html file.



                        It should be after all the server side routing but before all the app.post handler. In other words the following line should be placed after all app.gets and before all app.posts



                        // PRODUCTION
                        App.get('*', (req, res)=>{
                        res.sendFile(path.resolve(where, the, index.html, is))
                        })





                        share|improve this answer




























                          0












                          0








                          0







                          I had the same problem. And i solved it very easily... :)



                          I used react at my front-end(Create-React-App) and an express backend.
                          In dev mode all things works great but when i switched to the production build there was some routing issues in my react routing. After a long survey i found that the problem is on the server side serving up the the index.html file.



                          It should be after all the server side routing but before all the app.post handler. In other words the following line should be placed after all app.gets and before all app.posts



                          // PRODUCTION
                          App.get('*', (req, res)=>{
                          res.sendFile(path.resolve(where, the, index.html, is))
                          })





                          share|improve this answer















                          I had the same problem. And i solved it very easily... :)



                          I used react at my front-end(Create-React-App) and an express backend.
                          In dev mode all things works great but when i switched to the production build there was some routing issues in my react routing. After a long survey i found that the problem is on the server side serving up the the index.html file.



                          It should be after all the server side routing but before all the app.post handler. In other words the following line should be placed after all app.gets and before all app.posts



                          // PRODUCTION
                          App.get('*', (req, res)=>{
                          res.sendFile(path.resolve(where, the, index.html, is))
                          })






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 29 '18 at 19:02









                          isuruAb

                          1,24531725




                          1,24531725










                          answered Nov 28 '18 at 16:50









                          Mohammad EbrahimiMohammad Ebrahimi

                          92




                          92






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44491184%2freact-router-does-not-work-in-production-and-surge-deployments%23new-answer', 'question_page');
                              }
                              );

                              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







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)