Skip to content

Commit 273eebf

Browse files
feat(browser)!: Switch default browser session lifecycle mode to page (#21245)
#### Summary - Default browserSessionIntegration session lifecycle is now page instead of route. One session per page load; soft navigations no longer start new sessions unless lifecycle: 'route' is set. #### Checklist - If you've added code that should be tested, please add tests. - Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked. Closes : #21241 --------- Co-authored-by: Martin Sonnberger <martin.sonnberger@sentry.io>
1 parent 4e5acc4 commit 273eebf

9 files changed

Lines changed: 29 additions & 13 deletions

File tree

dev-packages/browser-integration-tests/suites/sessions/initial-scope/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ window.Sentry = Sentry;
55
Sentry.init({
66
dsn: 'https://public@dsn.ingest.sentry.io/1337',
77
release: '0.1',
8+
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'route' })],
89
initialScope: {
910
user: {
1011
id: '1337',

dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ window.Sentry = Sentry;
55
Sentry.init({
66
dsn: 'https://public@dsn.ingest.sentry.io/1337',
77
release: '0.1',
8-
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'page' })],
98
});

dev-packages/browser-integration-tests/suites/sessions/page-lifecycle/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
waitForSession,
99
} from '../../../utils/helpers';
1010

11-
sentryTest('starts a session on pageload with page lifecycle.', async ({ getLocalTestUrl, page }) => {
11+
sentryTest('starts a session on pageload with page lifecycle (default).', async ({ getLocalTestUrl, page }) => {
1212
const url = await getLocalTestUrl({ testDir: __dirname });
1313

1414
const sessionPromise = waitForSession(page, s => !!s.init && s.status === 'ok');
@@ -32,7 +32,7 @@ sentryTest('starts a session on pageload with page lifecycle.', async ({ getLoca
3232
});
3333

3434
sentryTest(
35-
"doesn't start a new session on pushState navigation with page lifecycle.",
35+
"doesn't start a new session on pushState navigation with page lifecycle (default).",
3636
async ({ getLocalTestUrl, page }) => {
3737
const url = await getLocalTestUrl({ testDir: __dirname });
3838

dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/init.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ window.Sentry = Sentry;
55
Sentry.init({
66
dsn: 'https://public@dsn.ingest.sentry.io/1337',
77
release: '0.1',
8+
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'route' })],
89
});

dev-packages/browser-integration-tests/suites/sessions/route-lifecycle/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { sentryTest } from '../../../utils/fixtures';
33
import { waitForSession } from '../../../utils/helpers';
44

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
release: '0.1',
8+
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'route' })],
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
7+
release: '0.1',
8+
integrations: [Sentry.browserSessionIntegration({ lifecycle: 'route' })],
9+
});

packages/browser/src/integrations/browsersession.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ interface BrowserSessionOptions {
77
/**
88
* Controls the session lifecycle - when new sessions are created.
99
*
10-
* - `'route'`: A session is created on page load and on every navigation.
11-
* This is the default behavior.
1210
* - `'page'`: A session is created once when the page is loaded. Session is not
13-
* updated on navigation. This is useful for webviews or single-page apps where
14-
* URL changes should not trigger new sessions.
11+
* updated on navigation. This is the default behavior.
12+
* - `'route'`: A session is created on page load and on every navigation.
1513
*
16-
* @default 'route'
14+
* @default 'page'
1715
*/
1816
lifecycle?: 'route' | 'page';
1917
}
@@ -25,7 +23,7 @@ interface BrowserSessionOptions {
2523
* Note: In order for session tracking to work, you need to set up Releases: https://docs.sentry.io/product/releases/
2624
*/
2725
export const browserSessionIntegration = defineIntegration((options: BrowserSessionOptions = {}) => {
28-
const lifecycle = options.lifecycle ?? 'route';
26+
const lifecycle = options.lifecycle ?? 'page';
2927

3028
return {
3129
name: 'BrowserSession' as const,

packages/browser/test/integrations/browsersession.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ describe('browserSessionIntegration', () => {
147147
});
148148

149149
it('does not re-send the navigation session when navigation happens before the deferred initial capture', () => {
150-
// Default lifecycle is 'route', which also registers the navigation handler.
151-
setupBrowserSession();
150+
setupBrowserSession({ lifecycle: 'route' });
152151

153152
// The initial capture is deferred, so nothing is sent synchronously.
154153
expect(SentryCore.captureSession).not.toHaveBeenCalled();
@@ -165,7 +164,7 @@ describe('browserSessionIntegration', () => {
165164
});
166165

167166
it('still captures a session on navigation that happens after the initial capture', () => {
168-
setupBrowserSession();
167+
setupBrowserSession({ lifecycle: 'route' });
169168

170169
vi.runAllTimers();
171170
expect(SentryCore.captureSession).toHaveBeenCalledTimes(1);

0 commit comments

Comments
 (0)