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

Check for localStorage support #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion src/lib/request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable functional/no-let */
/* eslint-disable functional/immutable-data */
import { sendEvent } from './request';
import { checkLocalStorageSupport, sendEvent } from './request';
import { PlausibleOptions } from './tracker';

const getXhrMockClass = () => ({
Expand Down Expand Up @@ -111,6 +111,15 @@ describe('sendEvent', () => {
sendEvent('myEvent', { ...defaultData, trackLocalhost: true });
expect(xmr).toHaveBeenCalled();
});
test('can determine if localStorage is supported in an inlined data:uri iframe', () => {
const fauxWindow = {} as Window;
const hasLocalStorageSupport = checkLocalStorageSupport(fauxWindow);
expect(hasLocalStorageSupport).toBeFalsy();
});
test('can determine if localStorage is supported in a true DOM environment with a window object', () => {
const hasLocalStorageSupport = checkLocalStorageSupport();
expect(hasLocalStorageSupport).toBeTruthy();
});
test('does not send if "plausible_ignore" is set to "true" in localStorage', () => {
window.localStorage.setItem('plausible_ignore', 'true');
expect(xmr).not.toHaveBeenCalled();
Expand Down
27 changes: 27 additions & 0 deletions src/lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ export type EventOptions = {
readonly props?: { readonly [propName: string]: string };
};

export const checkLocalStorageSupport = (
g?: Window | typeof globalThis | (Window & typeof globalThis)
): boolean => {
try {
if (g) {
const supported =
'localStorage' in g &&
g['localStorage'] !== null &&
g['localStorage'].setItem('plausible_test', `1`) === undefined;
g['localStorage'].removeItem('plausible_test');
return supported;
} else {
const supported =
'localStorage' in window &&
window['localStorage'] !== null &&
window['localStorage'].setItem('plausible_test', `1`) === undefined;
window['localStorage'].removeItem('plausible_test');
return supported;
}
} catch (_e) {
return false;
}
};

/**
* @internal
* Sends an event to Plausible's API
Expand All @@ -48,8 +72,11 @@ export function sendEvent(
);
}

const hasLocalStorageSupport = checkLocalStorageSupport();
const shouldIgnoreCurrentBrowser =
hasLocalStorageSupport &&
localStorage.getItem('plausible_ignore') === 'true';

if (shouldIgnoreCurrentBrowser) {
return console.warn(
'[Plausible] Ignoring event because "plausible_ignore" is set to "true" in localStorage'
Expand Down