Hi @Tejas Lade ,
I want to expand on Claudia's answer-
The issue you’re experiencing is a common one with single-page applications (SPAs) like your React app, where the routing is handled on the client-side. When you refresh the page, the server tries to find a resource matching the route, which doesn’t exist, hence the 404 error
Here’s how you can fix this issue:
- You need to add a fallback route in your server configuration that will serve your
index.html
file for all routes This allows the React router to handle the routing on the client-side. If you’re using Azure Static Web Apps, you can add this fallback route in the staticwebapp.config.json
file.
- If you’re using Azure App Service, you can add a
web.config
file at the root of your project that rewrites all routes to index.html
Here’s an example of what the web.config
file could look like:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="React Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
This configuration will rewrite all requests to index.html, allowing the React router to handle the routing.
Best,
Grace