Isaac.

Azure Static Web Apps (SWA)

Table of Contents

  • Core Concept: What is it?
  • Key Features & Benefits
  • How It Works: The Developer Workflow
  • Example: A Simple React App with an API
  • Ideal Use Cases
  • Limitations
  • Summary

Let's dive into Azure Static Web Apps (SWA).

This is a modern service designed to simplify the deployment and hosting of modern web applications. It's an excellent choice for projects built with popular frameworks like React, Angular, Vue, Svelte, or even plain HTML/CSS/JavaScript.

Core Concept: What is it?

In simple terms, Azure Static Web Apps is a service that:

  1. Hosts static content (HTML, CSS, JavaScript, images) from a code repository.
  2. Can be enhanced with serverless API functions(using Azure Functions), providing a full-stack solution.
  3. Offers global distribution for fast loading times.
  4. Provides features like authentication, authorization, and custom routing out-of-the-box.

Its biggest selling point is the incredibly smooth developer experience, especially its tight integration with GitHub for continuous deployment.


Key Features & Benefits

  1. Integrated CI/CD (Continuous Integration/Deployment):
    • You connect your GitHub repository to Azure Static Web Apps.
    • Whenever you push code to a specific branch (e.g., main), a GitHub Action automatically builds and deploys your app.
  2. Global Availability & High Performance:

    Your static content is automatically distributed to servers around the world (via a CDN), resulting in faster load times.

  3. Free SSL Certificates (HTTPS):

    Every SWA gets a free, automatically managed SSL certificate, ensuring secure connections.

  4. Serverless API Support:

    You can include an api folder in your project with Azure Functions. SWA seamlessly proxies requests to these endpoints without CORS issues.

  5. Built-in Authentication & Authorization:
    • SWA provides auth out-of-the-box for providers like Azure AD, GitHub, Facebook, Twitter, and Google.
    • Access rules can be defined in staticwebapp.config.json to restrict routes such as /admin.
  6. Staging Environments:

    Pull requests automatically generate staging environments with unique URLs for testing.

  7. Cost-Effective:

    Generous free tier for low-traffic sites. Costs mainly apply when exceeding free serverless API usage.


How It Works: The Developer Workflow

  1. Code: Develop your app in local repo (app/ for front-end, api/ for functions).
  2. Push: Push to GitHub repository.
  3. Create Resource: Create Static Web App resource in Azure Portal and configure paths.
  4. Automation: GitHub Actions workflow is created in .github/workflows/.
  5. Deploy: Pushes to main trigger build and deployment.

Example: A Simple React App with an API

my-todo-app/
├── api/
│   └── getTodos.js          # Azure Function that returns todos
├── app/
│   ├── public/
│   ├── src/
│   ├── package.json
│   └── (other React files)
└── .github/workflows/
    └── azure-static-web-apps-xxx.yml # Auto-generated workflow file

Configuration:

  • App Location: app
  • Api Location: api
  • Output Location: build

A request to https://your-app.azurewebsites.net/api/getTodos routes directly to the function.


Ideal Use Cases

  • JAMstack Sites: Blogs, marketing websites, portfolios.
  • SPAs with React, Vue, Angular.
  • PWAs needing offline support.
  • Front-ends for APIs.

Limitations

  • Static First: No native SSR. Supports SSG and CSR.
  • No Native WebSockets: Serverless backend only.
  • Vendor Lock-in: Auth and API integration are Azure-specific.

Summary

Azure Static Web Apps is a fantastic platform that removes the complexity of hosting, scaling, and securing modern web applications. If your project fits the JAMstack model, it's often the fastest and most cost-effective way to reach production.