Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[main] use proper URL for tracking when fetch is passed an empty string #2165

Merged
merged 3 commits into from
Sep 25, 2023

Conversation

jorupp
Copy link
Contributor

@jorupp jorupp commented Sep 25, 2023

This allows AI to track the dependency and sets the Traceparent header on the request to the server (if otherwise allowed).

Traceparent is now set
image

Fully-populated dependency is sent to ApplicationInsights
image
image

Data is displayed in Application Insights
image

Fixes #2164

@jorupp jorupp force-pushed the bugfix/2164-fetch-empty-string-tracking branch from cb703c2 to 2ce78e7 Compare September 25, 2023 14:06
…empty string

This allows AI to track it and sets the Traceparent header
@jorupp jorupp force-pushed the bugfix/2164-fetch-empty-string-tracking branch from 2ce78e7 to 6ee8dd4 Compare September 25, 2023 14:08
@@ -1186,6 +1186,9 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu

if (input instanceof Request) {
ajaxData.requestUrl = input ? input.url : "";
} else if (input === "" ) {
const location = getLocation();
Copy link
Contributor Author

@jorupp jorupp Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seemed to be the way existing code was accessing window.location

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now this makes a little more sense, your using an empty URL "" So that you can talk back to the same domain as the hosting page regardless of which environment you are deployed in (or page your hosted on)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, if i wrote the code, I wouldn't have used "", but that's how NextJS 13's app router handles server-actions - I brought it up in a discussions there, but after digging further into the code that appeared to be causing the issue (https://github.com/vercel/next.js/blob/82eb6e6f4955dc90752a4875ccf86b7ac0c8d03a/packages/next/src/client/components/router-reducer/reducers/server-action-reducer.ts#L58), it didn't seem that what the Next code was doing was actually contrary to how fetch is documented (or actually) works, so I figured the fix was better on this side in case there were other apps/libraries taking advantage of that behavior for "" with fetch.

@MSNev MSNev added this to the 3.0.4 milestone Sep 25, 2023
@@ -1186,6 +1186,9 @@ export class AjaxMonitor extends BaseTelemetryPlugin implements IDependenciesPlu

if (input instanceof Request) {
ajaxData.requestUrl = input ? input.url : "";
} else if (input === "" ) {
const location = getLocation();
ajaxData.requestUrl = location?.href?.split("#")[0] || input;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tend to not use the TypeScript ?. operator as this causes a lot of JavaScript to get generated and we are always attempting to keep the size of the generated JS code to a minimum.

Suggest maybe something like the following so that we handle the case where input.url === ""

let requestUrl: string;
if (input instanceof Request) {
    requestUrl = (input||{}).url || "";
} else {
    requestUrl = input;
}

// Handle case where either the input (as a string) or the input.url is an empty string
if (requestUrl === "") {
    const location = getLocation();
    if (location && location.href) {
        requestUrl = strSplit(location.href, "#")[0];
    }
}

ajaxData.requestUrl = requestUrl;

Where strSplit is imported from @nevware21/ts-utils

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will update. I saw a couple other uses of ?. in this file so I thought it ok here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, so not a "does it work" thing, but "how much JS gets generated" - got it.

Copy link
Contributor Author

@jorupp jorupp Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, that's a good point too re: "what if input.url === """ - I'll test how that actually acts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Request("") appears to be setting url to the current URL itself, but it doesn't hurt to handle that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update pushed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, so not a "does it work" thing, but "how much JS gets generated" - got it.

Will need to create a work item to track those down and remove ☹️

@MSNev
Copy link
Collaborator

MSNev commented Sep 25, 2023

Thanks again for creating the PR to address this issue

@MSNev
Copy link
Collaborator

MSNev commented Sep 25, 2023

Going to close and re-open to force the CLA to rerun

@MSNev MSNev closed this Sep 25, 2023
@MSNev MSNev reopened this Sep 25, 2023
@MSNev MSNev merged commit 9fa2b0e into microsoft:main Sep 25, 2023
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants