Skip to content

Commit aaa3339

Browse files
committed
Don't repeat addBreadcrumb
1 parent 655f433 commit aaa3339

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const HISTORY = 'history';
1616
const DOM = 'dom';
1717
const XHR = 'xhr';
1818

19+
type AddBreadcrumb = Hub['addBreadcrumb'];
20+
type AddBreadcrumbParams = Parameters<AddBreadcrumb>;
21+
1922
/** JSDoc */
2023
interface BreadcrumbsOptions {
2124
console: boolean;
@@ -71,9 +74,17 @@ export class Breadcrumbs implements Integration {
7174
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
7275
const hub = getCurrentHub();
7376

77+
// Alias the addBreadcrumb function to save on bundle size
78+
function addBreadcrumb(
79+
breadcrumb: AddBreadcrumbParams[0],
80+
breadcrumbHint: AddBreadcrumbParams[1],
81+
): ReturnType<AddBreadcrumb> {
82+
hub.addBreadcrumb(breadcrumb, breadcrumbHint);
83+
}
84+
7485
addGlobalEventProcessor(event => {
7586
if (this._options.sentry) {
76-
hub.addBreadcrumb(
87+
addBreadcrumb(
7788
{
7889
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
7990
event_id: event.event_id,
@@ -89,27 +100,27 @@ export class Breadcrumbs implements Integration {
89100
});
90101

91102
if (this._options.console) {
92-
_addConsoleBreadcrumbs(hub);
103+
_addConsoleBreadcrumbs(addBreadcrumb);
93104
}
94105
if (this._options.dom) {
95-
_addDomBreadcrumbs(hub, this._options.dom);
106+
_addDomBreadcrumbs(addBreadcrumb, this._options.dom);
96107
}
97108
if (this._options.xhr) {
98-
_addXhrBreadcrumbs(hub);
109+
_addXhrBreadcrumbs(addBreadcrumb);
99110
}
100111
if (this._options.fetch) {
101-
_addFetchBreadcrumbs(hub);
112+
_addFetchBreadcrumbs(addBreadcrumb);
102113
}
103114
if (this._options.history) {
104-
_addHistoryBreadcrumbs(hub);
115+
_addHistoryBreadcrumbs(addBreadcrumb);
105116
}
106117
}
107118
}
108119

109120
/**
110121
* Creates breadcrumbs from console API calls
111122
*/
112-
function _addConsoleBreadcrumbs(hub: Hub): void {
123+
function _addConsoleBreadcrumbs(addBreadcrumb: AddBreadcrumb): void {
113124
// eslint-disable-next-line @typescript-eslint/no-explicit-any
114125
addInstrumentationHandler((handlerData: { [key: string]: any }): void => {
115126
const breadcrumb = {
@@ -132,7 +143,7 @@ function _addConsoleBreadcrumbs(hub: Hub): void {
132143
}
133144
}
134145

135-
hub.addBreadcrumb(breadcrumb, {
146+
addBreadcrumb(breadcrumb, {
136147
input: handlerData.args,
137148
level: handlerData.level,
138149
});
@@ -142,47 +153,46 @@ function _addConsoleBreadcrumbs(hub: Hub): void {
142153
/**
143154
* Creates breadcrumbs from DOM API calls
144155
*/
145-
function _addDomBreadcrumbs(hub: Hub, dom: BreadcrumbsOptions['dom']): void {
156+
function _addDomBreadcrumbs(addBreadcrumb: AddBreadcrumb, dom: BreadcrumbsOptions['dom']): void {
146157
// eslint-disable-next-line @typescript-eslint/no-explicit-any
147158
addInstrumentationHandler((handlerData: { [key: string]: any }): void => {
148-
let target;
159+
const { event, name, global } = handlerData;
149160
let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;
150161

151162
if (typeof keyAttrs === 'string') {
152163
keyAttrs = [keyAttrs];
153164
}
154165

155166
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
167+
let target;
156168
try {
157-
target = handlerData.event.target
158-
? htmlTreeAsString(handlerData.event.target as Node, keyAttrs)
159-
: htmlTreeAsString((handlerData.event as unknown) as Node, keyAttrs);
169+
target = event.target
170+
? htmlTreeAsString(event.target as Node, keyAttrs)
171+
: htmlTreeAsString((event as unknown) as Node, keyAttrs);
160172
} catch (e) {
161173
target = '<unknown>';
162174
}
163175

164-
if (target.length === 0) {
165-
return;
176+
if (target) {
177+
addBreadcrumb(
178+
{
179+
category: `ui.${name}`,
180+
message: target,
181+
},
182+
{
183+
event,
184+
name,
185+
global,
186+
},
187+
);
166188
}
167-
168-
hub.addBreadcrumb(
169-
{
170-
category: `ui.${handlerData.name}`,
171-
message: target,
172-
},
173-
{
174-
event: handlerData.event,
175-
name: handlerData.name,
176-
global: handlerData.global,
177-
},
178-
);
179189
}, DOM);
180190
}
181191

182192
/**
183193
* Creates breadcrumbs from XHR API calls
184194
*/
185-
function _addXhrBreadcrumbs(hub: Hub): void {
195+
function _addXhrBreadcrumbs(addBreadcrumb: AddBreadcrumb): void {
186196
// eslint-disable-next-line @typescript-eslint/no-explicit-any
187197
addInstrumentationHandler((handlerData: { [key: string]: any }): void => {
188198
if (handlerData.endTimestamp) {
@@ -193,7 +203,7 @@ function _addXhrBreadcrumbs(hub: Hub): void {
193203

194204
const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {};
195205

196-
hub.addBreadcrumb(
206+
addBreadcrumb(
197207
{
198208
category: XHR,
199209
data: {
@@ -215,7 +225,7 @@ function _addXhrBreadcrumbs(hub: Hub): void {
215225
/**
216226
* Creates breadcrumbs from fetch API calls
217227
*/
218-
function _addFetchBreadcrumbs(hub: Hub): void {
228+
function _addFetchBreadcrumbs(addBreadcrumb: AddBreadcrumb): void {
219229
// eslint-disable-next-line @typescript-eslint/no-explicit-any
220230
addInstrumentationHandler((handlerData: { [key: string]: any }): void => {
221231
// We only capture complete fetch requests
@@ -239,19 +249,19 @@ function _addFetchBreadcrumbs(hub: Hub): void {
239249

240250
if (handlerData.error) {
241251
breadcrumbHint.data = handlerData.error;
242-
hub.addBreadcrumb(breadcrumb, breadcrumbHint);
252+
addBreadcrumb(breadcrumb, breadcrumbHint);
243253
} else {
244254
breadcrumb.data.status_code = handlerData.response.status;
245255
breadcrumbHint.response = handlerData.response;
246-
hub.addBreadcrumb(breadcrumb, breadcrumbHint);
256+
addBreadcrumb(breadcrumb, breadcrumbHint);
247257
}
248258
}, FETCH);
249259
}
250260

251261
/**
252262
* Creates breadcrumbs from history API calls
253263
*/
254-
function _addHistoryBreadcrumbs(hub: Hub): void {
264+
function _addHistoryBreadcrumbs(addBreadcrumb: AddBreadcrumb): void {
255265
// eslint-disable-next-line @typescript-eslint/no-explicit-any
256266
addInstrumentationHandler((handlerData: { [key: string]: any }): void => {
257267
let { from, to } = handlerData;
@@ -274,7 +284,7 @@ function _addHistoryBreadcrumbs(hub: Hub): void {
274284
from = parsedFrom.relative;
275285
}
276286

277-
hub.addBreadcrumb({
287+
addBreadcrumb({
278288
category: 'navigation',
279289
data: {
280290
from,

0 commit comments

Comments
 (0)