|
1 |
| -import { consoleSandbox } from '@sentry/core'; |
| 1 | +import { readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import { consoleSandbox, debug } from '@sentry/core'; |
2 | 3 | import { sentryVitePlugin } from '@sentry/vite-plugin';
|
3 |
| -import type { AstroConfig, AstroIntegration } from 'astro'; |
| 4 | +import type { AstroConfig, AstroIntegration, RoutePart } from 'astro'; |
4 | 5 | import * as fs from 'fs';
|
5 | 6 | import * as path from 'path';
|
6 | 7 | import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
|
7 |
| -import type { SentryOptions } from './types'; |
| 8 | +import type { IntegrationResolvedRoute, SentryOptions } from './types'; |
8 | 9 |
|
9 | 10 | const PKG_NAME = '@sentry/astro';
|
10 | 11 |
|
11 | 12 | export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
|
| 13 | + let sentryServerInitPath: string | undefined; |
| 14 | + |
12 | 15 | return {
|
13 | 16 | name: PKG_NAME,
|
14 | 17 | hooks: {
|
@@ -134,6 +137,8 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
|
134 | 137 | injectScript('page-ssr', buildServerSnippet(options || {}));
|
135 | 138 | }
|
136 | 139 |
|
| 140 | + sentryServerInitPath = pathToServerInit; |
| 141 | + |
137 | 142 | // Prevent Sentry from being externalized for SSR.
|
138 | 143 | // Cloudflare like environments have Node.js APIs are available under `node:` prefix.
|
139 | 144 | // Ref: https://developers.cloudflare.com/workers/runtime-apis/nodejs/
|
@@ -165,6 +170,53 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
|
165 | 170 | });
|
166 | 171 | }
|
167 | 172 | },
|
| 173 | + |
| 174 | + // @ts-expect-error - This hook is available in Astro 5 |
| 175 | + 'astro:routes:resolved': ({ routes }: { routes: IntegrationResolvedRoute[] }) => { |
| 176 | + if (!sentryServerInitPath) { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Astro lowercases the parametrized route. Joining segments manually is recommended to get the correct casing of the routes. |
| 182 | + * Recommendation in comment: https://github.com/withastro/astro/issues/13885#issuecomment-2934203029 |
| 183 | + * Function Reference: https://github.com/joanrieu/astro-typed-links/blob/b3dc12c6fe8d672a2bc2ae2ccc57c8071bbd09fa/package/src/integration.ts#L16 |
| 184 | + */ |
| 185 | + const joinSegments = (segments: RoutePart[][]): string => { |
| 186 | + const parthArray = segments.map(segment => |
| 187 | + segment.map(routePart => (routePart.dynamic ? `[${routePart.content}]` : routePart.content)).join(''), |
| 188 | + ); |
| 189 | + |
| 190 | + return `/${parthArray.join('/')}`; |
| 191 | + }; |
| 192 | + |
| 193 | + try { |
| 194 | + const serverInitContent = readFileSync(sentryServerInitPath, 'utf8'); |
| 195 | + |
| 196 | + const updatedServerInitContent = `${serverInitContent}\nglobalThis["__sentryRouteInfo"] = ${JSON.stringify( |
| 197 | + routes.map(route => { |
| 198 | + return { |
| 199 | + ...route, |
| 200 | + patternCaseSensitive: joinSegments(route.segments), // Store parametrized routes with correct casing on `globalThis` to be able to use them on the server during runtime |
| 201 | + patternRegex: route.patternRegex.source, // using `source` to be able to serialize the regex |
| 202 | + }; |
| 203 | + }), |
| 204 | + null, |
| 205 | + 2, |
| 206 | + )};`; |
| 207 | + |
| 208 | + writeFileSync(sentryServerInitPath, updatedServerInitContent, 'utf8'); |
| 209 | + |
| 210 | + debug.log('Successfully added route pattern information to Sentry server file:', sentryServerInitPath); |
| 211 | + } catch (error) { |
| 212 | + debug.warn(`Failed to write to sentry client init file at ${sentryServerInitPath}:`, error); |
| 213 | + } |
| 214 | + }, |
| 215 | + |
| 216 | + // Astro 4 (Astro 5 does not adds `routes` here anymore) |
| 217 | + 'astro:build:done': ({ routes }) => { |
| 218 | + console.log('astro:build:done', routes); |
| 219 | + }, |
168 | 220 | },
|
169 | 221 | };
|
170 | 222 | };
|
|
0 commit comments