nuxt.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import Alert from 'sentry/components/alert';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import crashReportCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/crashReportCallout';
  6. import widgetCallout from 'sentry/components/onboarding/gettingStartedDoc/feedback/widgetCallout';
  7. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  8. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  9. import type {
  10. Docs,
  11. DocsParams,
  12. OnboardingConfig,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  14. import {
  15. getCrashReportJavaScriptInstallStep,
  16. getCrashReportModalConfigDescription,
  17. getCrashReportModalIntroduction,
  18. getFeedbackConfigureDescription,
  19. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  20. import {getJSMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  21. import {MaybeBrowserProfilingBetaWarning} from 'sentry/components/onboarding/gettingStartedDoc/utils/profilingOnboarding';
  22. import {
  23. getReplayConfigOptions,
  24. getReplayConfigureDescription,
  25. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  26. import {t, tct} from 'sentry/locale';
  27. type Params = DocsParams;
  28. const getNuxtModuleSnippet = () => `
  29. export default defineNuxtConfig({
  30. modules: ["@sentry/nuxt/module"],
  31. });
  32. `;
  33. const getSdkClientSetupSnippet = (params: Params) => `
  34. import * as Sentry from "@sentry/nuxt";
  35. Sentry.init({
  36. // If set up, you can use your runtime config here
  37. // dsn: useRuntimeConfig().public.sentry.dsn,
  38. dsn: "${params.dsn.public}",${
  39. params.isReplaySelected
  40. ? `
  41. integrations: [Sentry.replayIntegration(${getReplayConfigOptions(params.replayOptions)})],`
  42. : ''
  43. }${
  44. params.isPerformanceSelected
  45. ? `
  46. // Tracing
  47. // We recommend adjusting this value in production, or using a tracesSampler for finer control.
  48. tracesSampleRate: 1.0, // Capture 100% of the transactions
  49. // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
  50. tracePropagationTargets: ["localhost", /^https:\\/\\/yourserver\\.io\\/api/],`
  51. : ''
  52. }${
  53. params.isReplaySelected
  54. ? `
  55. // Session Replay
  56. 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.
  57. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.`
  58. : ''
  59. }${
  60. params.isProfilingSelected
  61. ? `
  62. // Set profilesSampleRate to 1.0 to profile every transaction.
  63. // Since profilesSampleRate is relative to tracesSampleRate,
  64. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  65. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  66. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  67. profilesSampleRate: 1.0,`
  68. : ''
  69. }
  70. });
  71. `;
  72. const getSdkServerSetupSnippet = (params: Params) => `
  73. import * as Sentry from "@sentry/nuxt";
  74. Sentry.init({
  75. dsn: "${params.dsn.public}",${
  76. params.isPerformanceSelected
  77. ? `
  78. // Tracing
  79. // We recommend adjusting this value in production, or using a tracesSampler for finer control.
  80. tracesSampleRate: 1.0, // Capture 100% of the transactions`
  81. : ''
  82. }${
  83. params.isProfilingSelected
  84. ? `
  85. // Set profilesSampleRate to 1.0 to profile every transaction.
  86. // Since profilesSampleRate is relative to tracesSampleRate,
  87. // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
  88. // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
  89. // results in 25% of transactions being profiled (0.5*0.5=0.25)
  90. profilesSampleRate: 1.0,`
  91. : ''
  92. }
  93. });
  94. `;
  95. const getVerifyNuxtSnippet = () => `
  96. <script setup>
  97. const triggerError = () => {
  98. throw new Error("Nuxt Button Error");
  99. };
  100. </script>
  101. <template>
  102. <button id="errorBtn" @click="triggerError">Trigger Error</button>
  103. </template>`;
  104. const getInstallConfig = () => [
  105. {
  106. language: 'bash',
  107. code: [
  108. {
  109. label: 'npm',
  110. value: 'npm',
  111. language: 'bash',
  112. code: 'npm install --save @sentry/nuxt',
  113. },
  114. {
  115. label: 'yarn',
  116. value: 'yarn',
  117. language: 'bash',
  118. code: 'yarn add @sentry/nuxt',
  119. },
  120. {
  121. label: 'pnpm',
  122. value: 'pnpm',
  123. language: 'bash',
  124. code: `pnpm add @sentry/nuxt`,
  125. },
  126. ],
  127. },
  128. ];
  129. const onboarding: OnboardingConfig = {
  130. introduction: params => (
  131. <Fragment>
  132. <MaybeBrowserProfilingBetaWarning {...params} />
  133. <p>
  134. {tct(
  135. 'In this quick guide you’ll use [strong:npm], [strong:yarn] or [strong:pnpm] to set up:',
  136. {
  137. strong: <strong />,
  138. }
  139. )}
  140. </p>
  141. </Fragment>
  142. ),
  143. install: () => [
  144. {
  145. type: StepType.INSTALL,
  146. description: t(
  147. 'Add the Sentry Nuxt SDK as a dependency using your preferred package manager:'
  148. ),
  149. configurations: getInstallConfig(),
  150. },
  151. ],
  152. configure: (params: Params) => [
  153. {
  154. type: StepType.CONFIGURE,
  155. configurations: [
  156. {
  157. description: tct(
  158. 'Add the Sentry Nuxt module in your [code:nuxt.config.ts] file:',
  159. {code: <code />}
  160. ),
  161. code: [
  162. {
  163. label: 'TypeScript',
  164. value: 'typescript',
  165. language: 'typescript',
  166. filename: 'nuxt.config.ts',
  167. code: getNuxtModuleSnippet(),
  168. },
  169. ],
  170. },
  171. {
  172. description: tct(
  173. 'For the client, create a [codeFile:sentry.client.config.ts] file in your project root and initialize the Sentry SDK:',
  174. {codeFile: <code />}
  175. ),
  176. code: [
  177. {
  178. label: 'TypeScript',
  179. value: 'typescript',
  180. language: 'typescript',
  181. filename: 'sentry.client.config.ts',
  182. code: getSdkClientSetupSnippet(params),
  183. },
  184. ],
  185. },
  186. {
  187. description: (
  188. <Fragment>
  189. <p>
  190. {tct(
  191. 'For the server, create a [codeFile:sentry.server.config.ts] file in your project root and initialize the Sentry SDK:',
  192. {codeFile: <code />}
  193. )}
  194. </p>
  195. <StyledAlert type="info" showIcon>
  196. {tct(
  197. 'To complete the server-side setup, follow the [link:Sentry Nuxt docs] for guidance. Nuxt compiles your code in ESM on the server side as well, so the deployment setup can get tricky depending on where you deploy your application.',
  198. {
  199. link: (
  200. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nuxt/#server-side-setup" />
  201. ),
  202. }
  203. )}
  204. </StyledAlert>
  205. </Fragment>
  206. ),
  207. code: [
  208. {
  209. label: 'TypeScript',
  210. value: 'typescript',
  211. language: 'typescript',
  212. filename: 'sentry.server.config.ts',
  213. code: getSdkServerSetupSnippet(params),
  214. },
  215. ],
  216. },
  217. ],
  218. },
  219. {
  220. title: t('Upload Source Maps'),
  221. description: tct(
  222. 'To upload source maps to Sentry, follow the [link:instructions in our documentation].',
  223. {
  224. link: (
  225. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nuxt/#add-readable-stack-traces-to-errors" />
  226. ),
  227. }
  228. ),
  229. },
  230. ],
  231. verify: () => [
  232. {
  233. type: StepType.VERIFY,
  234. description: t(
  235. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  236. ),
  237. configurations: [
  238. {
  239. code: [
  240. {
  241. label: 'Vue',
  242. value: 'vue',
  243. language: 'html',
  244. code: getVerifyNuxtSnippet(),
  245. },
  246. ],
  247. },
  248. ],
  249. },
  250. ],
  251. nextSteps: () => [
  252. {
  253. id: 'nuxt-features',
  254. name: t('Nuxt Features'),
  255. description: t('Learn about our first class integration with the Nuxt framework.'),
  256. link: 'https://docs.sentry.io/platforms/javascript/guides/nuxt/features/',
  257. },
  258. ],
  259. };
  260. const replayOnboarding: OnboardingConfig = {
  261. install: () => [
  262. {
  263. type: StepType.INSTALL,
  264. description: tct(
  265. 'You need a minimum version 8.9.1 of [code:@sentry/nuxt] in order to use Session Replay. You do not need to install any additional packages.',
  266. {
  267. code: <code />,
  268. }
  269. ),
  270. configurations: getInstallConfig(),
  271. },
  272. ],
  273. configure: (params: Params) => [
  274. {
  275. type: StepType.CONFIGURE,
  276. description: getReplayConfigureDescription({
  277. link: 'https://docs.sentry.io/platforms/javascript/guides/nuxt/session-replay/',
  278. }),
  279. configurations: [
  280. {
  281. code: [
  282. {
  283. label: 'JavaScript',
  284. value: 'javascript',
  285. language: 'javascript',
  286. code: getSdkClientSetupSnippet(params),
  287. },
  288. ],
  289. additionalInfo: <TracePropagationMessage />,
  290. },
  291. ],
  292. },
  293. ],
  294. verify: () => [],
  295. nextSteps: () => [],
  296. };
  297. const feedbackOnboarding: OnboardingConfig = {
  298. install: () => [
  299. {
  300. type: StepType.INSTALL,
  301. description: tct(
  302. 'For the User Feedback integration to work, you must have the Sentry browser SDK package, or an equivalent framework SDK (e.g. [code:@sentry/nuxt]) installed, minimum version 7.85.0.',
  303. {
  304. code: <code />,
  305. }
  306. ),
  307. configurations: getInstallConfig(),
  308. },
  309. ],
  310. configure: (params: Params) => [
  311. {
  312. type: StepType.CONFIGURE,
  313. description: getFeedbackConfigureDescription({
  314. linkConfig:
  315. 'https://docs.sentry.io/platforms/javascript/guides/nuxt/user-feedback/configuration/',
  316. linkButton:
  317. 'https://docs.sentry.io/platforms/javascript/guides/nuxt/user-feedback/configuration/#bring-your-own-button',
  318. }),
  319. configurations: [
  320. {
  321. code: [
  322. {
  323. label: 'JavaScript',
  324. value: 'javascript',
  325. language: 'javascript',
  326. code: getSdkClientSetupSnippet(params),
  327. },
  328. ],
  329. },
  330. ],
  331. additionalInfo: crashReportCallout({
  332. link: 'https://docs.sentry.io/platforms/nuxt/guides/nuxt/user-feedback/#crash-report-modal',
  333. }),
  334. },
  335. ],
  336. verify: () => [],
  337. nextSteps: () => [],
  338. };
  339. const crashReportOnboarding: OnboardingConfig = {
  340. introduction: () => getCrashReportModalIntroduction(),
  341. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  342. configure: () => [
  343. {
  344. type: StepType.CONFIGURE,
  345. description: getCrashReportModalConfigDescription({
  346. link: 'https://docs.sentry.io/platforms/javascript/guides/nuxt/user-feedback/configuration/#crash-report-modal',
  347. }),
  348. additionalInfo: widgetCallout({
  349. link: 'https://docs.sentry.io/platforms/javascript/guides/nuxt/user-feedback/#user-feedback-widget',
  350. }),
  351. },
  352. ],
  353. verify: () => [],
  354. nextSteps: () => [],
  355. };
  356. const profilingOnboarding: OnboardingConfig = {
  357. ...onboarding,
  358. introduction: params => <MaybeBrowserProfilingBetaWarning {...params} />,
  359. };
  360. const docs: Docs = {
  361. onboarding,
  362. feedbackOnboardingNpm: feedbackOnboarding,
  363. replayOnboarding,
  364. customMetricsOnboarding: getJSMetricsOnboarding({getInstallConfig}),
  365. crashReportOnboarding,
  366. profilingOnboarding,
  367. };
  368. const StyledAlert = styled(Alert)`
  369. margin-bottom: 0;
  370. `;
  371. export default docs;