Mark Pitt
claude-skills
  1. claude-skills
  2. .NET skills
  3. azure-swa
  4. resources/configuration-routing.md

A file in azure-swa

resources/configuration-routing.md

Markdown 6.3 KB
configuration-routing.mdMarkdown, rendered

Configuration & Routing

Security reminder: All route patterns, role names, header values, and redirect URLs that come from user input must be validated before being written to staticwebapp.config.json. See the "Input Sanitization" table in SKILL.md for required validation rules. Never write unsanitized user input to this file.

staticwebapp.config.json

The staticwebapp.config.json file controls routing, authentication, and other runtime behaviors.

Location: Root of repository or output directory

Basic Example

{
  "routes": [
    {
      "route": "/api/*",
      "allowedRoles": ["authenticated"]
    },
    {
      "route": "/admin/*",
      "allowedRoles": ["admin"]
    },
    {
      "route": "/*",
      "serve": "/index.html",
      "statusCode": 200
    }
  ],
  "navigationFallback": {
    "rewrite": "/index.html",
    "exclude": ["/images/*.{png,jpg,gif}", "/css/*"]
  },
  "responseOverrides": {
    "401": {
      "redirect": "/login",
      "statusCode": 302
    },
    "404": {
      "rewrite": "/404.html",
      "statusCode": 404
    }
  },
  "globalHeaders": {
    "content-security-policy": "default-src 'self'",
    "X-Frame-Options": "DENY",
    "X-Content-Type-Options": "nosniff"
  },
  "mimeTypes": {
    ".json": "application/json",
    ".wasm": "application/wasm"
  }
}

Routes Section

Define access control and routing rules:

{
  "routes": [
    {
      "route": "/profile",
      "allowedRoles": ["authenticated"]
    },
    {
      "route": "/admin/*",
      "allowedRoles": ["admin", "superuser"]
    },
    {
      "route": "/public/*",
      "allowedRoles": ["anonymous"]
    }
  ]
}

Route Properties

PropertyTypeDescription
routestringURL pattern to match
allowedRolesstring[]Allowed user roles
servestringFile to serve (for routing)
statusCodenumberHTTP status for response
headersobjectRoute-specific headers
methodsstring[]Allowed HTTP methods
redirectstringRedirect URL

Navigation Fallback (SPA Support)

Essential for single-page applications:

{
  "navigationFallback": {
    "rewrite": "/index.html",
    "exclude": [
      "/api/*",
      "/*.{css,scss,js,png,gif,ico,jpg,svg,woff,woff2,ttf,eot}"
    ]
  }
}

Use when:

  • Building React, Angular, or Vue apps
  • Need client-side routing to handle
  • Want all non-matching routes to serve index.html

Exclude patterns:

  • API routes (already handled by /api/* route)
  • Static file extensions
  • Binary assets

Response Overrides

Custom error pages and redirects:

{
  "responseOverrides": {
    "401": {
      "redirect": "/.auth/login/github",
      "statusCode": 302
    },
    "403": {
      "rewrite": "/forbidden.html",
      "statusCode": 403
    },
    "404": {
      "rewrite": "/404.html",
      "statusCode": 404
    }
  }
}

Common status codes:

  • 401 - Authentication required
  • 403 - Forbidden (insufficient permissions)
  • 404 - Not found
  • 500 - Server error

Global Headers

Apply headers to all responses:

{
  "globalHeaders": {
    "X-Frame-Options": "DENY",
    "X-Content-Type-Options": "nosniff",
    "Referrer-Policy": "strict-origin-when-cross-origin",
    "Permissions-Policy": "camera=(), microphone=()"
  }
}

Security Headers Explained

HeaderPurposeExample
X-Frame-OptionsPrevent clickjackingDENY or SAMEORIGIN
X-Content-Type-OptionsPrevent MIME sniffingnosniff
X-XSS-ProtectionXSS attack protection1; mode=block
Referrer-PolicyControl referrer infostrict-origin-when-cross-origin
Content-Security-PolicyRestrict resource loadingdefault-src 'self'
Strict-Transport-SecurityForce HTTPSmax-age=31536000; includeSubDomains

Route-Specific Headers

Apply headers to specific routes:

{
  "routes": [
    {
      "route": "/api/*",
      "headers": {
        "Cache-Control": "no-cache, no-store, must-revalidate"
      }
    },
    {
      "route": "/static/*",
      "headers": {
        "Cache-Control": "public, max-age=31536000, immutable"
      }
    },
    {
      "route": "/images/*",
      "headers": {
        "Cache-Control": "public, max-age=86400"
      }
    }
  ]
}

Cache-Control Directives

DirectivePurpose
no-cacheRevalidate before use
no-storeDon't cache
publicCan be cached by any cache
privateOnly client can cache
max-age=<seconds>Cache validity duration
immutableResource never changes

Redirects

{
  "routes": [
    {
      "route": "/old-page",
      "redirect": "/new-page",
      "statusCode": 301
    },
    {
      "route": "/external",
      "redirect": "https://example.com",
      "statusCode": 302
    }
  ]
}

Redirect Status Codes

CodeUsage
301Permanent redirect (SEO-friendly)
302Temporary redirect

MIME Types

Specify content types for files:

{
  "mimeTypes": {
    ".json": "application/json",
    ".wasm": "application/wasm",
    ".webmanifest": "application/manifest+json"
  }
}

Complete Configuration Example

{
  "routes": [
    {
      "route": "/api/*",
      "allowedRoles": ["authenticated"],
      "headers": {
        "Cache-Control": "no-cache"
      }
    },
    {
      "route": "/admin/*",
      "allowedRoles": ["admin"]
    },
    {
      "route": "/static/*",
      "headers": {
        "Cache-Control": "public, max-age=31536000, immutable"
      }
    },
    {
      "route": "/old-path",
      "redirect": "/new-path",
      "statusCode": 301
    },
    {
      "route": "/*",
      "serve": "/index.html",
      "statusCode": 200
    }
  ],
  "navigationFallback": {
    "rewrite": "/index.html",
    "exclude": [
      "/api/*",
      "/*.{css,scss,js,png,gif,ico,jpg,svg,woff,woff2,ttf,eot}"
    ]
  },
  "responseOverrides": {
    "401": {
      "redirect": "/.auth/login/github",
      "statusCode": 302
    },
    "404": {
      "rewrite": "/404.html"
    }
  },
  "globalHeaders": {
    "content-security-policy": "default-src 'self'; script-src 'self' 'unsafe-inline'",
    "X-Frame-Options": "DENY",
    "X-Content-Type-Options": "nosniff",
    "X-XSS-Protection": "1; mode=block"
  }
}