Skip to content

feat(astro): Deprecate passing runtime config to astro integration #16839

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
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,36 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
// Will revisit this later.
const env = process.env;

const {
enabled,
clientInitPath,
serverInitPath,
autoInstrumentation,
sourceMapsUploadOptions,
bundleSizeOptimizations,
debug,
...otherOptions
} = options;

const otherOptionsKeys = Object.keys(otherOptions);
if (otherOptionsKeys.length > 0) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] You passed in additional options (${otherOptionsKeys.join(
', ',
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
);
});
}

const sdkEnabled = {
client: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.client ?? true,
server: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.server ?? true,
client: typeof enabled === 'boolean' ? enabled : enabled?.client ?? true,
server: typeof enabled === 'boolean' ? enabled : enabled?.server ?? true,
};

const sourceMapsNeeded = sdkEnabled.client || sdkEnabled.server;
const { unstable_sentryVitePluginOptions, ...uploadOptions } = options.sourceMapsUploadOptions || {};
const { unstable_sentryVitePluginOptions, ...uploadOptions } = sourceMapsUploadOptions || {};
const shouldUploadSourcemaps = (sourceMapsNeeded && uploadOptions?.enabled) ?? true;

// We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env
Expand Down Expand Up @@ -72,18 +95,15 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
},
},
...unstable_sentryVitePluginOptions,
debug: options.debug ?? false,
debug: debug ?? false,
sourcemaps: {
assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)],
filesToDeleteAfterUpload:
uploadOptions?.filesToDeleteAfterUpload ?? updatedFilesToDeleteAfterUpload,
...unstable_sentryVitePluginOptions?.sourcemaps,
},
bundleSizeOptimizations: {
...options.bundleSizeOptimizations,
// TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore
// ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582
excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing,
...bundleSizeOptimizations,
...unstable_sentryVitePluginOptions?.bundleSizeOptimizations,
},
}),
Expand All @@ -93,28 +113,24 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
}

if (sdkEnabled.client) {
const pathToClientInit = options.clientInitPath
? path.resolve(options.clientInitPath)
: findDefaultSdkInitFile('client');
const pathToClientInit = clientInitPath ? path.resolve(clientInitPath) : findDefaultSdkInitFile('client');

if (pathToClientInit) {
options.debug && logger.info(`Using ${pathToClientInit} for client init.`);
debug && logger.info(`Using ${pathToClientInit} for client init.`);
injectScript('page', buildSdkInitFileImportSnippet(pathToClientInit));
} else {
options.debug && logger.info('Using default client init.');
debug && logger.info('Using default client init.');
injectScript('page', buildClientSnippet(options || {}));
}
}

if (sdkEnabled.server) {
const pathToServerInit = options.serverInitPath
? path.resolve(options.serverInitPath)
: findDefaultSdkInitFile('server');
const pathToServerInit = serverInitPath ? path.resolve(serverInitPath) : findDefaultSdkInitFile('server');
if (pathToServerInit) {
options.debug && logger.info(`Using ${pathToServerInit} for server init.`);
debug && logger.info(`Using ${pathToServerInit} for server init.`);
injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit));
} else {
options.debug && logger.info('Using default server init.');
debug && logger.info('Using default server init.');
injectScript('page-ssr', buildServerSnippet(options || {}));
}

Expand All @@ -136,7 +152,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
}

const isSSR = config && (config.output === 'server' || config.output === 'hybrid');
const shouldAddMiddleware = sdkEnabled.server && options.autoInstrumentation?.requestHandler !== false;
const shouldAddMiddleware = sdkEnabled.server && autoInstrumentation?.requestHandler !== false;

// Guarding calling the addMiddleware function because it was only introduced in [email protected]
// Users on older versions of astro will need to add the middleware manually.
Expand Down
8 changes: 0 additions & 8 deletions packages/astro/src/integration/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function buildSdkInitFileImportSnippet(filePath: string): string {
* default options.
*/
export function buildClientSnippet(options: SentryOptions): string {
/* eslint-disable deprecation/deprecation */
return `import * as Sentry from "@sentry/astro";

Sentry.init({
Expand All @@ -22,7 +21,6 @@ Sentry.init({
replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1},
replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0},
});`;
/* eslint-enable deprecation/deprecation */
}

/**
Expand All @@ -37,7 +35,6 @@ Sentry.init({
});`;
}

/* eslint-disable deprecation/deprecation */
const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
options.dsn ? JSON.stringify(options.dsn) : 'import.meta.env.PUBLIC_SENTRY_DSN'
},
Expand All @@ -47,7 +44,6 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${
tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${
options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : ''
}`;
/* eslint-enable deprecation/deprecation */

/**
* We don't include the `BrowserTracing` integration if `bundleSizeOptimizations.excludeTracing` is falsy.
Expand All @@ -64,13 +60,9 @@ const buildClientIntegrations = (options: SentryOptions): string => {
}

if (
// eslint-disable-next-line deprecation/deprecation
options.replaysSessionSampleRate == null ||
// eslint-disable-next-line deprecation/deprecation
options.replaysSessionSampleRate ||
// eslint-disable-next-line deprecation/deprecation
options.replaysOnErrorSampleRate == null ||
// eslint-disable-next-line deprecation/deprecation
options.replaysOnErrorSampleRate
) {
integrations.push('Sentry.replayIntegration()');
Expand Down
48 changes: 10 additions & 38 deletions packages/astro/src/integration/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { BrowserOptions } from '@sentry/browser';
import type { Options } from '@sentry/core';
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';

type SdkInitPaths = {
Expand Down Expand Up @@ -185,40 +183,14 @@ type SdkEnabledOptions = {
};
};

type DeprecatedRuntimeOptions = Pick<
Options,
'environment' | 'release' | 'dsn' | 'debug' | 'sampleRate' | 'tracesSampleRate'
> &
Pick<BrowserOptions, 'replaysSessionSampleRate' | 'replaysOnErrorSampleRate'> & {
/**
* @deprecated Use the `environment` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
*/
environment?: string;
/**
* @deprecated Use the `release` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
*/
release?: string;
/**
* @deprecated Use the `dsn` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
*/
dsn?: string;
/**
* @deprecated Use the `sampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
*/
sampleRate?: number;
/**
* @deprecated Use the `tracesSampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead.
*/
tracesSampleRate?: number;
/**
* @deprecated Use the `replaysSessionSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead.
*/
replaysSessionSampleRate?: number;
/**
* @deprecated Use the `replaysOnErrorSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead.
*/
replaysOnErrorSampleRate?: number;
};
/**
* We accept aribtrary options that are passed through to the Sentry SDK.
* This is not recommended and will stop working in a future version.
* Note: Not all options are actually passed through, only a select subset:
* release, environment, dsn, debug, sampleRate, tracesSampleRate, replaysSessionSampleRate, replaysOnErrorSampleRate
* @deprecated This will be removed in a future major.
**/
type DeprecatedRuntimeOptions = Record<string, unknown>;

/**
* A subset of Sentry SDK options that can be set via the `sentryAstro` integration.
Expand All @@ -230,7 +202,6 @@ type DeprecatedRuntimeOptions = Pick<
* If you specify a dedicated init file, the SDK options passed to `sentryAstro` will be ignored.
*/
export type SentryOptions = SdkInitPaths &
DeprecatedRuntimeOptions &
InstrumentationOptions &
SdkEnabledOptions & {
/**
Expand All @@ -251,4 +222,5 @@ export type SentryOptions = SdkInitPaths &
* If enabled, prints debug logs during the build process.
*/
debug?: boolean;
};
// eslint-disable-next-line deprecation/deprecation
} & DeprecatedRuntimeOptions;
29 changes: 29 additions & 0 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,35 @@ describe('sentryAstro integration', () => {
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init'));
});

it('injects runtime config into client and server init scripts and warns about deprecation', async () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const integration = sentryAstro({
environment: 'test',
release: '1.0.0',
dsn: 'https://test.sentry.io/123',
debug: true,
bundleSizeOptimizations: {},
});

expect(integration.hooks['astro:config:setup']).toBeDefined();
// @ts-expect-error - the hook exists and we only need to pass what we actually use
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger: { info: vi.fn() } });

expect(consoleWarnSpy).toHaveBeenCalledWith(
'[Sentry] You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
);

expect(injectScript).toHaveBeenCalledTimes(2);
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('Sentry.init'));
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('dsn: "https://test.sentry.io/123"'));
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('release: "1.0.0"'));
expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('environment: "test"'));
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init'));
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('dsn: "https://test.sentry.io/123"'));
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('release: "1.0.0"'));
expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('environment: "test"'));
});

it("doesn't inject client init script if `enabled.client` is `false`", async () => {
const integration = sentryAstro({ enabled: { client: false } });

Expand Down
Loading