Serverless computing is a rapidly growing type of cloud computing that allows developers to build scalable and cost-effective applications without worrying about the underlying infrastructure. Azure Static Web Apps is a serverless solution that provides an easy way to deploy and host static web apps using Azure services.
In this tutorial, you will learn how to build and deploy a serverless web app using Azure Static Web Apps and a simple JavaScript framework, Vue.js.
Prerequisites
Before you get started, you will need the following:
- An Azure account. If you donโt have an Azure account, you can create one for free here.
- Node.js and npm installed on your machine. You can download Node.js from the official website here.
- A code editor. For this tutorial, we will be using Visual Studio Code, which you can download here.
- A basic knowledge of HTML, CSS, and JavaScript.
Create a new Vue.js project
The first step is to create a new Vue.js project. If you already have a project, you can skip this step.
- Open a terminal window and navigate to the directory where you want to store your project.
- Run the following command to create a new Vue.js project:
vue create my-app
Replace
my-app
with the name of your project. -
Follow the prompts in the command line to select options for your project setup. Choose the default options for this tutorial.
-
Once the project is created, navigate to the project directory by running the following command:
cd my-app
Install dependencies
Now that you have created a new Vue.js project, the next step is to install the dependencies required to build a serverless web app using Azure Static Web Apps.
- Open a terminal window and navigate to your project directory.
- Run the following command to install the required dependencies:
npm install --save-dev @azure/static-web-apps-cli
This will install the Azure Static Web Apps CLI as a dev dependency.
Set up a GitHub repository
You will need to set up a GitHub repository to connect to Azure Static Web Apps.
- Create a new repository on GitHub by following the instructions on the GitHub website.
- Clone the new repository to your local machine by running the following command:
git clone https://github.com/username/my-app.git
Replace
username
with your GitHub username andmy-app
with the name of your repository. -
Navigate to the newly cloned repository by running the following command:
cd my-app
Configure your Azure Static Web App
Once you have your GitHub repository set up, you can configure your Azure Static Web App.
- Open a terminal window and navigate to your project directory.
- Run the following command to log in to your Azure account:
az login
Follow the prompts in the command line to authenticate with your Azure account.
-
Run the following command to create a new Azure Static Web App:
az staticwebapp create --name my-app --sku Free --resource-group my-resource-group --location us-west --source . --branch main
Replace
my-app
with the name of your app,my-resource-group
with the name of your resource group, andus-west
with the Azure region you want to deploy to. You can choose any region that is available in your Azure account. -
Follow the prompts in the command line to complete the setup process. When prompted for the build command, enter:
npm run build
This will build your Vue.js app for production.
-
After the setup process is complete, the command line will display the URL for your Azure Static Web App. You can now visit your app by navigating to this URL in a web browser.
Update your Vue.js app
Now that your Azure Static Web App is set up, you are ready to update your Vue.js app.
- Open your Vue.js project in your chosen code editor.
- Navigate to the
src
directory and open theApp.vue
file. - Replace the existing code with the following code:
<template> <div> <h1>{{ message }}</h1> </div> </template> <script> export default { data() { return { message: 'Hello, Azure Static Web Apps!' } } } </script>
This code will display a simple message on your web app.
-
Save the file and return to your terminal window.
-
Commit your changes by running the following command:
git add . git commit -m "Updated app message" git push
This will push your changes to your GitHub repository.
-
Visit your Azure Static Web App URL to see your changes in action.
Deploy changes automatically
By default, Azure Static Web Apps will deploy changes to your app automatically when you push changes to your GitHub repository.
- Make a small change to your Vue.js app by modifying the message in the
App.vue
file. - Save the file and commit your changes by running the following command:
git add . git commit -m "Updated app message" git push
This will push your changes to your GitHub repository.
-
After a few seconds, refresh your Azure Static Web App URL to see the updated message.
And thatโs it! You have now successfully built and deployed a serverless web app with Azure Static Web Apps and Vue.js. You can continue to update your app by making changes to your Vue.js project and pushing the changes to your GitHub repository. Azure Static Web Apps will automatically deploy your changes to your app, making it easy to maintain and update your serverless web app.