Azure Static App Route configuration with React Router
Asked Answered
A

3

15

I am attempting to configure a static app on azure and am struggling to route correctly. When I navigate to /lti/login/ within the app it works fine. But if I refresh it throws a 404, which tells me that my routes.json isn't setup correctly (maybe). I am hoping someone can shine some light on this.

routes.json

{
    "routes": [
      {
        "route": "/",
        "serve":"/"
  
      },
      {
        "route": "/lti/login/*",
        "serve":"/lti/login"

      }
     
    ]
  
  }

App.js

   <Router>
      <div>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/lti/login/">About</Link>
          </li>
        </ul>

        <hr />

        {/* A <Switch> looks through its children <Route>s and
          renders the first one that matches the current URL. */}
        <Switch>
          <Route exact path="/">
            <Form />
          </Route>
          <Route path="/lti/login/*"> <----- If I navigate to this within the app and then refresh it throws a 404. 
            <About />
          </Route>
        </Switch>
      </div>
    </Router>
Avidin answered 8/2, 2021 at 22:28 Comment(1)
Did any of the answers work for you?Reddick
C
20

As for the latest documentation the use of routes is deprecated:

routes.json that was previously used to configure routing is deprecated. Use staticwebapp.config.json as described in this article to configure routing and other settings for your static web app.

now you need to add navigationFallback section in staticwebapp.config.json like so:

{
"navigationFallback": {
    "rewrite": "index.html",
    "exclude": ["*.{svg,png,jpg,gif}","*.{css,scss}","*.js"]
  }}

you can find the full documentation here:

https://learn.microsoft.com/en-us/azure/static-web-apps/configuration#fallback-routes

Condyle answered 27/9, 2021 at 16:58 Comment(0)
L
7

Following latest "Azure Static Web Apps configuration", I did one config example for React deployed to Azure Static Web Apps: staticwebapp.config.json

{
    "routes": [
        {
            "route": "/admin",
            "allowedRoles": ["administrator"]
        }
    ],
    "navigationFallback": {
      "rewrite": "index.html",
      "exclude": ["/static/media/*.{png,jpg,gif,svg}", "/static/css/*"]
    },
    "responseOverrides": {
      "400": {
        "rewrite": "/invalid-invitation-error.html"
      },
      "401": {
        "redirect": "/login",
        "statusCode": 302
      },
      "403": {
        "rewrite": "/custom-forbidden-page.html"
      },
      "404": {
        "rewrite": "/404.html"
      }
    },
    "globalHeaders": {
      "content-security-policy": "default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'"
    },
    "mimeTypes": {
      ".json": "text/json"
    }
  }
Lewert answered 14/9, 2021 at 11:43 Comment(0)
W
0

For react you need it to serve the index.html file.

Here's an example routes.json config:

{
  "routes": [
    {
      "route": "/*",
      "serve": "/index.html",
      "statusCode": 200
    }
  ],
  "platformErrorOverrides": [
    { "errorType": "NotFound", "serve": "/404" },
    {
      "errorType": "Unauthenticated",
      "statusCode": "302",
      "serve": "/login"
    }
  ],
  "mimeTypes": {
    "json": "application/json"
  }
}
Welt answered 23/2, 2021 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.