Skip to content
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
4 changes: 2 additions & 2 deletions .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ module.exports = [
path: 'packages/vue/build/esm/index.js',
import: createImport('init', 'browserTracingIntegration'),
gzip: true,
limit: '44.1 KB',
limit: '45 KB',
},
// Svelte SDK (ESM)
{
Expand All @@ -178,7 +178,7 @@ module.exports = [
name: 'CDN Bundle',
path: createCDNPath('bundle.min.js'),
gzip: true,
limit: '28 KB',
limit: '29 KB',
},
{
name: 'CDN Bundle (incl. Tracing)',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '0.1',
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'page' })],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let clickCount = 0;

document.getElementById('navigate').addEventListener('click', () => {
clickCount++;
// Each click navigates to a different page
history.pushState({}, '', `/page-${clickCount}`);
});

document.getElementById('manual-session').addEventListener('click', () => {
Sentry.startSession();
Sentry.captureException('Test error from manual session');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<button id="navigate">Navigate via pushState</button>
<button id="manual-session">Manual session</button>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { expect } from '@playwright/test';
import type { SessionContext } from '@sentry/core';
import { sentryTest } from '../../../utils/fixtures';
import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers';

sentryTest('should start a session on pageload with page lifecycle.', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const sessions = await getMultipleSentryEnvelopeRequests<SessionContext>(page, 1, {
url,
envelopeType: 'session',
timeout: 2000,
});

expect(sessions.length).toBeGreaterThanOrEqual(1);
const session = sessions[0];
expect(session).toBeDefined();
expect(session.init).toBe(true);
expect(session.errors).toBe(0);
expect(session.status).toBe('ok');
});

sentryTest(
'should NOT start a new session on pushState navigation with page lifecycle.',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const sessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, {
url,
envelopeType: 'session',
timeout: 4000,
});

const manualSessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, {
envelopeType: 'session',
timeout: 4000,
});

const eventsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, {
envelopeType: 'event',
timeout: 4000,
});

await page.waitForSelector('#navigate');

await page.locator('#navigate').click();
await page.locator('#navigate').click();
await page.locator('#navigate').click();

const sessions = (await sessionsPromise).filter(session => session.init);

expect(sessions.length).toBe(1);
Comment on lines +48 to +52
Copy link
Member

@Lms24 Lms24 Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: What I like to do when I test that something is not sent, is to await an event after navigating so that we have some time before ending the tests. I guess in this case, we could wait for an error or a transaction event. There's still an assumption here that the session would have been sent before that event but I think that's reasonable and better than not waiting at all or waiting for a a specific time.

We could even make it more deterministic by:

  1. going to the page
  2. awaiting for the init session
  3. registering the request listener for additional sessions
  4. navigating
  5. waiting for the unrelated event
  6. asserting that no more session envelopes have been sent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are very valid points. I'll try to implement these

expect(sessions[0].init).toBe(true);

// teardown and verify if nothing else got sent
await page.locator('#manual-session').click();

const newSessions = (await manualSessionsPromise).filter(session => session.init);
const events = await eventsPromise;

expect(newSessions.length).toBe(2);
expect(newSessions[0].init).toBe(true);
expect(newSessions[1].init).toBe(true);
expect(newSessions[1].sid).not.toBe(newSessions[0].sid);
expect(events).toEqual([
expect.objectContaining({
level: 'error',
message: 'Test error from manual session',
}),
]);
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '0.1',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let clickCount = 0;

document.getElementById('navigate').addEventListener('click', () => {
clickCount++;
history.pushState({}, '', `/page-${clickCount}`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<button id="navigate">Navigate via pushState</button>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect } from '@playwright/test';
import type { SessionContext } from '@sentry/core';
import { sentryTest } from '../../../utils/fixtures';
import { getMultipleSentryEnvelopeRequests } from '../../../utils/helpers';

sentryTest(
'should start new sessions on pushState navigation with route lifecycle (default).',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const sessionsPromise = getMultipleSentryEnvelopeRequests<SessionContext>(page, 10, {
url,
envelopeType: 'session',
timeout: 4000,
});

await page.waitForSelector('#navigate');

await page.locator('#navigate').click();
await page.locator('#navigate').click();
await page.locator('#navigate').click();

const sessions = (await sessionsPromise).filter(session => session.init);

expect(sessions.length).toBe(3);
},
);
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@ export { globalHandlersIntegration } from './integrations/globalhandlers';
export { httpContextIntegration } from './integrations/httpcontext';
export { linkedErrorsIntegration } from './integrations/linkederrors';
export { browserApiErrorsIntegration } from './integrations/browserapierrors';
export { browserSessionIntegration } from './integrations/browsersession';

export { lazyLoadIntegration } from './utils/lazyLoadIntegration';
1 change: 0 additions & 1 deletion packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export type { Span, FeatureFlagsIntegration } from '@sentry/core';
export { makeBrowserOfflineTransport } from './transports/offline';
export { browserProfilingIntegration } from './profiling/integration';
export { spotlightBrowserIntegration } from './integrations/spotlight';
export { browserSessionIntegration } from './integrations/browsersession';
export { launchDarklyIntegration, buildLaunchDarklyFlagUsedHandler } from './integrations/featureFlags/launchdarkly';
export { openFeatureIntegration, OpenFeatureIntegrationHook } from './integrations/featureFlags/openfeature';
export { unleashIntegration } from './integrations/featureFlags/unleash';
Expand Down
37 changes: 28 additions & 9 deletions packages/browser/src/integrations/browsersession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils
import { DEBUG_BUILD } from '../debug-build';
import { WINDOW } from '../helpers';

interface BrowserSessionOptions {
/**
* Controls the session lifecycle - when new sessions are created.
*
* - `'route'`: A session is created on page load and on every navigation.
* This is the default behavior.
* - `'page'`: A session is created once when the page is loaded. Session is not
* updated on navigation. This is useful for webviews or single-page apps where
* URL changes should not trigger new sessions.
*
* @default 'route'
*/
lifecycle?: 'route' | 'page';
}

/**
* When added, automatically creates sessions which allow you to track adoption and crashes (crash free rate) in your Releases in Sentry.
* More information: https://docs.sentry.io/product/releases/health/
*
* Note: In order for session tracking to work, you need to set up Releases: https://docs.sentry.io/product/releases/
*/
export const browserSessionIntegration = defineIntegration(() => {
export const browserSessionIntegration = defineIntegration((options: BrowserSessionOptions = {}) => {
const lifecycle = options.lifecycle ?? 'route';

return {
name: 'BrowserSession',
setupOnce() {
Expand All @@ -26,14 +43,16 @@ export const browserSessionIntegration = defineIntegration(() => {
startSession({ ignoreDuration: true });
captureSession();

// We want to create a session for every navigation as well
addHistoryInstrumentationHandler(({ from, to }) => {
// Don't create an additional session for the initial route or if the location did not change
if (from !== undefined && from !== to) {
startSession({ ignoreDuration: true });
captureSession();
}
});
if (lifecycle === 'route') {
// We want to create a session for every navigation as well
addHistoryInstrumentationHandler(({ from, to }) => {
// Don't create an additional session for the initial route or if the location did not change
if (from !== undefined && from !== to) {
startSession({ ignoreDuration: true });
captureSession();
}
});
}
},
};
});
Loading