Skip to content

fix(astro): Do not inject withSentry into Cloudflare Pages#19558

Open
JPeer264 wants to merge 1 commit intodevelopfrom
jp/cloudflare-pages
Open

fix(astro): Do not inject withSentry into Cloudflare Pages#19558
JPeer264 wants to merge 1 commit intodevelopfrom
jp/cloudflare-pages

Conversation

@JPeer264
Copy link
Member

@JPeer264 JPeer264 commented Feb 27, 2026

When running on Cloudflare Pages the withSentry function got bundled and wrapped into the index.js. This actually didn't cause any problems during the runtime, but it just added unnecessary code into the bundle. This removes now the automatic wrapping, which is required for Cloudflare Workers only.

By adding the plugin sentryCloudflareNodeWarningPlugin we also remove tons of warnings like following when building for Cloudflare Pages:

11:20:40 [WARN] [vite] [plugin vite:resolve] Automatically externalized node built-in module "node:diagnostics_channel" imported from "node_modules/.pnpm/@sentry+node@10.40.0/node_modules/@sentry/node/build/esm/integrations/tracing/fastify/index.js". Consider adding it to environments.ssr.external if it is intended.
11:20:40 [WARN] [vite] [plugin vite:resolve] Automatically externalized node built-in module "node:diagnostics_channel" imported from "node_modules/.pnpm/@sentry+node-core@10.40.0_@opentelemetry+api@1.9.0_@opentelemetry+context-async-hooks@2_feb79575758996d9eeb93d3eb9a5534b/node_modules/@sentry/node-core/build/esm/integrations/http/httpServerIntegration.js". Consider adding it to environments.ssr.external if it is intended.

Closes #19559 (added automatically)

Comment on lines +280 to +282
if (configFile.endsWith('.toml')) {
return content.includes('pages_build_output_dir');
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The isCloudflarePages function incorrectly detects Pages projects by checking for pages_build_output_dir in TOML file comments, leading to misidentification.
Severity: MEDIUM

Suggested Fix

To fix this, either implement TOML comment stripping before checking the file content, similar to how JSON/JSONC files are handled, or use a proper TOML parsing library to accurately read configuration keys. An alternative is to use a more reliable detection mechanism, like checking for Workers-specific fields.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/astro/src/integration/index.ts#L280-L282

Potential issue: The `isCloudflarePages` function checks for the presence of the string
`pages_build_output_dir` in `.toml` files using a simple `content.includes()` check.
This method does not account for TOML syntax, causing it to return a false positive if
the string appears within a comment. This misidentifies a Cloudflare Workers project as
a Pages project, which then prevents the `sentryCloudflareVitePlugin` from being added.
As a result, Sentry initialization for non-SSR routes (like actions and API routes) is
skipped, leading to incomplete error and performance monitoring for the affected Workers
project.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.


try {
// Strip comments from JSONC before parsing
const parsed = JSON.parse(content.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, ''));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSONC comment regex corrupts strings containing double slashes

Medium Severity

The regex /\/\*[\s\S]*?\*\/|\/\/.*/g used to strip comments before JSON.parse also matches // inside JSON string values (e.g., URLs like "https://api.example.com"). This corrupts the JSON, causes JSON.parse to throw, and the catch block returns false. A Cloudflare Pages project with any URL-containing value in wrangler.json or wrangler.jsonc would be misdetected as Workers, causing sentryCloudflareVitePlugin and withSentry wrapping to be unnecessarily added while the intended ssr.noExternal config is skipped. The regex is also needlessly applied to plain .json files which never have comments.

Fix in Cursor Fix in Web

const content = fs.readFileSync(configPath, 'utf-8');

if (configFile.endsWith('.toml')) {
return content.includes('pages_build_output_dir');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TOML detection matches comments containing the key name

Medium Severity

The TOML detection uses a naive content.includes('pages_build_output_dir') which matches the string anywhere — including TOML comments (e.g., # pages_build_output_dir = ...). A Workers project with a commented-out pages_build_output_dir line would be misdetected as Cloudflare Pages, causing the critical sentryCloudflareVitePlugin and withSentry wrapping to be skipped. This results in losing per-request isolation, async context, and trace propagation for the Worker.

Fix in Cursor Fix in Web

@github-actions
Copy link
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.62 kB - -
@sentry/browser - with treeshaking flags 24.12 kB - -
@sentry/browser (incl. Tracing) 42.42 kB - -
@sentry/browser (incl. Tracing, Profiling) 47.09 kB - -
@sentry/browser (incl. Tracing, Replay) 81.24 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 70.86 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 85.94 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 98.2 kB - -
@sentry/browser (incl. Feedback) 42.43 kB - -
@sentry/browser (incl. sendFeedback) 30.29 kB - -
@sentry/browser (incl. FeedbackAsync) 35.34 kB - -
@sentry/browser (incl. Metrics) 26.79 kB - -
@sentry/browser (incl. Logs) 26.93 kB - -
@sentry/browser (incl. Metrics & Logs) 27.61 kB - -
@sentry/react 27.37 kB - -
@sentry/react (incl. Tracing) 44.76 kB - -
@sentry/vue 30.07 kB - -
@sentry/vue (incl. Tracing) 44.29 kB - -
@sentry/svelte 25.64 kB - -
CDN Bundle 28.16 kB - -
CDN Bundle (incl. Tracing) 43.25 kB - -
CDN Bundle (incl. Logs, Metrics) 29 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 44.09 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 68.08 kB - -
CDN Bundle (incl. Tracing, Replay) 80.13 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 80.99 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 85.64 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.53 kB - -
CDN Bundle - uncompressed 82.34 kB - -
CDN Bundle (incl. Tracing) - uncompressed 128.06 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 85.18 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 130.89 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 208.84 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 244.94 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 247.76 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 257.85 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 260.66 kB - -
@sentry/nextjs (client) 47.17 kB - -
@sentry/sveltekit (client) 42.88 kB - -
@sentry/node-core 52.18 kB +0.02% +9 B 🔺
@sentry/node 174.48 kB +0.01% +5 B 🔺
@sentry/node - without tracing 97.33 kB +0.01% +8 B 🔺
@sentry/aws-serverless 113.13 kB +0.01% +8 B 🔺

View base workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(astro): Do not inject withSentry into Cloudflare Pages

1 participant