routeflow-browser 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.ts CHANGED
@@ -2,6 +2,10 @@
2
2
  * HTTP client for sending events to Routeflow backend.
3
3
  */
4
4
  import { IngestPayload } from "./types";
5
+ /**
6
+ * Get the current backend URL.
7
+ */
8
+ export declare function getBackendUrl(): string;
5
9
  /**
6
10
  * Send events to Routeflow backend.
7
11
  */
package/dist/client.js CHANGED
@@ -1,11 +1,15 @@
1
1
  /**
2
2
  * HTTP client for sending events to Routeflow backend.
3
3
  */
4
- // Use localhost for development, production URL for deployed environments
5
- const BACKEND_URL = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1"
6
- ? "http://127.0.0.1:5004"
7
- : "https://routeflow-backend-production.up.railway.app";
4
+ // Hardcoded backend URL - always use production
5
+ const BACKEND_URL = "https://routeflow-backend-production.up.railway.app";
8
6
  const INGEST_ENDPOINT = "/v1/ingest";
7
+ /**
8
+ * Get the current backend URL.
9
+ */
10
+ export function getBackendUrl() {
11
+ return BACKEND_URL;
12
+ }
9
13
  /**
10
14
  * Send events to Routeflow backend.
11
15
  */
@@ -16,7 +16,7 @@ class ExternalSpanTracker {
16
16
  };
17
17
  this.flushTimer = null;
18
18
  this.SDK_NAME = "@routeflow/browser";
19
- this.SDK_VERSION = "0.1.0"; // TODO: sync with package.json
19
+ this.SDK_VERSION = "0.1.2";
20
20
  this.config = config;
21
21
  this.startFlushTimer();
22
22
  }
@@ -5,12 +5,28 @@ import { isSameOrigin } from "./validate";
5
5
  import { getOrCreateSessionId, generateTraceId } from "./trace";
6
6
  import { getCurrentFrontendRoute } from "./route";
7
7
  import { trackExternalFetch } from "./external_spans";
8
+ import { getBackendUrl } from "./client";
8
9
  const TRACE_ID_HEADER = "x-routeflow-trace-id";
9
10
  const SESSION_ID_HEADER = "x-routeflow-session-id";
10
11
  const FRONTEND_ROUTE_HEADER = "x-routeflow-frontend-route";
11
12
  let originalFetch;
12
13
  let isPatched = false;
13
14
  let trackExternal = false;
15
+ /**
16
+ * Check if a URL is targeting the backend (where we should inject headers).
17
+ */
18
+ function isBackendRequest(url) {
19
+ try {
20
+ const backendUrl = getBackendUrl();
21
+ const urlObj = new URL(url, window.location.href);
22
+ const backendObj = new URL(backendUrl);
23
+ // Match by origin (protocol + hostname + port)
24
+ return urlObj.origin === backendObj.origin;
25
+ }
26
+ catch {
27
+ return false;
28
+ }
29
+ }
14
30
  /**
15
31
  * Patch window.fetch to add correlation headers.
16
32
  */
@@ -30,13 +46,15 @@ export function patchFetch(enableExternalTracking) {
30
46
  : input instanceof URL
31
47
  ? input.href
32
48
  : input.url;
49
+ const isBackend = isBackendRequest(url);
33
50
  const sameOrigin = isSameOrigin(url);
34
- // Add correlation headers for same-origin requests
35
- // For external requests, we'll add headers in trackAndFetch if tracking is enabled
36
- if (sameOrigin) {
51
+ // Add correlation headers ONLY for backend requests
52
+ if (isBackend) {
37
53
  init = addCorrelationHeaders(input, init);
38
54
  }
39
- // Track external spans if enabled (will also add correlation headers)
55
+ // Track external spans if enabled and not same-origin
56
+ // Note: We don't add correlation headers to external requests anymore
57
+ // unless they happen to be the backend
40
58
  if (trackExternal && !sameOrigin) {
41
59
  return trackAndFetch(url, init);
42
60
  }
@@ -73,15 +91,14 @@ function addCorrelationHeaders(input, init) {
73
91
  }
74
92
  /**
75
93
  * Track external fetch call and execute it.
76
- * Also adds correlation headers to tracked external requests.
94
+ * Note: Headers are NOT added here - only if isBackendRequest() is true (already handled in patchedFetch).
77
95
  */
78
96
  async function trackAndFetch(url, init) {
79
97
  const startTime = performance.now();
80
98
  let response = null;
81
99
  let error = null;
82
- // Add correlation headers to tracked external requests
83
- // This allows backends in your infrastructure to correlate requests
84
- init = addCorrelationHeaders(url, init);
100
+ // Note: We don't add correlation headers here anymore
101
+ // Headers are only added if isBackendRequest() returned true (handled in patchedFetch)
85
102
  try {
86
103
  response = await originalFetch(url, init);
87
104
  return response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "routeflow-browser",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Browser SDK for Routeflow - Frontend to backend correlation and external dependency tracking",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",