nextjs.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import {Fragment} from 'react';
  2. import ExternalLink from 'sentry/components/links/externalLink';
  3. import List from 'sentry/components/list/';
  4. import ListItem from 'sentry/components/list/listItem';
  5. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  6. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  7. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  8. import {ProductSolution} from 'sentry/components/onboarding/productSelection';
  9. import {t, tct} from 'sentry/locale';
  10. // Configuration Start
  11. const replayIntegration = `
  12. new Sentry.Replay(),
  13. `;
  14. const replayOtherConfig = `
  15. // Session Replay
  16. 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.
  17. replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  18. `;
  19. const performanceIntegration = `
  20. new Sentry.BrowserTracing(),
  21. `;
  22. const performanceOtherConfig = `
  23. // Performance Monitoring
  24. tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!
  25. `;
  26. export const steps = ({
  27. sentryInitContent,
  28. }: {
  29. sentryInitContent?: string;
  30. } = {}): LayoutProps['steps'] => [
  31. {
  32. type: StepType.INSTALL,
  33. description: (
  34. <p>
  35. {tct(
  36. 'Add Sentry automatically to your app with the [wizardLink:Sentry wizard].',
  37. {
  38. wizardLink: (
  39. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nextjs/#install" />
  40. ),
  41. }
  42. )}
  43. </p>
  44. ),
  45. configurations: [
  46. {
  47. language: 'bash',
  48. code: `npx @sentry/wizard@latest -i nextjs`,
  49. },
  50. ],
  51. },
  52. {
  53. type: StepType.CONFIGURE,
  54. description: (
  55. <Fragment>
  56. {t('The Sentry wizard will automatically patch your application:')}
  57. <List symbol="bullet">
  58. <ListItem>
  59. {tct(
  60. 'Create [sentryClientCode:sentry.client.config.js] and [sentryServerCode:sentry.server.config.js] with the default [sentryInitCode:Sentry.init].',
  61. {
  62. sentryClientCode: <code />,
  63. sentryServerCode: <code />,
  64. sentryInitCode: <code />,
  65. }
  66. )}
  67. </ListItem>
  68. <ListItem>
  69. {tct('Create [code:next.config.js] with the default configuration.', {
  70. code: <code />,
  71. })}
  72. </ListItem>
  73. <ListItem>
  74. {tct(
  75. 'Create [code:sentry.properties] with configuration for sentry-cli (which is used when automatically uploading source maps).',
  76. {
  77. code: <code />,
  78. }
  79. )}
  80. </ListItem>
  81. </List>
  82. <p>
  83. {tct('Alternatively, you can also [manualSetupLink:set up the SDK manually].', {
  84. manualSetupLink: (
  85. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/" />
  86. ),
  87. })}
  88. </p>
  89. </Fragment>
  90. ),
  91. configurations: [
  92. {
  93. description: (
  94. <Fragment>
  95. <strong>{t('Configure the Sentry SDK:')}</strong>
  96. <p>
  97. {tct(
  98. 'Install Sentry’s Next.js SDK using either [yarnCode:yarn] or [npmCode:npm]:',
  99. {
  100. yarnCode: <code />,
  101. npmCode: <code />,
  102. }
  103. )}
  104. </p>
  105. </Fragment>
  106. ),
  107. language: 'bash',
  108. code: `
  109. yarn add @sentry/nextjs
  110. # or
  111. npm install --save @sentry/nextjs
  112. `,
  113. },
  114. {
  115. language: 'javascript',
  116. code: `
  117. Sentry.init({
  118. ${sentryInitContent}
  119. });
  120. `,
  121. },
  122. ],
  123. },
  124. {
  125. type: StepType.VERIFY,
  126. description: t(
  127. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  128. ),
  129. configurations: [
  130. {
  131. language: 'javascript',
  132. code: `
  133. return <button onClick={() => methodDoesNotExist()}>Break the world</button>;
  134. `,
  135. },
  136. ],
  137. },
  138. ];
  139. export const nextSteps = [
  140. {
  141. id: 'source-maps',
  142. name: t('Source Maps'),
  143. description: t('Learn how to enable readable stack traces in your Sentry errors.'),
  144. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/sourcemaps/',
  145. },
  146. {
  147. id: 'performance-monitoring',
  148. name: t('Performance Monitoring'),
  149. description: t(
  150. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  151. ),
  152. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/performance/',
  153. },
  154. {
  155. id: 'session-replay',
  156. name: t('Session Replay'),
  157. description: t(
  158. '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.'
  159. ),
  160. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/session-replay/',
  161. },
  162. ];
  163. // Configuration End
  164. export function GettingStartedWithNextJs({
  165. dsn,
  166. activeProductSelection = [],
  167. ...props
  168. }: ModuleProps) {
  169. const integrations: string[] = [];
  170. const otherConfigs: string[] = [];
  171. let nextStepDocs = [...nextSteps];
  172. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  173. integrations.push(performanceIntegration.trim());
  174. otherConfigs.push(performanceOtherConfig.trim());
  175. nextStepDocs = nextStepDocs.filter(
  176. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  177. );
  178. }
  179. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  180. integrations.push(replayIntegration.trim());
  181. otherConfigs.push(replayOtherConfig.trim());
  182. nextStepDocs = nextStepDocs.filter(
  183. step => step.id !== ProductSolution.SESSION_REPLAY
  184. );
  185. }
  186. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  187. if (integrations.length > 0) {
  188. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  189. }
  190. if (otherConfigs.length > 0) {
  191. sentryInitContent = sentryInitContent.concat(otherConfigs);
  192. }
  193. return (
  194. <Layout
  195. steps={steps({sentryInitContent: sentryInitContent.join('\n')})}
  196. nextSteps={nextStepDocs}
  197. {...props}
  198. />
  199. );
  200. }
  201. export default GettingStartedWithNextJs;