capacitor.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  2. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  3. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import type {
  6. Docs,
  7. DocsParams,
  8. OnboardingConfig,
  9. PlatformOption,
  10. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  11. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  12. import {
  13. getCrashReportJavaScriptInstallStep,
  14. getCrashReportModalConfigDescription,
  15. getCrashReportModalIntroduction,
  16. getFeedbackConfigOptions,
  17. getFeedbackConfigureDescription,
  18. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  19. import {
  20. getReplayConfigOptions,
  21. getReplayConfigureDescription,
  22. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  23. import {t, tct} from 'sentry/locale';
  24. export enum SiblingOption {
  25. ANGULARV10 = 'angularV10',
  26. ANGULARV12 = 'angularV12',
  27. REACT = 'react',
  28. VUE3 = 'vue3',
  29. VUE2 = 'vue2',
  30. }
  31. type PlatformOptionKey = 'siblingOption';
  32. const platformOptions: Record<PlatformOptionKey, PlatformOption> = {
  33. siblingOption: {
  34. label: t('Sibling Package'),
  35. items: [
  36. {
  37. label: t('Angular 12+'),
  38. value: SiblingOption.ANGULARV12,
  39. },
  40. {
  41. label: t('Angular 10 & 11'),
  42. value: SiblingOption.ANGULARV10,
  43. },
  44. {
  45. label: t('React'),
  46. value: SiblingOption.REACT,
  47. },
  48. {
  49. label: t('Vue 3'),
  50. value: SiblingOption.VUE3,
  51. },
  52. {
  53. label: t('Vue 2'),
  54. value: SiblingOption.VUE2,
  55. },
  56. ],
  57. },
  58. };
  59. type PlatformOptions = typeof platformOptions;
  60. type Params = DocsParams<PlatformOptions>;
  61. const getSentryInitLayout = (params: Params, siblingOption: string): string => {
  62. return `${
  63. siblingOption === SiblingOption.VUE2
  64. ? `Vue,`
  65. : siblingOption === SiblingOption.VUE3
  66. ? 'app,'
  67. : ''
  68. }dsn: "${params.dsn}",
  69. integrations: [${
  70. params.isPerformanceSelected
  71. ? `
  72. new ${getSiblingImportName(siblingOption)}.BrowserTracing({
  73. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  74. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],
  75. ${
  76. params.isPerformanceSelected ? getPerformanceIntegration(siblingOption) : ''
  77. }})`
  78. : ''
  79. }${
  80. params.isFeedbackSelected
  81. ? `
  82. Sentry.feedbackIntegration({
  83. // Additional SDK configuration goes in here, for example:
  84. colorScheme: "system",
  85. ${getFeedbackConfigOptions(params.feedbackOptions)}}),`
  86. : ''
  87. }${
  88. params.isReplaySelected
  89. ? `
  90. new ${getSiblingImportName(siblingOption)}.Replay(${getReplayConfigOptions(
  91. params.replayOptions
  92. )}),`
  93. : ''
  94. }
  95. ],${
  96. params.isPerformanceSelected
  97. ? `
  98. // Performance Monitoring
  99. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  100. : ''
  101. }${
  102. params.isReplaySelected
  103. ? `
  104. // Session Replay
  105. replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
  106. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  107. : ''
  108. }`;
  109. };
  110. const isAngular = (siblingOption: string): boolean =>
  111. siblingOption === SiblingOption.ANGULARV10 ||
  112. siblingOption === SiblingOption.ANGULARV12;
  113. const isVue = (siblingOption: string): boolean =>
  114. siblingOption === SiblingOption.VUE2 || siblingOption === SiblingOption.VUE3;
  115. function getPerformanceIntegration(siblingOption: string): string {
  116. return `${
  117. isVue(siblingOption)
  118. ? `routingInstrumentation: SentryVue.vueRouterInstrumentation(router),`
  119. : isAngular(siblingOption)
  120. ? `routingInstrumentation: SentryAngular.routingInstrumentation,`
  121. : ''
  122. }`;
  123. }
  124. const performanceAngularErrorHandler = `,
  125. {
  126. provide: SentryAngular.TraceService,
  127. deps: [Router],
  128. },
  129. {
  130. provide: APP_INITIALIZER,
  131. useFactory: () => () => {},
  132. deps: [SentryAngular.TraceService],
  133. multi: true,
  134. },`;
  135. const onboarding: OnboardingConfig<PlatformOptions> = {
  136. install: (params: Params) => [
  137. {
  138. type: StepType.INSTALL,
  139. description: (
  140. <p>
  141. {tct(
  142. `Install the Sentry Capacitor SDK as a dependency using [codeNpm:npm] or [codeYarn:yarn], alongside the Sentry [siblingName:] SDK:`,
  143. {
  144. codeYarn: <code />,
  145. codeNpm: <code />,
  146. siblingName: getSiblingName(params.platformOptions.siblingOption),
  147. }
  148. )}
  149. </p>
  150. ),
  151. configurations: [
  152. {
  153. language: 'bash',
  154. code: [
  155. {
  156. label: 'npm',
  157. value: 'npm',
  158. language: 'bash',
  159. code: `npm install --save @sentry/capacitor ${getNpmPackage(
  160. params.platformOptions.siblingOption
  161. )}@^7`,
  162. },
  163. {
  164. label: 'yarn',
  165. value: 'yarn',
  166. language: 'bash',
  167. code: `yarn add @sentry/capacitor ${getNpmPackage(
  168. params.platformOptions.siblingOption
  169. )}@^7 --exact`,
  170. },
  171. ],
  172. },
  173. {
  174. additionalInfo: (
  175. <p>
  176. {tct(
  177. `The version of the Sentry [siblingName:] SDK must match with the version referred by Sentry Capacitor. To check which version of the Sentry [siblingName:] SDK is installed, use the following command: [code:npm info @sentry/capacitor peerDependencies]`,
  178. {
  179. code: <code />,
  180. siblingName: getSiblingName(params.platformOptions.siblingOption),
  181. }
  182. )}
  183. </p>
  184. ),
  185. },
  186. ],
  187. },
  188. ],
  189. configure: params => [
  190. {
  191. type: StepType.CONFIGURE,
  192. configurations: getSetupConfiguration({params, showExtraStep: true}),
  193. },
  194. getUploadSourceMapsStep({
  195. guideLink:
  196. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/sourcemaps/',
  197. ...params,
  198. }),
  199. ],
  200. verify: _ => [
  201. {
  202. type: StepType.VERIFY,
  203. description: t(
  204. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  205. ),
  206. configurations: [
  207. {
  208. language: 'javascript',
  209. code: `myUndefinedFunction();`,
  210. },
  211. ],
  212. },
  213. ],
  214. nextSteps: params => [
  215. {
  216. id: 'capacitor-android-setup',
  217. name: t('Capacitor 2 Setup'),
  218. description: t(
  219. 'If you are using Capacitor 2 or older, follow this step to add required changes in order to initialize the Capacitor SDK on Android.'
  220. ),
  221. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/?#capacitor-2---android-specifics',
  222. },
  223. params.isPerformanceSelected
  224. ? null
  225. : {
  226. id: 'performance-monitoring',
  227. name: t('Performance Monitoring'),
  228. description: t(
  229. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  230. ),
  231. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/performance/',
  232. },
  233. params.isReplaySelected
  234. ? null
  235. : {
  236. id: 'session-replay',
  237. name: t('Session Replay'),
  238. description: t(
  239. 'Get to the root cause of an error or latency issue faster by seeing all the technical details related to that issue in one visual replay on your web application.'
  240. ),
  241. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  242. },
  243. ],
  244. };
  245. function getSiblingImportsSetupConfiguration(siblingOption: string): string {
  246. switch (siblingOption) {
  247. case SiblingOption.VUE3:
  248. return `import {createApp} from "vue";
  249. import {createRouter} from "vue-router";`;
  250. case SiblingOption.VUE2:
  251. return `import Vue from "vue";
  252. import Router from "vue-router";`;
  253. default:
  254. return '';
  255. }
  256. }
  257. function getVueConstSetup(siblingOption: string): string {
  258. switch (siblingOption) {
  259. case SiblingOption.VUE3:
  260. return `
  261. const app = createApp({
  262. // ...
  263. });
  264. const router = createRouter({
  265. // ...
  266. });
  267. `;
  268. case SiblingOption.VUE2:
  269. return `
  270. Vue.use(Router);
  271. const router = new Router({
  272. // ...
  273. });
  274. `;
  275. default:
  276. return '';
  277. }
  278. }
  279. function getSetupConfiguration({
  280. params,
  281. showExtraStep,
  282. showDescription,
  283. }: {
  284. params: Params;
  285. showExtraStep: boolean;
  286. showDescription?: boolean;
  287. }) {
  288. const siblingOption = params.platformOptions.siblingOption;
  289. const sentryInitLayout = getSentryInitLayout(params, siblingOption);
  290. const configuration = [
  291. {
  292. description: showDescription
  293. ? tct(
  294. `You should init the Sentry capacitor SDK in your [code:main.ts] file as soon as possible during application load up, before initializing Sentry [siblingName:]:`,
  295. {
  296. siblingName: getSiblingName(siblingOption),
  297. code: <code />,
  298. }
  299. )
  300. : null,
  301. language: 'javascript',
  302. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  303. import * as Sentry from '@sentry/capacitor';
  304. import * as ${getSiblingImportName(siblingOption)} from '${getNpmPackage(
  305. siblingOption
  306. )}';
  307. ${getVueConstSetup(siblingOption)}
  308. Sentry.init({
  309. ${sentryInitLayout}
  310. },
  311. // Forward the init method from ${getNpmPackage(params.platformOptions.siblingOption)}
  312. ${getSiblingImportName(siblingOption)}.init
  313. );`,
  314. },
  315. ];
  316. if (isAngular(siblingOption) && showExtraStep) {
  317. configuration.push({
  318. description: tct(
  319. "The Sentry Angular SDK exports a function to instantiate ErrorHandler provider that will automatically send JavaScript errors captured by the Angular's error handler.",
  320. {}
  321. ),
  322. language: 'javascript',
  323. code: `
  324. import { APP_INITIALIZER, ErrorHandler, NgModule } from "@angular/core";
  325. import { Router } from "@angular/router";
  326. import * as SentryAngular from "${getNpmPackage(siblingOption)}";
  327. @NgModule({
  328. // ...
  329. providers: [
  330. {
  331. provide: ErrorHandler,
  332. useValue: SentryAngular.createErrorHandler(),
  333. }${params.isPerformanceSelected ? performanceAngularErrorHandler : ' '}
  334. ],
  335. // ...
  336. })
  337. export class AppModule {}`,
  338. });
  339. }
  340. return configuration;
  341. }
  342. function getNpmPackage(siblingOption: string): string {
  343. const packages: Record<SiblingOption, string> = {
  344. [SiblingOption.ANGULARV10]: '@sentry/angular',
  345. [SiblingOption.ANGULARV12]: '@sentry/angular-ivy',
  346. [SiblingOption.REACT]: '@sentry/react',
  347. [SiblingOption.VUE3]: '@sentry/vue',
  348. [SiblingOption.VUE2]: '@sentry/vue',
  349. };
  350. return packages[siblingOption];
  351. }
  352. function getSiblingName(siblingOption: string): string {
  353. siblingOption;
  354. switch (siblingOption) {
  355. case SiblingOption.ANGULARV10:
  356. case SiblingOption.ANGULARV12:
  357. return 'Angular';
  358. case SiblingOption.REACT:
  359. return 'React';
  360. case SiblingOption.VUE2:
  361. case SiblingOption.VUE3:
  362. return 'Vue';
  363. default:
  364. return '';
  365. }
  366. }
  367. function getSiblingImportName(siblingOption: string): string {
  368. siblingOption;
  369. switch (siblingOption) {
  370. case SiblingOption.ANGULARV10:
  371. case SiblingOption.ANGULARV12:
  372. return 'SentryAngular';
  373. case SiblingOption.REACT:
  374. return 'SentryReact';
  375. case SiblingOption.VUE2:
  376. case SiblingOption.VUE3:
  377. return 'SentryVue';
  378. default:
  379. return '';
  380. }
  381. }
  382. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  383. install: params => onboarding.install(params),
  384. configure: params => [
  385. {
  386. type: StepType.CONFIGURE,
  387. description: getReplayConfigureDescription({
  388. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/session-replay/',
  389. }),
  390. configurations: getSetupConfiguration({
  391. params,
  392. showExtraStep: false,
  393. showDescription: false,
  394. }),
  395. additionalInfo: <TracePropagationMessage />,
  396. },
  397. ],
  398. verify: () => [],
  399. nextSteps: () => [],
  400. };
  401. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  402. install: (params: Params) => onboarding.install(params),
  403. configure: (params: Params) => [
  404. {
  405. type: StepType.CONFIGURE,
  406. description: getFeedbackConfigureDescription({
  407. linkConfig:
  408. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/',
  409. linkButton:
  410. 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/#bring-your-own-button',
  411. }),
  412. configurations: getSetupConfiguration({
  413. params,
  414. showExtraStep: false,
  415. showDescription: false,
  416. }),
  417. additionalInfo: crashReportCallout({
  418. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/#crash-report-modal',
  419. }),
  420. },
  421. ],
  422. verify: () => [],
  423. nextSteps: () => [],
  424. };
  425. const crashReportOnboarding: OnboardingConfig<PlatformOptions> = {
  426. introduction: () => getCrashReportModalIntroduction(),
  427. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  428. configure: () => [
  429. {
  430. type: StepType.CONFIGURE,
  431. description: getCrashReportModalConfigDescription({
  432. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/configuration/#crash-report-modal',
  433. }),
  434. additionalInfo: widgetCallout({
  435. link: 'https://docs.sentry.io/platforms/javascript/guides/capacitor/user-feedback/#user-feedback-widget',
  436. }),
  437. },
  438. ],
  439. verify: () => [],
  440. nextSteps: () => [],
  441. };
  442. const docs: Docs<PlatformOptions> = {
  443. onboarding,
  444. platformOptions,
  445. feedbackOnboardingNpm: feedbackOnboarding,
  446. replayOnboardingNpm: replayOnboarding,
  447. crashReportOnboarding,
  448. };
  449. export default docs;