-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(vue): Register Vue route provider #23793
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
Merged
logaretm
merged 7 commits into
awad/route-provider-astro
from
awad/route-provider-vue-app
Oct 1, 2026
+304
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f60133e
feat(vue): Register a route provider read off the Vue app
logaretm 0d5127d
ref(vue): Import the route provider API from `@sentry/core/browser`
logaretm 0184481
ref(vue): Import the route provider API from `@sentry/browser`
logaretm 0429302
ref(vue): Pass the route provider as the `routeProvider` option
logaretm ba19db5
test(vue): Cover route resolution through the route provider
logaretm d3a44e0
fix(vue): Resolve hash and base routes, register the provider from `v…
logaretm 3b4b45e
feat(vue): Register the route provider for Vue 2
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/vue-3/src/views/RouteProviderView.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <script setup lang="ts"> | ||
| import * as Sentry from '@sentry/vue'; | ||
| import { onMounted, ref } from 'vue'; | ||
|
|
||
| const route = ref<string>(); | ||
|
|
||
| onMounted(() => { | ||
| route.value = Sentry.resolveCurrentRoute() ?? 'unresolved'; | ||
| }); | ||
| </script> | ||
|
|
||
| <template> | ||
| <div id="resolved-route">{{ route }}</div> | ||
| </template> |
9 changes: 9 additions & 0 deletions
9
dev-packages/e2e-tests/test-applications/vue-3/tests/route-provider.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| // The route provider reads the router off the app passed to `Sentry.init`, so this fails if it can't | ||
| // find it there. | ||
| test('resolves the parameterized route through the route provider', async ({ page }) => { | ||
| await page.goto('/route-provider/123'); | ||
|
|
||
| await expect(page.locator('#resolved-route')).toHaveText('/route-provider/:id'); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type { RouteProvider } from '@sentry/browser'; | ||
| import { createUrlRouteProvider } from '@sentry/browser'; | ||
| import type { Route } from './router'; | ||
| import type { Vue } from './types'; | ||
|
|
||
| // Vue Router 3 resolves to `{ route }`, Vue Router 4+ returns the route itself. | ||
| type ResolvedLocation = Route | { route: Route }; | ||
|
|
||
| type RouteUrl = Parameters<RouteProvider['resolveRoute']>[0]; | ||
|
|
||
| interface InstalledRouter { | ||
| resolve?: (to: string) => ResolvedLocation; | ||
| /** Vue Router 4+ */ | ||
| options?: { history?: { base?: string } }; | ||
| /** Vue Router 3 */ | ||
| mode?: string; | ||
| history?: { base?: string }; | ||
| } | ||
|
|
||
| interface AppWithRouter { | ||
| config?: { globalProperties?: { $router?: InstalledRouter } }; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a route provider from a `vue-router` instance, however the SDK got hold of one. | ||
| * | ||
| * The router is looked up per call rather than captured once, because `app.use(router)` may run | ||
| * either side of `Sentry.init()` and only the app itself is guaranteed to exist by then. | ||
| */ | ||
| export function createVueRouteProvider(getRouter: () => InstalledRouter | undefined): RouteProvider { | ||
| return createUrlRouteProvider(url => { | ||
| const router = getRouter(); | ||
| const resolved = router?.resolve?.(getRouterLocation(router, url)); | ||
| if (!resolved) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const route = 'matched' in resolved ? resolved : resolved.route; | ||
|
|
||
| // Always the matched path, never `route.name`. Callers set `url.template` from this, and a route | ||
| // name is an identifier rather than a template. | ||
| return route.matched[route.matched.length - 1]?.path; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reads the router `vue-router` installed onto a Vue app. | ||
| */ | ||
| export function getRouterFromApp(app: Vue | Vue[] | undefined): InstalledRouter | undefined { | ||
| const firstApp = (Array.isArray(app) ? app[0] : app) as AppWithRouter | undefined; | ||
|
|
||
| return firstApp?.config?.globalProperties?.$router; | ||
| } | ||
|
|
||
| /** | ||
| * Vue 2 only exposes the router on instances, not on the constructor passed to `init`, so this picks it | ||
| * up from the root instance (`new Vue({ router })`) as it is created. | ||
| */ | ||
| export function captureRouterFromVue(Vue: Vue): () => InstalledRouter | undefined { | ||
| let router: InstalledRouter | undefined; | ||
| Vue.mixin({ | ||
| beforeCreate(this: { $options: { router?: InstalledRouter } }) { | ||
| router = router || this.$options.router; | ||
| }, | ||
| }); | ||
|
|
||
| return () => router; | ||
| } | ||
|
|
||
| /** | ||
| * `resolve` matches the router's own location rather than the browser's: the hash in hash mode, and | ||
| * the path without the router's base otherwise. Mirrors vue-router's `createCurrentLocation`. | ||
| */ | ||
| function getRouterLocation(router: InstalledRouter, { pathname, search, hash }: RouteUrl): string { | ||
| const base = router.options?.history?.base ?? router.history?.base ?? ''; | ||
| const hashPos = base.indexOf('#'); | ||
|
|
||
| if (hashPos > -1 || router.mode === 'hash') { | ||
| const hashBase = hashPos > -1 ? base.slice(hashPos) : '#'; | ||
| const path = hash.slice(hash.startsWith(hashBase) ? hashBase.length : 1); | ||
|
|
||
| return path.startsWith('/') ? path : `/${path}`; | ||
| } | ||
|
|
||
| const path = | ||
| base && pathname.toLowerCase().startsWith(base.toLowerCase()) ? pathname.slice(base.length) || '/' : pathname; | ||
|
|
||
| return `${path}${search}${hash}`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createVueRouteProvider, getRouterFromApp } from '../src/routeProvider'; | ||
| import type { Route } from '../src/router'; | ||
| import type { Vue } from '../src/types'; | ||
|
|
||
| function makeRoute(overrides: Partial<Route> = {}): Route { | ||
| return { path: '/users/42', query: {}, params: {}, matched: [{ path: '/users/:id' }], ...overrides }; | ||
| } | ||
|
|
||
| /** Vue Router 4+ returns the route itself. Only `/users/42` matches, so a wrong location resolves to nothing. */ | ||
| const v4Router = (route: Route | undefined, base = '') => ({ | ||
| options: { history: { base } }, | ||
| resolve: (to: string) => (to.split('?')[0] === '/users/42' ? route : makeRoute({ matched: [] })) as Route, | ||
| }); | ||
| /** Vue Router 3 wraps it in `{ route }`. */ | ||
| const v3Router = (route: Route, mode = 'history', base = '') => ({ | ||
| mode, | ||
| history: { base }, | ||
| resolve: (to: string) => ({ route: to.split('?')[0] === '/users/42' ? route : makeRoute({ matched: [] }) }), | ||
| }); | ||
|
|
||
| /** A Vue 3 app with `vue-router` installed, which sets `config.globalProperties.$router`. */ | ||
| const appWithRouter = (router: unknown) => ({ config: { globalProperties: { $router: router } } }) as unknown as Vue; | ||
|
|
||
| describe('getRouterFromApp', () => { | ||
| it('reads the router vue-router installed on the app', () => { | ||
| const router = v4Router(makeRoute()); | ||
|
|
||
| expect(getRouterFromApp(appWithRouter(router))).toBe(router); | ||
| }); | ||
|
|
||
| it('reads from the first app when several were passed', () => { | ||
| const router = v4Router(makeRoute()); | ||
|
|
||
| expect(getRouterFromApp([appWithRouter(router), appWithRouter(undefined)])).toBe(router); | ||
| }); | ||
|
|
||
| it('returns undefined when no router is installed yet', () => { | ||
| expect(getRouterFromApp(appWithRouter(undefined))).toBeUndefined(); | ||
| expect(getRouterFromApp(undefined)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createVueRouteProvider', () => { | ||
| it('resolves the matched path for Vue Router 4+', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute())); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/users/42?tab=1'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('unwraps the `{ route }` shape Vue Router 3 resolves to', () => { | ||
| const provider = createVueRouteProvider(() => v3Router(makeRoute())); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/users/42'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('returns the matched path even for a named route, since a name is not a template', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute({ name: 'UserProfile' }))); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/users/42'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('picks the router up late, since `app.use(router)` may run after `Sentry.init`', () => { | ||
| let router: ReturnType<typeof v4Router> | undefined; | ||
| const provider = createVueRouteProvider(() => router); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/users/42'))).toBeUndefined(); | ||
|
|
||
| router = v4Router(makeRoute()); | ||
| expect(provider.resolveRoute(new URL('https://example.com/users/42'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('returns undefined when nothing matched', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute())); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/nope'))).toBeUndefined(); | ||
| }); | ||
|
|
||
| describe('Vue Router 4+ history base', () => { | ||
| it('strips a non-root base', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute(), '/app')); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/app/users/42'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('resolves from the hash with hash history', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute(), '/#')); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/#/users/42?tab=1'))).toBe('/users/:id'); | ||
| expect(provider.resolveRoute(new URL('https://example.com/'))).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('resolves from the hash with hash history under a base', () => { | ||
| const provider = createVueRouteProvider(() => v4Router(makeRoute(), '/app/#')); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/app/#/users/42'))).toBe('/users/:id'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Vue Router 3 mode', () => { | ||
| it('strips a non-root base in history mode', () => { | ||
| const provider = createVueRouteProvider(() => v3Router(makeRoute(), 'history', '/app')); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/app/users/42'))).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('resolves from the hash in hash mode', () => { | ||
| const provider = createVueRouteProvider(() => v3Router(makeRoute(), 'hash')); | ||
|
|
||
| expect(provider.resolveRoute(new URL('https://example.com/#/users/42'))).toBe('/users/:id'); | ||
| }); | ||
| }); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { getRouteProvider, resolveRoute } from '@sentry/browser'; | ||
| import { getMainCarrier } from '@sentry/core'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
| import { vueIntegration } from '../src/integration'; | ||
| import { init } from '../src/sdk'; | ||
| import type { Vue } from '../src/types'; | ||
|
|
||
| const DSN = 'https://public@dsn.ingest.sentry.io/1337'; | ||
|
|
||
| const router = { resolve: () => ({ matched: [{ path: '/users/:id' }] }) }; | ||
|
|
||
| const app = { | ||
| config: { globalProperties: { $router: router } }, | ||
| mixin: vi.fn(), | ||
| } as unknown as Vue; | ||
|
|
||
| describe('route provider registration', () => { | ||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| getMainCarrier().__SENTRY__ = undefined; | ||
| }); | ||
|
|
||
| it('registers a route provider that reads the router off the app passed to `init`', () => { | ||
| const client = init({ dsn: DSN, app, defaultIntegrations: false, integrations: [vueIntegration()] }); | ||
|
|
||
| expect(resolveRoute('https://example.com/users/42', client)).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('registers a route provider when the app is passed to `vueIntegration`', () => { | ||
| const client = init({ dsn: DSN, defaultIntegrations: false, integrations: [vueIntegration({ app })] }); | ||
|
|
||
| expect(resolveRoute('https://example.com/users/42', client)).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('registers a route provider that picks the router up from the Vue 2 root instance', () => { | ||
| let beforeCreate: (this: unknown) => void = () => {}; | ||
| const Vue2 = { | ||
| config: {}, | ||
| mixin: (mixin: { beforeCreate: typeof beforeCreate }) => { | ||
| beforeCreate = mixin.beforeCreate; | ||
| }, | ||
| } as unknown as Vue; | ||
| const client = init({ dsn: DSN, Vue: Vue2, defaultIntegrations: false, integrations: [vueIntegration()] }); | ||
|
|
||
| expect(resolveRoute('https://example.com/users/42', client)).toBeUndefined(); | ||
|
|
||
| beforeCreate.call({ $options: { router } }); | ||
| expect(resolveRoute('https://example.com/users/42', client)).toBe('/users/:id'); | ||
| }); | ||
|
|
||
| it('does not register a route provider without an app to read the router from', () => { | ||
| const client = init({ dsn: DSN, defaultIntegrations: false, integrations: [vueIntegration()] }); | ||
|
|
||
| expect(getRouteProvider(client)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('keeps a route provider passed by the user', () => { | ||
| const routeProvider = { resolveRoute: () => '/custom', resolveCurrentRoute: () => '/custom' }; | ||
| const client = init({ dsn: DSN, app, routeProvider, defaultIntegrations: false, integrations: [vueIntegration()] }); | ||
|
|
||
| expect(getRouteProvider(client)).toBe(routeProvider); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-null assertion lacks required comment
Low Severity
A new non-null assertion on
Vuehas no comment explaining why a safer type is not possible. The earlierapp/Vueguard makes the assertion valid, but the project rule still requires that comment on new!uses in SDK source. Flagged because this was mentioned in the review rules file.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 89d893e. Configure here.