Skip to content

Commit

Permalink
add type
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms committed May 9, 2024
1 parent db05c84 commit 46a67ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
34 changes: 19 additions & 15 deletions tools/applicationinsights-web-snippet/src/miniLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,49 +207,53 @@ declare var cfg:ISnippetConfig;
var sender = window.fetch;
var endpointUrl = "https://js.monitor.azure.com/beta/ai.3.integrity.json";
var strPostMethod = "GET"; // Assuming this is a GET request based on the example

let targetSrc = (aiConfig as any)["url"] || cfg.src;
let targetType = "@" + (cfg.type || "gbl.min.js");
let file = "ai.3.gbl.min.js"; // Default to 3 if we can't get the version
var integrity = null;

if (targetSrc) {
if (sender && !cfg.useXhr) {
sender(endpointUrl, { method: strPostMethod, mode: "cors" })
.then(response => response.json())
.then(json => {
let currentVersion = json.version;
setScript(currentVersion, targetSrc);
})
.catch(error => console.error("Error loading JSON:", error));
.then(response => response.json())
.then(json => {
file = json.ext[targetType].file;
integrity = json.ext[targetType].integrity;
targetSrc = targetSrc.replace(/ai\.\d+\..+\.js/, file);
setScript(targetSrc);
})
.catch(error => console.error("Error loading JSON:", error));
} else if (XMLHttpRequest) {
var xhr = new XMLHttpRequest();
xhr.open(strPostMethod, endpointUrl);
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
var currentVersion = json.version;
setScript(currentVersion, targetSrc);
file = json.ext[targetType].file;
integrity = json.ext[targetType].integrity;
targetSrc = targetSrc.replace(/ai\.\d+\..+\.js/, file);
setScript(targetSrc);
} else {
console.error("Error loading JSON:", xhr.statusText);
}
}
};
xhr.send();
} else {
setScript("3", targetSrc);
setScript(targetSrc); // Fallback to original behavior
}
}


function setScript(currentVersion: string, targetSrc: string) {
function setScript(targetSrc: string) {
if (isIE() && targetSrc.indexOf("ai.3") !== -1) {
// This regex matches any URL which contains "\ai.3." but not any full versions like "\ai.3.1" etc
targetSrc = targetSrc.replace(/(\/)(ai\.3\.)([^\d]*)$/, function(_all, g1, g2) {
return g1 + "ai.2" + g2;
});
// let message = "Load Version 2 SDK instead to support IE"; // where to report this error?
}
if (currentVersion) {
targetSrc = targetSrc.replace(/\/ai\.\d+\./, "/ai." + currentVersion + ".");
}

if (cfg.cr !== false){
for (var i = 0; i < domains.length; i++){
Expand Down Expand Up @@ -323,7 +327,7 @@ declare var cfg:ISnippetConfig;
return scriptElement;
}
_createScript(targetSrc);
}
}
// capture initial cookie
try {
appInsights.cookie = doc.cookie;
Expand Down
1 change: 1 addition & 0 deletions tools/applicationinsights-web-snippet/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ISnippetConfig {
cfg: IConfiguration;
cr?: boolean; // cdn retry would be proceed if ture
dle?: boolean; // Custom optional value to disable sdk load error to be sent
type?: string; // Custom optional value to specify the type of the snippet, e.g. "cjs.js"
}

export interface Fields {
Expand Down

0 comments on commit 46a67ae

Please sign in to comment.