Implementing path-based routing in Azure Front Door with Azure Static Web Apps

Ravi Praneeth 50 Reputation points
2025-05-07T13:14:08.0966667+00:00

Issue Description:

We are using Azure Front Door to route between two React-based Azure Static Web Apps with the following setup:

Azure Front Door Domain: https://sample-d6h7buaXXXXXXXX.z01.azurefd.net

  • Rout1 (Home) (Default / First Static Web App):
    • Patterns to match: /*
    • Origin Group : Home ( Origin host name = https://nice-ground-0XXXXXXX.azurestaticapps.net
    • Origin Path: /
    • Status: Working as expected. This is the main/default site.
  • Route 2 (bakery) (Secondary Static web App):
    • Patterns to match: /bakery/*
    • Origin group : Bakery(Origin Host name = https://proud-glacier-07e5XXXXXX.azurestapps.net
    • Origin Path: /bakery
    • Status: Partially working.

Problem Observed:

Accessing https://sample-d6h7buaXXXXXXXX.z01.azurefd.net/bakery correctly routes to Static Web App 2 (Bakery) and loads the index page.

However, all JavaScript, CSS, and static assets requested by the Bakery app are being misrouted.

These assets are incorrectly routed back to Static Web App 1 (the default route /*), leading to:

404 errors

  **Blank screen or broken UI**
  
  The underlying issue appears to be that the assets are requested using relative paths that are not adjusted to match the `/bakery/*` base path.
  

Expected Behavior:

When visiting the /bakery path via Front Door, all assets (JS, CSS, images, etc.) should be loaded from https://proud-glacier-07e5XXXXXX.azurestaticapps.net, not from the root origin (nice-ground-0XXXXXXX.azurestaticapps.net).

Static Web App 2 should work seamlessly under the /bakery/* route.


Request:

We need help ensuring:

Azure Front Door properly serves all assets for /bakery/* from Static Web App 2 only.

There is no fallback to the root (/*) route when assets are requested by the React app.

Guidance on Front Door config and/or React app routing and asset base path changes (if required).


Azure Front Door
Azure Front Door
An Azure service that provides a cloud content delivery network with threat protection.
850 questions
{count} votes

Accepted answer
  1. Venkat V 2,545 Reputation points Microsoft External Staff Moderator
    2025-05-13T08:35:46.1966667+00:00

    Hi @Ravi Praneeth

    As seen above, the error occurred because the package.json file does not have the correct configuration to route the application properly.

    Your React app was built for the root path (/), but you were serving it from a subpath (/bakery/) via Azure Front Door. Because of this mismatch, the browser tried to load static assets like.

    /bakery/css/bootstrap.css
    /bakery/static/js/main.js
    

    To resolve this issue, update the package.Jsonfile with the following entry:

    "homepage": "/bakery"
    

    Here is the update package.Json file

    {
      "name": "landingpage-react-template",
      "private": true,
      "homepage": "/bakery",
      "dependencies": {
        "@testing-library/jest-dom": "^4.2.4",
        "@testing-library/react": "^9.3.2",
        "@testing-library/user-event": "^7.1.2",
        "emailjs-com": "^2.6.4",
        "react": "^17.0.1",
        "react-dom": "^17.0.1",
        "react-helmet": "^6.1.0",
        "react-scripts": "^5.0.1",
        "smooth-scroll": "^16.1.3"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "CI=false react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
      "eslintConfig": {
        "extends": "react-app"
      },
      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      }
    }
    

    After updating the code and rebuilding the app, it loads properly, as shown below
    User's image

    I hope this helps to resolve your query.


    If the above is unclear and/or you are unsure about something, add a comment below.

    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alex Burlachenko 9,780 Reputation points
    2025-05-12T11:11:19.2+00:00

    Hi Ravi,

    Thank you for sharing the details of your routing challenge. This is a common scenario when implementing path-based routing with React applications behind Azure Front Door. The core issue stems from how React handles relative asset paths combined with Azure Front Door's routing behavior.

    The root cause appears to be that your Bakery React application generates asset paths without accounting for the /bakery base path. When these relative paths are requested through Azure Front Door, they're being routed to your default origin (/) instead of remaining within the /bakery/ path space.

    To resolve this, we'll need to make adjustments both in your React application configuration and your Azure Front Door setup.

    First, let's configure your React application to be aware of its base path. If you're using Create React App, you can set the homepage property in your package.json to /bakery or configure the PUBLIC_URL environment variable during build. For applications using React Router, you'll want to specify the basename prop on your BrowserRouter component like this: <BrowserRouter basename="/bakery">. This ensures all client-side routing and asset paths are properly prefixed. You can find more details about this in the React Router documentation.

    For your Azure Front Door configuration, while you've already set the origin path to /bakery, we should consider adding a URL rewrite rule. This rule would strip the /bakery prefix before forwarding requests to your Static Web App origin. You can create a rule that matches /bakery/* and rewrites it to /*, which helps maintain clean paths at the origin while preserving the path-based routing at the edge. The Azure Front Door rules engine documentation provides excellent guidance on setting this up.

    It's also worth checking your Static Web App's configuration file (staticwebapp.config.json) to ensure it properly handles the /bakery prefix for routes and redirects. You might need to add specific route configurations to maintain proper functionality. The Azure Static Web Apps configuration documentation covers this in detail.

    After implementing these changes, I recommend testing thoroughly in a non-production environment first. Pay particular attention to how assets are being loaded (check the Network tab in your browser's developer tools) and verify that all client-side navigation works as expected. Remember that you may need to clear caches (both browser and Azure Front Door) to see the changes take effect immediately.

    If you continue to experience issues or need clarification on any of these steps, don't hesitate to reach out. I'd be happy to help troubleshoot any specific challenges you encounter during implementation.

    Best regards,
    Alex
    P.S. If my answer help to you, please Accept my answer
    PPS That is my Answer and not a Comment
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.