Skip to content

Commit de2b2a9

Browse files
Merge pull request #242640 from craigshoemaker/swa/snippets
[Static Web Apps] Snippets (new article)
2 parents 816e6a7 + cf7437f commit de2b2a9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

articles/static-web-apps/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
- name: Enable monitoring
136136
href: monitor.md
137137
displayName: Application Insights
138+
- name: Inject custom code at runtime
139+
href: snippets.md
138140
- name: Reset deployment tokens
139141
href: deployment-token-management.md
140142
- name: Troubleshoot errors

articles/static-web-apps/snippets.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Snippets in Azure Static Web Apps (preview)
3+
description: Inject custom code in the HEAD or BODY elements at runtime in Azure Static Web Apps
4+
services: static-web-apps
5+
author: craigshoemaker
6+
ms.service: static-web-apps
7+
ms.topic: overview
8+
ms.date: 06/22/2023
9+
ms.author: cshoe
10+
---
11+
12+
# Snippets in Azure Static Web Apps (preview)
13+
14+
Azure Static Web Apps allows you to inject custom code into the `head` or `body` elements at runtime. These pieces of code are known as *snippets*.
15+
16+
Snippets give you the flexibility to add code to every page in your site in a single place, all without modifying the core codebase.
17+
18+
Common use cases of snippets include:
19+
20+
- Analytics scripts
21+
- Common scripts
22+
- Global UI elements
23+
24+
> [!NOTE]
25+
> Some front-end frameworks may overwrite your snippet code. Test your snippets before applying them to a production environment.
26+
27+
## Add a snippet
28+
29+
1. Go to your static web app in the Azure portal.
30+
31+
1. From the *Settings* menu, select **Configuration**.
32+
33+
1. Select the **Snippets** tab.
34+
35+
1. Select the **Add** button.
36+
37+
1. Enter the following settings in the Snippets window:
38+
39+
| Setting | Value | Comments |
40+
|---|---|---|
41+
| Location | Select which HTML page element you want your code injected into. | |
42+
| Name | Enter a snippet name. | |
43+
| Insertion location | Select whether you want to **Prepend** or **Append** your code to the selected element. | *Prepend* means your code appears directly after the open tag of the element. *Append* means your code appears directly before the close tag of the element. |
44+
| Environment | Select the environment(s) you want to target. | If you pick **Select environment**, then you can choose from different environments to target. |
45+
46+
1. Enter your code in the text box.
47+
48+
1. Select **OK** to close the window.
49+
50+
1. Select **Save** to commit your changes.
51+
52+
## Next steps
53+
54+
> [!div class="nextstepaction"]
55+
> [Split traffic](./traffic-splitting.md)

0 commit comments

Comments
 (0)