Skip to content

Commit d45145c

Browse files
Josmithralexvy86
andauthored
build(docs): Fix ApplicationInsights initialization for local dev builds (#24150)
As written, initialization of `ApplicationInsights` would fail when running the site locally if `INSTRUMENTATION_KEY` is not set. This PR updates it to simply not initialize `ApplicationInsights` if `INSTRUMENTATION_KEY` is not set (which matches what we do in production). Also makes some updates to `env.template` comments. --------- Co-authored-by: Alex Villarreal <[email protected]>
1 parent 3c2b96a commit d45145c

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

docs/.env.template

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# `.env` file template.
2-
# Copy these contents into a local `.env` file in this directoy and fill in the values as needed.
2+
# Copy these contents into a local `.env` file in this directory and fill in the values as needed.
33
# `.env` is configured to be git-ignored. Please do not override this behavior.
44

55
# Flag that determines whether or not repo-local API documentation should be generated and included in the site build.
66
# Only used in local development.
7+
# E.g. LOCAL_API_DOCS=true
78
LOCAL_API_DOCS=<boolean>
89

9-
# Key used to authenticate with the Azure Application Insights API. Refer to README.md for more information.
10+
# Key used to authenticate with the Azure Application Insights API.
11+
# Refer to README.md for more information.
1012
INSTRUMENTATION_KEY=<instrumentation_key>

docs/src/appInsights.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ import { ApplicationInsights } from "@microsoft/applicationinsights-web";
1111
const reactPlugin = new ReactPlugin();
1212
let appInsights: ApplicationInsights | undefined;
1313

14-
if (siteConfig?.customFields?.INSTRUMENTATION_KEY === undefined) {
15-
console.warn("Instrumentation Key is missing. App Insights will not be initialized.");
16-
}
17-
1814
// Only initialize Application Insights if not in local development.
1915
// Remove the condition if you want to run Application Insights locally.
2016
if (typeof window !== "undefined" && window.location.hostname !== "localhost") {
21-
appInsights = new ApplicationInsights({
22-
config: {
23-
connectionString: `InstrumentationKey=${siteConfig?.customFields?.INSTRUMENTATION_KEY}`,
24-
enableAutoRouteTracking: true,
25-
enableDebug: true,
26-
extensions: [reactPlugin],
27-
},
28-
});
29-
appInsights.loadAppInsights();
17+
const instrumentationKey = siteConfig?.customFields?.INSTRUMENTATION_KEY;
18+
if (instrumentationKey === undefined) {
19+
console.warn("Instrumentation Key is missing. App Insights will not be initialized.");
20+
} else {
21+
appInsights = new ApplicationInsights({
22+
config: {
23+
connectionString: `InstrumentationKey=${instrumentationKey}`,
24+
enableAutoRouteTracking: true,
25+
enableDebug: true,
26+
extensions: [reactPlugin],
27+
},
28+
});
29+
appInsights.loadAppInsights();
30+
}
3031
}
3132

3233
export default appInsights;

0 commit comments

Comments
 (0)