diff --git a/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.test.ts b/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.test.ts index 868be40d4647f..e86cb0d6f2c79 100644 --- a/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.test.ts +++ b/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.test.ts @@ -9,6 +9,8 @@ import { BehaviorSubject, firstValueFrom } from 'rxjs'; import { analyticsServiceMock } from '@kbn/core-analytics-browser-mocks'; +import { applicationServiceMock } from '@kbn/core-application-browser-mocks'; +import type { InternalApplicationStart } from '@kbn/core-application-browser-internal'; import type { AnalyticsServiceSetup } from '@kbn/core-analytics-browser'; import type { ExecutionContextSetup } from '@kbn/core-execution-context-browser'; import { ExecutionContextService } from './execution_context_service'; @@ -18,14 +20,19 @@ describe('ExecutionContextService', () => { let curApp$: BehaviorSubject; let execService: ExecutionContextService; let analytics: jest.Mocked; + let history: jest.Mocked; beforeEach(() => { analytics = analyticsServiceMock.createAnalyticsServiceSetup(); + history = applicationServiceMock.createInternalStartContract().history as jest.Mocked< + InternalApplicationStart['history'] + >; execService = new ExecutionContextService(); execContext = execService.setup({ analytics }); curApp$ = new BehaviorSubject('app1'); execContext = execService.start({ curApp$, + history, }); }); @@ -96,6 +103,50 @@ describe('ExecutionContextService', () => { ); }); + it('url updates automatically when there is a navigation', async () => { + execContext.set({ + type: 'ghf', + meta: { + foo: 1, + }, + description: 'first set', + }); + + expect(execContext.get()).toMatchInlineSnapshot( + { + name: 'app1', + description: 'first set', + type: 'ghf', + url: '/', + }, + ` + Object { + "description": "first set", + "meta": Object { + "foo": 1, + }, + "name": "app1", + "type": "ghf", + "url": "/", + } + ` + ); + + history.listen.mock.calls[0][0]({ ...history.location, pathname: '/another-path' }, 'PUSH'); + + expect(execContext.get()).toMatchInlineSnapshot(` + Object { + "description": "first set", + "meta": Object { + "foo": 1, + }, + "name": "app1", + "type": "ghf", + "url": "/another-path", + } + `); + }); + it('sets context and adds current url and appid when getting it', () => { execContext.set({ type: 'ghf', diff --git a/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.ts b/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.ts index 9aadfe1448e70..5d5bf78b1f6bd 100644 --- a/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.ts +++ b/packages/core/execution-context/core-execution-context-browser-internal/src/execution_context_service.ts @@ -18,6 +18,7 @@ import type { ExecutionContextSetup, ExecutionContextStart, } from '@kbn/core-execution-context-browser'; +import type { InternalApplicationStart } from '@kbn/core-application-browser-internal'; // Should be exported from elastic/apm-rum export type LabelValue = string | number | boolean; @@ -32,6 +33,7 @@ export interface SetupDeps { export interface StartDeps { curApp$: Observable; + history: InternalApplicationStart['history']; } /** @internal */ @@ -75,7 +77,7 @@ export class ExecutionContextService return this.contract; } - public start({ curApp$ }: StartDeps) { + public start({ curApp$, history }: StartDeps) { const start = this.contract!; // Track app id changes and clear context on app change @@ -86,6 +88,13 @@ export class ExecutionContextService }) ); + // Track URL changes to make sure that we reflect the new path name + this.subscription.add( + history.listen((location) => { + start.set({ url: location.pathname }); + }) + ); + return start; } diff --git a/packages/core/root/core-root-browser-internal/src/core_system.ts b/packages/core/root/core-root-browser-internal/src/core_system.ts index 44e25b257e32c..6728a26d3c891 100644 --- a/packages/core/root/core-root-browser-internal/src/core_system.ts +++ b/packages/core/root/core-root-browser-internal/src/core_system.ts @@ -339,6 +339,7 @@ export class CoreSystem { const executionContext = this.executionContext.start({ curApp$: application.currentAppId$, + history: application.history, }); const chrome = await this.chrome.start({