|
1 | 1 | ---
|
2 |
| -title: Setup local development for App Service Static Apps |
3 |
| -description: #Required; article description that is displayed in search results. |
4 |
| -services: #Required for articles that deal with a service; service slug assigned to your service by ACOM. |
| 2 | +title: Set up local development for Azure Static Web Apps |
| 3 | +description: Learn to set you your local development environment for Azure Static Web Apps |
| 4 | +services: static-web-apps |
5 | 5 | author: burkeholland
|
6 |
| -ms.service: azure-functions |
7 |
| -ms.topic: how-to |
| 6 | +ms.service: static-web-apps |
| 7 | +ms.topic: how-to |
8 | 8 | ms.date: 05/08/2020
|
9 | 9 | ms.author: buhollan
|
10 | 10 | ---
|
11 | 11 |
|
12 |
| -<!---Recommended: Removal all the comments in this template before you sign-off or merge to master.---> |
| 12 | +# Set up local development for Azure Static Web Apps |
13 | 13 |
|
14 |
| -# Setup local development for App Service Static Apps |
| 14 | +An Azure Static Web Apps instance is made up of two different types of applications. The first is a web app for your static content. Web apps are often created with front-end frameworks and libraries or with static site generators. The second aspect is the API, which is an Azure Functions app that provides a rich back-end development environment. |
15 | 15 |
|
16 |
| -Introductory paragraph. |
| 16 | +When running in the cloud, Azure Static Web Apps seamlessly maps requests to the `api` route from the web app to the Azure Functions app without requiring CORS configuration. Locally, you need to configure your application to mimic this behavior. |
17 | 17 |
|
18 |
| -<!---Required: |
19 |
| -Lead with a light intro that describes, in customer-friendly language, what the customer will learn, or do, or accomplish. Answer the fundamental "why would I want to do this?" question. |
20 |
| ----> |
| 18 | +This article demonstrates recommended best-practices for local development, including the following concepts: |
21 | 19 |
|
22 |
| -<!---Avoid notes, tips, and important boxes. Readers tend to skip over them. Better to put that info directly into the article text.---> |
| 20 | +- Set up the web app for static content |
| 21 | +- Configuring the Azure Functions app for your application's API |
| 22 | +- Debugging and running the application |
| 23 | +- Best-practices for your app's file and folder structure |
23 | 24 |
|
24 | 25 | ## Prerequisites
|
25 | 26 |
|
26 |
| -- First prerequisite |
27 |
| -- Second prerequisite |
28 |
| -- Third prerequisite |
29 |
| -<!--- Make Prerequisites your first H2 if you have them. |
30 |
| -If there's something a customer needs to take care of before they start (for example, creating a VM) it's OK to link to that content before they begin. |
31 |
| ----> |
| 27 | +- [Visual Studio Code](https://code.visualstudio.com/) |
| 28 | +- [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) for Visual Studio Code |
| 29 | +- [Live Server extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer) for Visual Studio Code |
| 30 | + - Necessary only if you're not using a front-end JavaScript framework or static site generator's CLI |
32 | 31 |
|
33 |
| -## <section> |
| 32 | +## Run projects locally |
34 | 33 |
|
35 |
| -<!---Detail what the reader needs to know in each section |
36 |
| ----> |
| 34 | +Running an Azure Static Web App locally involves three processes, depending on whether or not your project contains an API. |
37 | 35 |
|
38 |
| -Include a sentence or two to explain only what is needed to complete the procedure. |
| 36 | +- Running a local web server |
| 37 | +- Running the API |
| 38 | +- Connecting the web project to the API |
39 | 39 |
|
40 |
| -1. Step one of the procedure |
41 |
| -1. Step two of the procedure |
42 |
| -1. Step three of the procedure |
43 |
| - <!---  ---> |
44 |
| - <!---Use screenshots but be judicious to maintain a reasonable length. Make sure screenshots align to the [current standards](contribute-mvc-screen-shots.md). |
45 |
| - If users access your product/service via a web browser the first screenshot should always include the full browser window in Chrome or Safari. This is to show users that the portal is browser-based - OS and browser agnostic.---> |
46 |
| -1. Step four of the procedure |
| 40 | +Depending on how a website is built, a local web server may or may not be required to run the application in the browser. When using front-end JavaScript frameworks and static site generators, this functionality is built in to their respective CLIs (Command Line Interfaces). The following links point to the CLI reference for a selection of frameworks, libraries, and generators. |
47 | 41 |
|
48 |
| -## Next steps |
| 42 | +### JavaScript frameworks and libraries |
49 | 43 |
|
50 |
| -<!-- Uncomment this block and add the appropriate link |
| 44 | +- [Angular CLI](https://angular.io/cli) |
| 45 | +- [Vue CLI](https://cli.vuejs.org/guide/creating-a-project.html) |
| 46 | +- [React CLI](https://create-react-app.dev/) |
51 | 47 |
|
52 |
| -> [!div class="nextstepaction"] |
53 |
| -> [Next steps button](contribute-get-started-mvc.md) |
| 48 | +### Static site generators |
| 49 | + |
| 50 | +- [Gatsby CLI](https://www.gatsbyjs.org/docs/gatsby-cli/) |
| 51 | +- [Hugo](https://gohugo.io/getting-started/quick-start/) |
| 52 | +- [Jekyll](https://jekyllrb.com/docs/usage/) |
| 53 | + |
| 54 | +If you're using a CLI tool to serve your site, you can skip to the [Running the API](#run-api-locally) section. |
| 55 | + |
| 56 | +### Running a local web server with Live Server |
| 57 | + |
| 58 | +The Live Server extension for Visual Studio Code provides a local development web server that serves static content. |
| 59 | + |
| 60 | +#### Create a repository |
| 61 | + |
| 62 | +1. Navigate to [https://github.com/staticwebdev/vanilla-api/generate](https://github.com/staticwebdev/vanilla-api/generate) and create a new GitHub project named **vanilla-api**, using this template. |
| 63 | + |
| 64 | + :::image type="content" source="media/local-development/vanilla-api.png" alt-text="GitHub new repo window"::: |
| 65 | + |
| 66 | +1. Open Visual Studio Code. |
| 67 | + |
| 68 | +1. Press **F1** to open the Command Palette. |
| 69 | + |
| 70 | +1. Type **clone** in the search box and select **Git: Clone**. |
| 71 | + |
| 72 | + :::image type="content" source="media/local-development/command-palette-git-clone.png" alt-text="git clone option in Visual Studio Code"::: |
| 73 | + |
| 74 | +1. Enter the following value for **Repository URL**. |
| 75 | + |
| 76 | + ```http |
| 77 | + [email protected]:<YOUR_GITHUB_ACCOUNT>/vanilla-api.git |
| 78 | + ``` |
| 79 | + |
| 80 | +1. Select a folder location for the new project. |
| 81 | + |
| 82 | +1. When prompted to open the cloned repository, select **Open**. |
| 83 | + |
| 84 | + :::image type="content" source="media/local-development/open-new-window.png" alt-text="Open in new window"::: |
| 85 | + |
| 86 | +Visual Studio Code opens the cloned project in the editor. |
| 87 | + |
| 88 | +### Run the website locally with Live Server |
| 89 | + |
| 90 | +1. Press **F1** to open the Command Palette. |
| 91 | + |
| 92 | +1. Type **Live Server** in the search box and select **Live Server: Open with Live Server** |
| 93 | + |
| 94 | + A browser tab opens to display the application. |
| 95 | + |
| 96 | + :::image type="content" source="media/local-development/vanilla-api-site.png" alt-text="Simple static site running in the browser"::: |
| 97 | + |
| 98 | + This application makes an HTTP request to the `api/message` endpoint. Right now, that request is failing because the API portion of this application needs to be started. |
| 99 | + |
| 100 | +### Run API locally |
| 101 | + |
| 102 | +Azure Static Web Apps APIs are powered by Azure Functions. See [Add an API to Azure Static Web Apps with Azure Functions](add-api.md) for details regarding adding an API to an Azure Static Web Apps project. |
| 103 | + |
| 104 | +As part of the API creation process, a launch configuration is created for Visual Studio Code. This configuration is located in the _.vscode_ folder. This folder contains all of the required settings for building and running the API locally. |
| 105 | + |
| 106 | +1. In Visual Studio Code, press **F5** to start the API. |
| 107 | + |
| 108 | +1. A new terminal instance opens showing the output from the API build process. |
| 109 | + |
| 110 | + :::image type="content" source="media/local-development/terminal-api-debug.png" alt-text="API running in Visual Studio Code terminal"::: |
| 111 | + |
| 112 | + The status bar in Visual Studio Code is now orange. This color indicates that the API is now running and the debugger is attached. |
| 113 | + |
| 114 | +1. Next, press **Ctrl/Cmd** and click on the URL in the terminal to open a browser window that calls the API. |
| 115 | + |
| 116 | + :::image type="content" source="media/local-development/hello-from-api-endpoint.png" alt-text="Browser display result of API call"::: |
| 117 | + |
| 118 | +### Debugging the API |
| 119 | + |
| 120 | +1. Open the _api/GetMessage/index.js_ file in Visual Studio Code. |
54 | 121 |
|
55 |
| ---> |
| 122 | +1. Click in the left-hand margin on line 2 to set a breakpoint. A red dot appears which indicates the breakpoint is set. |
56 | 123 |
|
57 |
| -<!--- Required: |
58 |
| -A single link in the blue box format should direct the customer to the next article - and you can shorten the title in the boxes if the original one doesn't fit. |
59 |
| ----> |
| 124 | + :::image type="content" source="media/local-development/breakpoint-set.png" alt-text="Breakpoint in Visual Studio Code"::: |
| 125 | + |
| 126 | +1. In the browser, refresh the page running at <http://127.0.0.1:7071/api/message>. |
| 127 | + |
| 128 | +1. The breakpoint is hit in Visual Studio Code and program execution is paused. |
| 129 | + |
| 130 | + :::image type="content" source="media/local-development/breakpoint-hit.png" alt-text="Breakpoint hit in Visual Studio Code"::: |
| 131 | + |
| 132 | + A complete [debugging experience is available in Visual Studio Code](https://code.visualstudio.com/Docs/editor/debugging) for your API. |
| 133 | + |
| 134 | +1. Press the **Continue** button in the debug bar to continue execution. |
| 135 | + |
| 136 | + :::image type="content" source="media/local-development/continue-button.png" alt-text="Continue button in Visual Studio Code"::: |
| 137 | + |
| 138 | +### Calling the API from the application |
| 139 | + |
| 140 | +When deployed, Azure Static Web Apps automatically maps these requests to the endpoints in the _api_ folder. This mapping ensures that requests from the application to the API look like the following example. |
| 141 | + |
| 142 | +```javascript |
| 143 | +let response = await fetch("/api/message"); |
| 144 | +``` |
| 145 | + |
| 146 | +Depending on whether or not your application is built with a JavaScript framework CLI, there are two ways to configure the path to the `api` route when running your application locally. |
| 147 | + |
| 148 | +- Environment configuration files (recommended for JavaScript frameworks and libraries) |
| 149 | +- Local proxy |
| 150 | + |
| 151 | +### Environment configuration files |
| 152 | + |
| 153 | +If you are building your app with front-end frameworks that have a CLI, you should use environment configuration files. Each framework or library has a different way of handling these environment configuration files. It's common to have a configuration file for development that is used when your application is running locally, and one for production that is used when your application is running in production. The CLI for the JavaScript framework or static site generator that you are using will automatically know to use the development file locally and the production file when your app is built by Azure Static Web Apps. |
| 154 | + |
| 155 | +In the development configuration file, you can specify the path to the API, which points to the local location of `http:127.0.0.1:7071` where the API for your site is running locally. |
| 156 | + |
| 157 | +``` |
| 158 | +API=http:127.0.0.1:7071/api |
| 159 | +``` |
| 160 | + |
| 161 | +In the production configuration file, specify the path to the API as `api`. This way your application will call the api via "yoursite.com/api" when running in production. |
| 162 | + |
| 163 | +``` |
| 164 | +API=api |
| 165 | +``` |
| 166 | + |
| 167 | +These configuration values can be referenced as Node environment variables in the web app's JavaScript. |
| 168 | + |
| 169 | +```js |
| 170 | +let response = await fetch(`${process.env.API}/message`); |
| 171 | +``` |
| 172 | + |
| 173 | +When the CLI is used to run your site in development mode or to build the site for production, the `process.env.API` value is replaced with the value from the appropriate configuration file. |
| 174 | + |
| 175 | +For more information on configuring environment files for front-end JavaScript frameworks and libraries, see these articles: |
| 176 | + |
| 177 | +- [Angular Environment Variables](https://angular.io/guide/build#configuring-application-environments) |
| 178 | +- [React - Adding Custom Environment Variables](https://create-react-app.dev/docs/adding-custom-environment-variables/) |
| 179 | +- [Vue - Modes and Environment Variables](https://cli.vuejs.org/guide/mode-and-env.html) |
| 180 | + |
| 181 | +[!INCLUDE [static-web-apps-local-proxy](../../includes/static-web-apps-local-proxy.md)] |
| 182 | + |
| 183 | +##### Restart Live Server |
| 184 | + |
| 185 | +1. Press **F1** to open the Command Palette in Visual Studio Code. |
| 186 | + |
| 187 | +1. Type **Live Server** and select **Live Server: Stop Live Server**. |
| 188 | + |
| 189 | + :::image type="content" source="media/local-development/stop-live-server.png" alt-text="Stop Live Server command in Visual Studio command palette"::: |
| 190 | + |
| 191 | +1. Press **F1** to open the Command Palette. |
| 192 | + |
| 193 | +1. Type **Live Server** and select **Live Server: Open with Live Server**. |
| 194 | + |
| 195 | +1. Refresh the application running at `http://locahost:3000`. The browser now displays the message returned from the API. |
| 196 | + |
| 197 | + :::image type="content" source="media/local-development/hello-from-api.png" alt-text="Hello from API displayed in the browser"::: |
| 198 | + |
| 199 | +## Next steps |
| 200 | + |
| 201 | +> [!div class="nextstepaction"] |
| 202 | +> [Configure app settings](application-settings.md) |
0 commit comments