Deploying a Vite-based React application that uses React Router to Azure App

By | 4 months ago

React Azure App ServiceVitedeployment

Deploying a Vite-based React application that uses React Router to Azure App Service involves several steps. Here’s a detailed guide to help you through the process:

1. Prepare Your Application

First, ensure your React application is ready for deployment:

  • **Install Dependencies**: Make sure all necessary dependencies are installed in your project. If you haven’t already, initialize your project with Vite:

    npm create vite@latest my-react-app -- --template react cd my-react-app npm install \`\`\`
  • **Set Up React Router**: If React Router isn't set up yet, install it and configure your routes:

    npm install react-router-dom \`\`\`
  • **Configure Base Path**: Vite requires you to specify the base path if your app is served from a subdirectory. Update the `vite.config.js` file:

    export default defineConfig({ base: '/production-sub-path/', plugins: [react()] }); \`\`\`

2. Build Your Application

Build your React application using Vite. This process bundles your app into static files for production.

npm run build

3. Prepare for Azure Deployment

  • **Create a `web.config` File**: Azure App Service uses IIS, so you need to configure it to correctly handle the client-side routing set up by React Router.

    • Create a `web.config` file in your project's root directory with the following content:

      <?xml version="1.0"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="React Routes" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> </system.webServer> </configuration> \`\`\`

4. Deploy to Azure App Service

  • **Create an Azure App Service**: Go to the Azure Portal, create a new App Service, and configure it according to your preferences.

  • **Set up Deployment**: You can deploy your application to Azure App Service using FTP, local Git, GitHub, or Azure DevOps. Choose the one that best fits your workflow.

    **Example: Deploy using Azure CLI**

    • Install Azure CLI if you haven't already.

    • Log in to your Azure account:

      az login \`\`\`
    • Deploy your app:

      az webapp up --name <your-app-name> --resource-group <your-resource-group> --location <region> --html \`\`\`

5. Verify Deployment

After deployment, access your Azure App Service URL to see if the application is running correctly. Make sure that client-side routing works as expected and that all routes are accessible.

6. Troubleshooting

If you encounter issues:

  • Check the Azure App Service logs for any errors.

  • Ensure the `web.config` file is correctly set up to handle React Router.

  • Verify that all environment variables and settings in Azure match those required by your application.

By following these steps, your Vite-based React app with React Router should be successfully deployed and running on Azure App Service.