vue.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import {Fragment} from 'react';
  2. import {buildSdkConfig} from 'sentry/components/onboarding/gettingStartedDoc/buildSdkConfig';
  3. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  4. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  5. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  6. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  7. import type {
  8. Docs,
  9. DocsParams,
  10. OnboardingConfig,
  11. PlatformOption,
  12. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  13. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  14. import {
  15. getCrashReportJavaScriptInstallStep,
  16. getCrashReportModalConfigDescription,
  17. getCrashReportModalIntroduction,
  18. getFeedbackConfigureDescription,
  19. getFeedbackSDKSetupSnippet,
  20. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  21. import {
  22. getProfilingDocumentHeaderConfigurationStep,
  23. MaybeBrowserProfilingBetaWarning,
  24. } from 'sentry/components/onboarding/gettingStartedDoc/utils/profilingOnboarding';
  25. import {
  26. getReplayConfigOptions,
  27. getReplayConfigureDescription,
  28. getReplayVerifyStep,
  29. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  30. import {featureFlagOnboarding} from 'sentry/gettingStartedDocs/javascript/javascript';
  31. import {t, tct} from 'sentry/locale';
  32. export enum VueVersion {
  33. VUE3 = 'vue3',
  34. VUE2 = 'vue2',
  35. }
  36. type PlatformOptionKey = 'siblingOption';
  37. const platformOptions: Record<PlatformOptionKey, PlatformOption> = {
  38. siblingOption: {
  39. label: t('Vue Version'),
  40. items: [
  41. {
  42. label: t('Vue 3'),
  43. value: VueVersion.VUE3,
  44. },
  45. {
  46. label: t('Vue 2'),
  47. value: VueVersion.VUE2,
  48. },
  49. ],
  50. },
  51. };
  52. type PlatformOptions = typeof platformOptions;
  53. type Params = DocsParams<PlatformOptions>;
  54. const getIntegrations = (params: Params): string[] => {
  55. const integrations = [];
  56. if (params.isPerformanceSelected) {
  57. integrations.push(`Sentry.browserTracingIntegration({ router })`);
  58. }
  59. if (params.isReplaySelected) {
  60. integrations.push(
  61. `Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)})`
  62. );
  63. }
  64. if (params.isProfilingSelected) {
  65. integrations.push(`Sentry.browserProfilingIntegration()`);
  66. }
  67. return integrations;
  68. };
  69. const getDynamicParts = (params: Params): string[] => {
  70. const dynamicParts: string[] = [];
  71. if (params.isPerformanceSelected) {
  72. dynamicParts.push(`
  73. // Tracing
  74. tracesSampleRate: 1.0, // Capture 100% of the transactions
  75. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  76. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/]`);
  77. }
  78. if (params.isReplaySelected) {
  79. dynamicParts.push(`
  80. // Session Replay
  81. 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.
  82. replaysOnErrorSampleRate: 1.0 // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`);
  83. }
  84. if (params.isProfilingSelected) {
  85. dynamicParts.push(`
  86. // Profiling
  87. profilesSampleRate: 1.0 // Profile 100% of the transactions. This value is relative to tracesSampleRate`);
  88. }
  89. return dynamicParts;
  90. };
  91. const getSentryInitLayout = (params: Params, siblingOption: string): string => {
  92. const config = buildSdkConfig({
  93. params,
  94. staticParts: [
  95. `${siblingOption === VueVersion.VUE2 ? 'Vue' : 'app'}`,
  96. `dsn: "${params.dsn.public}"`,
  97. ],
  98. getIntegrations,
  99. getDynamicParts,
  100. });
  101. return `Sentry.init({
  102. ${config}
  103. });`;
  104. };
  105. const getInstallConfig = () => [
  106. {
  107. language: 'bash',
  108. code: [
  109. {
  110. label: 'npm',
  111. value: 'npm',
  112. language: 'bash',
  113. code: `npm install --save @sentry/vue`,
  114. },
  115. {
  116. label: 'yarn',
  117. value: 'yarn',
  118. language: 'bash',
  119. code: `yarn add @sentry/vue`,
  120. },
  121. ],
  122. },
  123. ];
  124. const onboarding: OnboardingConfig<PlatformOptions> = {
  125. introduction: params => (
  126. <Fragment>
  127. <MaybeBrowserProfilingBetaWarning {...params} />
  128. <p>
  129. {tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  130. strong: <strong />,
  131. })}
  132. </p>
  133. </Fragment>
  134. ),
  135. install: () => [
  136. {
  137. type: StepType.INSTALL,
  138. description: (
  139. <p>
  140. {tct(
  141. `Install the Sentry Vue SDK as a dependency using [code:npm] or [code:yarn], alongside the Sentry Vue SDK:`,
  142. {
  143. code: <code />,
  144. }
  145. )}
  146. </p>
  147. ),
  148. configurations: getInstallConfig(),
  149. },
  150. ],
  151. configure: params => [
  152. {
  153. type: StepType.CONFIGURE,
  154. description: tct(
  155. "Initialize Sentry as early as possible in your application's lifecycle, usually your Vue app's entry point ([code:main.ts/js]).",
  156. {code: <code />}
  157. ),
  158. configurations: getSetupConfiguration(params),
  159. },
  160. getUploadSourceMapsStep({
  161. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/vue/sourcemaps/',
  162. ...params,
  163. }),
  164. ],
  165. verify: () => [
  166. {
  167. type: StepType.VERIFY,
  168. description: t(
  169. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  170. ),
  171. configurations: [
  172. {
  173. language: 'javascript',
  174. code: `myUndefinedFunction();`,
  175. },
  176. ],
  177. },
  178. ],
  179. nextSteps: () => [
  180. {
  181. id: 'vue-features',
  182. name: t('Vue Features'),
  183. description: t('Learn about our first class integration with the Vue framework.'),
  184. link: 'https://docs.sentry.io/platforms/javascript/guides/vue/features/',
  185. },
  186. ],
  187. };
  188. function getSiblingImportsSetupConfiguration(siblingOption: string): string {
  189. switch (siblingOption) {
  190. case VueVersion.VUE3:
  191. return `import {createApp} from "vue";
  192. import {createRouter} from "vue-router";
  193. import router from "./router";
  194. `;
  195. case VueVersion.VUE2:
  196. default:
  197. return `import Vue from "vue";
  198. import Router from "vue-router";`;
  199. }
  200. }
  201. function getSiblingSuffix(siblingOption: string): string {
  202. switch (siblingOption) {
  203. case VueVersion.VUE3:
  204. return `app.use(router);
  205. app.mount("#app");`;
  206. case VueVersion.VUE2:
  207. default:
  208. return `new Vue({
  209. router,
  210. render: (h) => h(App),
  211. }).$mount("#app");`;
  212. }
  213. }
  214. function getVueConstSetup(siblingOption: string): string {
  215. switch (siblingOption) {
  216. case VueVersion.VUE3:
  217. return `
  218. const app = createApp({
  219. // ...
  220. });
  221. `;
  222. case VueVersion.VUE2:
  223. return `
  224. Vue.use(Router);
  225. const router = new Router({
  226. // ...
  227. });
  228. `;
  229. default:
  230. return '';
  231. }
  232. }
  233. function getSetupConfiguration(params: Params) {
  234. const siblingOption = params.platformOptions.siblingOption;
  235. const sentryInitLayout = getSentryInitLayout(params, siblingOption);
  236. const configuration = [
  237. {
  238. language: 'javascript',
  239. code: `${getSiblingImportsSetupConfiguration(siblingOption)}
  240. import * as Sentry from "@sentry/vue";
  241. ${getVueConstSetup(siblingOption)}
  242. ${sentryInitLayout}
  243. ${getSiblingSuffix(siblingOption)}`,
  244. },
  245. ...(params.isProfilingSelected
  246. ? [getProfilingDocumentHeaderConfigurationStep()]
  247. : []),
  248. ];
  249. return configuration;
  250. }
  251. const replayOnboarding: OnboardingConfig<PlatformOptions> = {
  252. install: () => [
  253. {
  254. type: StepType.INSTALL,
  255. description: tct(
  256. 'You need a minimum version 7.27.0 of [code:@sentry/vue] in order to use Session Replay. You do not need to install any additional packages.',
  257. {
  258. code: <code />,
  259. }
  260. ),
  261. configurations: getInstallConfig(),
  262. },
  263. ],
  264. configure: params => [
  265. {
  266. type: StepType.CONFIGURE,
  267. description: getReplayConfigureDescription({
  268. link: 'https://docs.sentry.io/platforms/javascript/guides/vue/session-replay/',
  269. }),
  270. configurations: getSetupConfiguration(params),
  271. additionalInfo: <TracePropagationMessage />,
  272. },
  273. ],
  274. verify: getReplayVerifyStep(),
  275. nextSteps: () => [],
  276. };
  277. const feedbackOnboarding: OnboardingConfig<PlatformOptions> = {
  278. install: () => [
  279. {
  280. type: StepType.INSTALL,
  281. description: tct(
  282. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/vue]) installed, minimum version 7.85.0.',
  283. {
  284. code: <code />,
  285. }
  286. ),
  287. configurations: getInstallConfig(),
  288. },
  289. ],
  290. configure: (params: Params) => [
  291. {
  292. type: StepType.CONFIGURE,
  293. description: getFeedbackConfigureDescription({
  294. linkConfig:
  295. 'https://docs.sentry.io/platforms/javascript/guides/vue/user-feedback/configuration/',
  296. linkButton:
  297. 'https://docs.sentry.io/platforms/javascript/guides/vue/user-feedback/configuration/#bring-your-own-button',
  298. }),
  299. configurations: [
  300. {
  301. code: [
  302. {
  303. label: 'JavaScript',
  304. value: 'javascript',
  305. language: 'javascript',
  306. code: getFeedbackSDKSetupSnippet({
  307. importStatement: `import * as Sentry from "@sentry/vue";`,
  308. dsn: params.dsn.public,
  309. feedbackOptions: params.feedbackOptions,
  310. }),
  311. },
  312. ],
  313. },
  314. ],
  315. additionalInfo: crashReportCallout({
  316. link: 'https://docs.sentry.io/platforms/javascript/guides/vue/user-feedback/#crash-report-modal',
  317. }),
  318. },
  319. ],
  320. verify: () => [],
  321. nextSteps: () => [],
  322. };
  323. const crashReportOnboarding: OnboardingConfig<PlatformOptions> = {
  324. introduction: () => getCrashReportModalIntroduction(),
  325. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  326. configure: () => [
  327. {
  328. type: StepType.CONFIGURE,
  329. description: getCrashReportModalConfigDescription({
  330. link: 'https://docs.sentry.io/platforms/javascript/guides/vue/user-feedback/configuration/#crash-report-modal',
  331. }),
  332. additionalInfo: widgetCallout({
  333. link: 'https://docs.sentry.io/platforms/javascript/guides/vue/user-feedback/#user-feedback-widget',
  334. }),
  335. },
  336. ],
  337. verify: () => [],
  338. nextSteps: () => [],
  339. };
  340. const profilingOnboarding: OnboardingConfig<PlatformOptions> = {
  341. ...onboarding,
  342. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  343. };
  344. const docs: Docs<PlatformOptions> = {
  345. onboarding,
  346. platformOptions,
  347. feedbackOnboardingNpm: feedbackOnboarding,
  348. replayOnboarding,
  349. crashReportOnboarding,
  350. profilingOnboarding,
  351. featureFlagOnboarding,
  352. };
  353. export default docs;