cloudflare-pages.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  9. import {
  10. getCrashReportJavaScriptInstallStep,
  11. getCrashReportModalConfigDescription,
  12. getCrashReportModalIntroduction,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import {t, tct} from 'sentry/locale';
  15. import {getInstallConfig} from 'sentry/utils/gettingStartedDocs/node';
  16. type Params = DocsParams;
  17. const getSdkConfigureSnippetToml = () => `
  18. compatibility_flags = ["nodejs_compat"]
  19. # compatibility_flags = ["nodejs_als"]
  20. compatibility_date = "2024-09-23"
  21. `;
  22. const getSdkConfigureSnippetJson = () => `
  23. {
  24. "compatibility_flags": [
  25. "nodejs_compat"
  26. ],
  27. "compatibility_date": "2024-09-23"
  28. }`;
  29. const getSdkSetupSnippet = (params: Params) => `
  30. import * as Sentry from "@sentry/cloudflare";
  31. export const onRequest = [
  32. // Make sure Sentry is the first middleware
  33. Sentry.sentryPagesPlugin((context) => ({
  34. dsn: "${params.dsn.public}",
  35. // Set tracesSampleRate to 1.0 to capture 100% of spans for tracing.
  36. // Learn more at
  37. // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  38. tracesSampleRate: 1.0,
  39. })),
  40. // Add more middlewares here
  41. ];`;
  42. const getVerifySnippet = () => `
  43. export function onRequest(context) {
  44. throw new Error();
  45. }`;
  46. const onboarding: OnboardingConfig = {
  47. introduction: () =>
  48. tct(
  49. "In this quick guide, you'll set up and configure the Sentry Cloudflare SDK for use in your Cloudflare Pages application. This will enable Sentry for the backend part of your application: the functions. If you'd like to monitor the frontend as well, refer to the instrumentation guide for [platformLink:the framework of your choice].",
  50. {
  51. platformLink: <ExternalLink href="https://docs.sentry.io/platforms/" />,
  52. }
  53. ),
  54. install: params => [
  55. {
  56. type: StepType.INSTALL,
  57. description: t('Add the Sentry Cloudflare SDK as a dependency:'),
  58. configurations: getInstallConfig(params, {
  59. basePackage: '@sentry/cloudflare',
  60. }),
  61. },
  62. ],
  63. configure: params => [
  64. {
  65. type: StepType.CONFIGURE,
  66. description: t(
  67. "Configuration should happen as early as possible in your application's lifecycle."
  68. ),
  69. configurations: [
  70. {
  71. description: tct(
  72. "To use the SDK, you'll need to set either the [code:nodejs_compat] or [code:nodejs_als] compatibility flags in your [code:wrangler.toml]. This is because the SDK needs access to the [code:AsyncLocalStorage] API to work correctly.",
  73. {
  74. code: <code />,
  75. }
  76. ),
  77. code: [
  78. {
  79. label: 'JSON',
  80. value: 'json',
  81. language: 'json',
  82. filename: 'wrangler.json',
  83. code: getSdkConfigureSnippetJson(),
  84. },
  85. {
  86. label: 'Toml',
  87. value: 'toml',
  88. language: 'toml',
  89. filename: 'wrangler.toml',
  90. code: getSdkConfigureSnippetToml(),
  91. },
  92. ],
  93. },
  94. {
  95. description: tct(
  96. 'Add the [code:sentryPagesPlugin] as [guideLink:middleware to your Cloudflare Pages application]. We recommend adding a [code:functions/_middleware.js] for the middleware setup so that Sentry is initialized for your entire app.',
  97. {
  98. code: <code />,
  99. guideLink: (
  100. <ExternalLink href="https://developers.cloudflare.com/pages/functions/middleware/" />
  101. ),
  102. }
  103. ),
  104. code: [
  105. {
  106. label: 'JavaScript',
  107. value: 'javascript',
  108. language: 'javascript',
  109. filename: 'functions/_middleware.js',
  110. code: getSdkSetupSnippet(params),
  111. },
  112. ],
  113. },
  114. ],
  115. },
  116. getUploadSourceMapsStep({
  117. guideLink:
  118. 'https://docs.sentry.io/platforms/javascript/guides/cloudflare/sourcemaps/',
  119. ...params,
  120. }),
  121. ],
  122. verify: () => [
  123. {
  124. type: StepType.VERIFY,
  125. description: tct(
  126. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected. To trigger it, you need to access the [code:/customerror] path on your deployment.",
  127. {
  128. code: <code />,
  129. }
  130. ),
  131. configurations: [
  132. {
  133. label: 'JavaScript',
  134. value: 'javascript',
  135. language: 'javascript',
  136. filename: 'functions/customerror.js',
  137. code: getVerifySnippet(),
  138. },
  139. ],
  140. },
  141. ],
  142. };
  143. const crashReportOnboarding: OnboardingConfig = {
  144. introduction: () => getCrashReportModalIntroduction(),
  145. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  146. configure: () => [
  147. {
  148. type: StepType.CONFIGURE,
  149. description: getCrashReportModalConfigDescription({
  150. link: 'https://docs.sentry.io/platforms/javascript/guides/cloudflare/user-feedback/configuration/#crash-report-modal',
  151. }),
  152. },
  153. ],
  154. verify: () => [],
  155. nextSteps: () => [],
  156. };
  157. const docs: Docs = {
  158. onboarding,
  159. crashReportOnboarding,
  160. };
  161. export default docs;