Skip to content

Commit

Permalink
chore: store image resource events
Browse files Browse the repository at this point in the history
  • Loading branch information
williazz committed Aug 24, 2023
1 parent 8f1bc75 commit 9ea3521
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/plugins/event-plugins/ResourcePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const LOAD = 'load';
*/
export class ResourcePlugin extends InternalPlugin {
private config: PerformancePluginConfig;
private storeTypes = [ResourceType.IMAGE];

constructor(config?: PartialPerformancePluginConfig) {
super(RESOURCE_EVENT_PLUGIN_ID);
Expand Down Expand Up @@ -117,17 +118,27 @@ export class ResourcePlugin extends InternalPlugin {
}

if (this.context?.record) {
const fileType = getResourceFileType(entryData.name);
const eventData: ResourceEvent = {
version: '1.0.0',
initiatorType: entryData.initiatorType,
duration: entryData.duration,
fileType: getResourceFileType(entryData.name),
fileType,
transferSize: entryData.transferSize
};
if (this.context.config.recordResourceUrl) {
eventData.targetUrl = entryData.name;
}
this.context.record(PERFORMANCE_RESOURCE_EVENT_TYPE, eventData);

const storageKey = this.storeTypes.includes(fileType)
? entryData
: undefined;

this.context.record(
PERFORMANCE_RESOURCE_EVENT_TYPE,
eventData,
storageKey
);
}
};

Expand Down
20 changes: 16 additions & 4 deletions src/plugins/event-plugins/__tests__/ResourcePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ import { mockRandom } from 'jest-mock-random';
import {
context as mockContext,
DEFAULT_CONFIG,
getSession,
record,
recordPageView
record
} from '../../../test-utils/test-utils';
import { PERFORMANCE_RESOURCE_EVENT_TYPE } from '../../utils/constant';
import { ResourceEvent } from '../../../events/resource-event';
import { PluginContext } from '../../types';
import { PartialPerformancePluginConfig } from 'plugins/utils/performance-utils';

const buildResourcePlugin = (config?: PartialPerformancePluginConfig) => {
Expand Down Expand Up @@ -262,4 +259,19 @@ describe('ResourcePlugin tests', () => {

expect(record).not.toHaveBeenCalled();
});

test('when entry is an image then it is stored', async () => {
mockPerformanceObjectWithResources();
mockPerformanceObserver();

const plugin = buildResourcePlugin();
plugin.load(mockContext);
window.dispatchEvent(new Event('load'));
plugin.disable();

const imageCall = record.mock.calls.find(
(call) => (call[1] as ResourceEvent).fileType === 'image'
)!;
expect(imageCall[2]).toMatchObject(imageResourceEvent);
});
});

0 comments on commit 9ea3521

Please sign in to comment.