capacitor.tsx 12 KB

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