Description
Description/Screenshot
@microsoft/applicationinsights-web
breaks Angular SSR in Cloudflare Worker by 1. redefining "name" property, which is not allowed by CF Worker and 2. make the rendering stuck (page not loading) for unknown reasons.
Steps to Reproduce
- Create a simple Angular SSR project with Cloudflare Worker (
pnpm create cloudflare@latest my-angular-app --framework=angular
) Ref - Install
@microsoft/applicationinsights-web
- Create the AppInsights instance in
app.component.ts
, for example:
constructor() {
const _appInsights = new ApplicationInsights({
config: {
connectionString: '123123',
enableAutoRouteTracking: true,
enableCorsCorrelation: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
}
});
}
- Make sure the rendering mode is set to Server in
app.route.server.ts
, like
import { RenderMode, ServerRoute } from '@angular/ssr';
export const serverRoutes: ServerRoute[] = [
{
path: '**',
renderMode: RenderMode.Server
}
];
- Build the project and either deploy it to Cloudflare Worker or run locally with wrangler (i.e.,
npx pnpm build && npx wranger dev
) - Open browser to access the running site
- OS/Browser: N/A
- SDK Version [e.g. 22]:
3.3.6
- How you initialized the SDK:
this._appInsights = new ApplicationInsights({
config: {
connectionString: environment.appInsights.connectionString,
enableAutoRouteTracking: true,
enableCorsCorrelation: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
}
});
this._appInsights.loadAppInsights();
Expected behavior
Website loaded correctly
Additional context
The SDK introduces two bugs:
- it somehow triggered
esbuild
to redefinename
property. When runs in Cloudflare Worker, it cause error in the logs saying
Cannot redefine property: name
and the stack trace points to the bundled file
at defineProperty (<anonymous>)
at __name (server.js:7:33)
at dist/<redacted>/server/en-CA/chunk-M5SVHNEI.mjs (server.js:36770:5)
at __init (server.js:9:62)
at dist/<redacted>/server/en-CA/chunk-7DM6DADX.mjs (server.js:45727:5)
at __init (server.js:9:62)
at dist/<redacted>/server/en-CA/main.server.mjs (server.js:60053:5)
at __init (server.js:9:62)
at server.js:149095:73
at async e18.getAngularServerAppForRequest (server.js:149484:35)
When running locally with wrangler
, the error message is:
[wrangler:err] Error: Method not implemented.
at CallSite.toString (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:11069:11)
at prepareStackTrace (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/@cspotcode/source-map-support/source-map-support.js:671:39)
at sourceMapper (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:11177:12)
at getSourceMappedStack (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:11235:27)
at reviveError (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:11268:17)
at handlePrettyErrorRequest (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:11273:17)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async #handleLoopback (/Users/<redacted>/.npm/_npx/32026684e21afda6/node_modules/miniflare/dist/src/index.js:15598:20)
This bug can be workaround by manually invoke esbuild
with preserveNames=false
and instruct wrangler
to skip bundle. However, it will trigger the next bug.
2. It somehow make the entire rendering process unresponsive. This issue is consistent between running in CF Worker and locally. There are no error message, but the page loading was just hang. After an hour, you can observe crazy Wall Time (e.g., 1,500,581ms
) in Cloudflare Worker logs. If you really want to wait, the page might or might not loaded eventually.
In order to trigger the above bugs, appInsights doesn't need to be loaded (i.e., remove _appInsights.loadAppInsights()
won't help). Simply creating ApplicationInsights
(i.e., new ApplicationInsights({})
) object will trigger the bugs.
The workaround is to check if it is in SSR mode, and load appInsights dynamically when in CSR
private async lazyLoadAppInsights(connectionString: string) {
try {
// Dynamically import the module only at runtime in browser
const appInsights = await import('@microsoft/applicationinsights-web');
const ApplicationInsights = appInsights.ApplicationInsights;
this._appInsights = new ApplicationInsights({
config: {
connectionString: connectionString,
enableAutoRouteTracking: true,
enableCorsCorrelation: false,
enableRequestHeaderTracking: true,
enableResponseHeaderTracking: true,
}
});
this._appInsights.loadAppInsights();
} catch (error) {
console.error('Failed to initialize ApplicationInsights:', error);
}
}
Activity
MSNev commentedon May 19, 2025
I've created a duplication issue in the dynamicProto repo to fix / address this in dynamicProto microsoft/DynamicProto-JS#95
MSNev commentedon May 19, 2025
Based on the identified fixes in microsoft/DynamicProto-JS#96, I'm not convinced that this is the root (or only) cause of this issue, ignoring the usage of dynamicProto what else could cause this issue?
Or does the stack trace specifically identify dynamicProto as the root cause? And if so what line
MSNev commentedon May 20, 2025
@xionglingfeng After using Co-Pilot to perform some in-depth analysis / evaluation of the code, we have not identified any specific issue within the Application Insights code, we also don't use or define the
__name
property.One of the last comments from Co-Pilot is
And is specifically states the following, which seems like a likely root cause and not something specific to the Application Insights code, is this something that you can help confirm or deny?
I am still using Co-Pilot to try and identify any specific detection or work-around that we might be able to implement, but at this point I'm not seeing anything that pinpoints the issue that we have.
Question: Does this environment have any specific polyfills / extensions to root objects like Array? As there was this issue that will be fixed in the next release that is caused by the end-users runtime adding a
set
function toArray.prototype
which then cause the core dynamic configuration code to get confused...xionglingfeng commentedon May 20, 2025
@MSNev you might find its helpful to run follow the steps I posted in the bug report. I don't think your code directly redefined the
name
property viaObject.defineProperty
. Rather, I think something in your code makesesbuild
to believe adding that line is necessary.In my original bug report:
To me, this is quite convincing that the bug is not because your code redefined
name
, but somehow your code makeesbuild
to believe it is necessary to redefinename
MSNev commentedon May 22, 2025
@xionglingfeng , I redirected Co-Pilot at your comment above and it came up with the following PR (eventually), does this sound like a reasonable solution that would work for you?
https://github.com/microsoft/ApplicationInsights-JS/pull/2548/files
I might keep prompting Co-Pilot to investigate deeper, if this doesn't work.
However, I'm convinced that as a good general solution, but I'll leave the PR as is for now so you can review
xionglingfeng commentedon May 22, 2025
Hi @MSNev, the doc update make sense.
However, I think the root cause is buried somewhere deeper, and, frankly, I am not sure if the LLM at this stage has a good chance to find it.
MSNev commentedon May 22, 2025
I Understand.
We are not experts on CloudFlare (which is why I'm throwing LLM at it), to try to see if it can provide some insights that might trigger some memories or identify a possible path forward.
As we have a general goal of not impacting any runtime that we might get loaded into ie we either workaround limitations or we have a graceful fallback. Both of which I'm currently uncertain on how do achieve this at this point. -- without spending lots of time re-creating a repo (hmm -- sound like something I might be able to get the LLM to try and do for us)
And as an FYI, we are a small team which limits our capacity as well as being impacted by recent events.
xionglingfeng commentedon May 22, 2025
That's definitely understandable.
The issue is relatively easy to be reproduced with one npm command as I wrote in the original bug report. At least the first bug is not related to Cloudflare Worker, its more related to
esbuild
. I am not familiar withesbuild
's behaviour, norAppInsights-JS
's implementation, thus I am unable to dig further what exactly causeesbuild
to generate the line redefiningname
.frasermclean commentedon Jun 12, 2025
Just wanted to say that I experienced the exact same issue when trying to deploy my Angular SSR app with Application Insights to Cloudflare Worker.
I had no idea what was causing it.