Skip to content

Allow late instrumentation of globalThis.fetch #57527

Closed
@BenoitZugmeyer

Description

@BenoitZugmeyer

Which @angular/* package(s) are relevant/related to the feature request?

common

Description

I am working on the Datadog Browser SDK, and we have customers having issues when using withFetch().

Our RUM SDK, like many other RUM solutions, is replacing the fetch global with an instrumented version (see our code, Sentry code, OpenTelemetry code). This works fine most of the time, but because Angular 18 is storing a reference of the fetch global internally, users willing to use withFetch() have to load and initialize our SDK before starting their Angular app, else Angular will use the native fetch instead of our instrumented version. This is not ideal as it forces the Angular app to wait for our SDK to be loaded and initialized before starting.

Proposed solution

Keep using the global fetch variable. This could be done by replacing

  private readonly fetchImpl =
    inject(FetchFactory, {optional: true})?.fetch ?? fetch.bind(globalThis);

with

  private readonly fetchImpl =
    inject(FetchFactory, {optional: true})?.fetch ?? (...args) => globalThis.fetch(...args)

here

Alternatives considered

Ask Angular 18 users to initialize third-party instrumentation libraries before starting their Angular app.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions