Azure static web app anonymous health check route

153 Views Asked by At

I have an Azure static web app with the staticwebapp.config.json like so:

"routes": [
  {
    "route": "/*",
    "allowedRoles": ["authenticated"]
  }
]
"navigationFallback": {
  "rewrite": "index.html",
  "exclude": ["/static/media/*.{png,jpg,gif,svg}", "/static/css/*"]
}

This works and all routes need to go through auth. Now, I want to add an exception to the /health route to allow for health check services to see if my site is up and running. How can I do this? I tried adding a route like this:

"routes": [
  {
    "route": "/health",
    "allowedRoles": ["anonymous"]
  },
  {
    "route": "/*",
    "allowedRoles": ["authenticated"]
  }
]
"navigationFallback": {
  "rewrite": "index.html",
  "exclude": ["/static/media/*.{png,jpg,gif,svg}", "/static/css/*"]
}

But that does not work, /health route still requires authentication.

2

There are 2 best solutions below

1
Lauden On

What do you have behind the /health ?

It may be the navigationFallback that is redirecting you to index.html which requires authentication.

You can define a fallback rule by adding a navigationFallback section. The following example returns /index.html for all static file requests that don't match a deployed file. official doc

To be sure, add /health to the excluded path:

"exclude": ["/static/media/*.{png,jpg,gif,svg}", "/static/css/*", "/health"]

It is not due to the order of routes in your config

Rule evaluation stops at the first match

1
stemixrf On

From the example given here, Configure SWA, this most closely matches your requirement..

{ "route": "/calendar*", "rewrite": "/calendar.html" }

so you may need the wildcard at the end as indicated by

Wildcard pattern

...

The calendar.html file can then use client-side routing to serve a different view for URL variations like /calendar/january/1, /calendar/2020, and /calendar/overview.

Note

A route pattern of /calendar/* matches all requests under the /calendar/ path. However, it won't match requests for the paths /calendar or /calendar.html. Use /calendar* to match all requests that begin with /calendar.