Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/pageViewStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export function migrateLegacyPageViewStorage(loggingService: LoggingService | nu
const needsMigration = !alreadyMigrated && Array.isArray(legacyViews);

if (needsMigration) {
loggingService?.log({
message: 'Rokt Kit: Migrating legacy page-view storage',
code: 'PAGE_VIEW_LEGACY_MIGRATION',
});
const migrated = writeNamespacedField(LS_NAMESPACE_KEY, LS_PAGE_VIEWS_FIELD, legacyViews);
if (!migrated) {
loggingService?.log({
message:
'Rokt Kit: Failed to migrate legacy page-view storage; retaining legacy key for retry [reason: migration_retry]',
message: 'Rokt Kit: Failed to migrate legacy page-view storage [reason: migration_retry]',
code: 'PAGE_VIEW_CAPTURE_FAILED',
});
return;
}
}

Expand Down
30 changes: 28 additions & 2 deletions test/src/pageViewStorage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ describe('pageViewStorage', () => {
expect(window.localStorage.getItem(LEGACY_PAGE_VIEWS_KEY)).toBeNull();
});

it('retains the legacy key and logs when the migrating write fails', () => {
it('logs PAGE_VIEW_LEGACY_MIGRATION when the migration path is taken', () => {
window.localStorage.setItem(LEGACY_PAGE_VIEWS_KEY, JSON.stringify([pageView('home')]));
const logger = { log: vi.fn() } as unknown as LoggingService;

migrateLegacyPageViewStorage(logger);

expect(logger.log).toHaveBeenCalledWith(expect.objectContaining({ code: 'PAGE_VIEW_LEGACY_MIGRATION' }));
});

it('removes the legacy key and logs when the migrating write fails', () => {
window.localStorage.setItem(LEGACY_PAGE_VIEWS_KEY, JSON.stringify([pageView('home')]));
const logger = { log: vi.fn() } as unknown as LoggingService;
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
Expand All @@ -58,9 +67,26 @@ describe('pageViewStorage', () => {

migrateLegacyPageViewStorage(logger);

expect(window.localStorage.getItem(LEGACY_PAGE_VIEWS_KEY)).not.toBeNull();
// Legacy key is removed even on failure — prevents the infinite retry loop.
expect(window.localStorage.getItem(LEGACY_PAGE_VIEWS_KEY)).toBeNull();
expect(logger.log).toHaveBeenCalledWith(expect.objectContaining({ code: 'PAGE_VIEW_CAPTURE_FAILED' }));
});

it('does not log PAGE_VIEW_CAPTURE_FAILED on a second call after a failed migration', () => {
window.localStorage.setItem(LEGACY_PAGE_VIEWS_KEY, JSON.stringify([pageView('home')]));
const logger = { log: vi.fn() } as unknown as LoggingService;
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new DOMException('quota', 'QuotaExceededError');
});

migrateLegacyPageViewStorage(logger); // first call: fails, legacy key removed
vi.restoreAllMocks();
logger.log.mockClear();

migrateLegacyPageViewStorage(logger); // second call: legacy key gone, no-op

expect(logger.log).not.toHaveBeenCalled();
});
});

describe('loadPageViews', () => {
Expand Down
Loading