Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React app route not working after deployed to IIS

Tags:

iis

reactjs

I'm new to reactjs and currently facing some issue on IIS deployment for react app.

i execute npm run build and it generate a build folder, i then copy this folder to my IIS. When i browse the page, im able to view blank page but when i navigate to test route it shows 404.

I've try to add "homepage":"/" or "homepage":"." in my package.json but still the same.

index.html

enter image description here

This is my build structure

enter image description here

Any help is appreciated.

like image 298
Darren Lau Avatar asked Oct 14 '25 20:10

Darren Lau


2 Answers

Whenever you deploy a react page to production you will be facing this problem. Please refer to this page: https://inthetechpit.com/2019/06/13/handle-client-side-routes-with-iis-on-page-refresh-react-app/

The issue happens in the server (not IIS only, but all of them). You will be required to install an extension and then, to add a config file to the path of your front end build directory.

The extension to install is here: https://www.iis.net/downloads/microsoft/url-rewrite

the content of the web.config file that should be placed on the front end build directory goes as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
      <rules>
        <rule name="ReactRouter Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.html" />
        </rule>

      </rules>
    </rewrite>
</system.webServer>
</configuration>
like image 56
Dimas De Jesus Veliz Blanco Avatar answered Oct 17 '25 09:10

Dimas De Jesus Veliz Blanco


To deploy react js app in iis you could follow the below steps:

1)make sure you enabled below iis feature:

asp.net latest version static content directory browsing

enter image description here

2)open command prompt as administrator.enter to the react app folder then run below command:

npm run build

which create a build folder in your app folder.

3)open iis then select add site and add folder path build folder and site binding detail:

enter image description here

now, browse your site.

enter image description here

note: make sure you assign the iis_iusrs and iusr permission to the site folder.

like image 31
Jalpa Panchal Avatar answered Oct 17 '25 10:10

Jalpa Panchal