-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat(astro): Register astro route provider #23792
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
Open
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e30d851
feat(astro): Register a route provider backed by the route meta tag
logaretm d411e8a
ref(astro): Import the route provider API from `@sentry/core/browser`
logaretm 423bfb7
ref(astro): Only require the pathname in the route provider
logaretm c9b3e56
ref(astro): Import the route provider API from `@sentry/browser`
logaretm e5d0d38
ref(astro): Pass the route provider as the `routeProvider` option
logaretm bccce36
test(astro): Cover route resolution through the route provider
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
18 changes: 18 additions & 0 deletions
18
dev-packages/e2e-tests/test-applications/astro-5/src/pages/route-provider/[id].astro
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,18 @@ | ||
| --- | ||
| import Layout from '../../layouts/Layout.astro'; | ||
|
|
||
| export const prerender = false; | ||
| --- | ||
|
|
||
| <Layout title="Route provider"> | ||
| <button id="resolve-route">Resolve route</button> | ||
| <div id="resolved-route"></div> | ||
| </Layout> | ||
|
|
||
| <script> | ||
| import * as Sentry from '@sentry/astro'; | ||
|
|
||
| document.getElementById('resolve-route')?.addEventListener('click', () => { | ||
| document.getElementById('resolved-route')!.textContent = Sentry.resolveCurrentRoute() ?? 'unresolved'; | ||
| }); | ||
| </script> |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/astro-5/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,10 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
|
|
||
| // The route provider reads the route the middleware renders into the page, so this fails if that route | ||
| // never reaches the browser. | ||
| test('resolves the parameterized route through the route provider', async ({ page }) => { | ||
| await page.goto('/route-provider/123'); | ||
| await page.locator('#resolve-route').click(); | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import type { RouteProvider } from '@sentry/browser'; | ||
| import { WINDOW } from '@sentry/browser'; | ||
|
|
||
| /** | ||
| * Reads the parameterized route the Astro middleware injects into the document it rendered. | ||
| */ | ||
| function readRouteNameFromMeta(): string | undefined { | ||
| const optionalDocument = WINDOW.document as (typeof WINDOW)['document'] | undefined; | ||
| const content = optionalDocument?.querySelector('meta[name=sentry-route-name]')?.getAttribute('content'); | ||
| if (!content) { | ||
| return undefined; | ||
| } | ||
|
|
||
| try { | ||
| return decodeURIComponent(content); | ||
| } catch { | ||
| // The middleware encodes the route, so a value we can't decode isn't one we put there. | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A route provider backed by the `sentry-route-name` meta tag the Astro middleware injects. | ||
| * | ||
| * Unlike a manifest-backed provider this is not a matcher: the document only ever describes the page | ||
| * it rendered, so a URL other than the current one resolves to `undefined` rather than a guess. | ||
| * | ||
| * The tag does track client-side navigations. Astro's `ClientRouter` swaps it during | ||
| * `astro:after-swap`, at the same moment `location` changes, so reading it per call stays correct | ||
| * across soft navigations and back/forward. It is only stale *during* a navigation, before the swap, | ||
| * which is why `resolveRoute` refuses to answer for anything but the current path. | ||
| */ | ||
| export function createAstroRouteProvider(): RouteProvider { | ||
| const isCurrentPath = (url: { pathname: string }): boolean => url.pathname === WINDOW.location?.pathname; | ||
|
|
||
| return { | ||
| resolveRoute: url => (isCurrentPath(url) ? readRouteNameFromMeta() : undefined), | ||
| resolveCurrentRoute: readRouteNameFromMeta, | ||
| }; | ||
| } | ||
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,65 @@ | ||
| import { GLOBAL_OBJ } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { createAstroRouteProvider } from '../../src/client/routeProvider'; | ||
|
|
||
| let originalDocument: unknown; | ||
| let originalLocation: unknown; | ||
|
|
||
| /** Mirrors what the Astro middleware injects: an encoded route on a `sentry-route-name` meta tag. */ | ||
| function renderPage(pathname: string, routeName: string | undefined): void { | ||
| const meta = routeName | ||
| ? { getAttribute: (attr: string) => (attr === 'content' ? encodeURIComponent(routeName) : null) } | ||
| : null; | ||
|
|
||
| (GLOBAL_OBJ as { document?: unknown }).document = { | ||
| querySelector: (selector: string) => (selector === 'meta[name=sentry-route-name]' ? meta : null), | ||
| }; | ||
| (GLOBAL_OBJ as { location?: unknown }).location = { pathname }; | ||
| } | ||
|
|
||
| describe('createAstroRouteProvider', () => { | ||
| beforeEach(() => { | ||
| originalDocument = (GLOBAL_OBJ as { document?: unknown }).document; | ||
| originalLocation = (GLOBAL_OBJ as { location?: unknown }).location; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| (GLOBAL_OBJ as { document?: unknown }).document = originalDocument; | ||
| (GLOBAL_OBJ as { location?: unknown }).location = originalLocation; | ||
| }); | ||
|
|
||
| it('resolves the current route from the meta tag', () => { | ||
| renderPage('/users/1', '/users/[id]'); | ||
|
|
||
| expect(createAstroRouteProvider().resolveCurrentRoute()).toBe('/users/[id]'); | ||
| }); | ||
|
|
||
| it('resolves a URL that is the current page', () => { | ||
| renderPage('/users/1', '/users/[id]'); | ||
|
|
||
| expect(createAstroRouteProvider().resolveRoute(new URL('https://example.com/users/1'))).toBe('/users/[id]'); | ||
| }); | ||
|
|
||
| it('refuses to answer for a URL that is not the current page', () => { | ||
| renderPage('/users/1', '/users/[id]'); | ||
|
|
||
| // The document only ever describes the page it rendered, so guessing here would be wrong. This is | ||
| // also what keeps a navigation from being named after the route it is leaving. | ||
| expect(createAstroRouteProvider().resolveRoute(new URL('https://example.com/posts/hello'))).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('follows a client-side navigation, since the meta tag is swapped with the document', () => { | ||
| const provider = createAstroRouteProvider(); | ||
| renderPage('/users/1', '/users/[id]'); | ||
| expect(provider.resolveCurrentRoute()).toBe('/users/[id]'); | ||
|
|
||
| renderPage('/posts/hello', '/posts/[slug]'); | ||
| expect(provider.resolveCurrentRoute()).toBe('/posts/[slug]'); | ||
| }); | ||
|
|
||
| it('returns undefined when the middleware injected no route', () => { | ||
| renderPage('/users/1', undefined); | ||
|
|
||
| expect(createAstroRouteProvider().resolveCurrentRoute()).toBeUndefined(); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.