nextjs.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 -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 [code:sentry.client.config.js] and [code:sentry.server.config.js] with the default [code:Sentry.init].',
  61. {
  62. code: <code />,
  63. }
  64. )}
  65. </ListItem>
  66. <ListItem>
  67. {tct('Create [code:next.config.js] with the default configuration.', {
  68. code: <code />,
  69. })}
  70. </ListItem>
  71. <ListItem>
  72. {tct(
  73. 'Create [code:sentry.properties] with configuration for sentry-cli (which is used when automatically uploading source maps).',
  74. {
  75. code: <code />,
  76. }
  77. )}
  78. </ListItem>
  79. </List>
  80. <p>
  81. {tct('Alternatively, you can also [manualSetupLink:set up the SDK manually].', {
  82. manualSetupLink: (
  83. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/" />
  84. ),
  85. })}
  86. </p>
  87. </Fragment>
  88. ),
  89. configurations: [
  90. {
  91. description: (
  92. <Fragment>
  93. <strong>{t('Configure the Sentry SDK:')}</strong>
  94. <p>
  95. {tct(
  96. 'Install Sentry’s Next.js SDK using either [code:yarn] or [code:npm]:',
  97. {
  98. code: <code />,
  99. }
  100. )}
  101. </p>
  102. </Fragment>
  103. ),
  104. language: 'bash',
  105. code: `
  106. yarn add @sentry/nextjs
  107. # or
  108. npm install --save @sentry/nextjs
  109. `,
  110. },
  111. {
  112. language: 'javascript',
  113. code: `
  114. Sentry.init({
  115. ${sentryInitContent}
  116. });
  117. `,
  118. },
  119. ],
  120. },
  121. {
  122. type: StepType.VERIFY,
  123. description: t(
  124. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  125. ),
  126. configurations: [
  127. {
  128. language: 'javascript',
  129. code: `
  130. return <button onClick={() => methodDoesNotExist()}>Break the world</button>;
  131. `,
  132. },
  133. ],
  134. },
  135. ];
  136. export const nextSteps = [
  137. {
  138. id: 'source-maps',
  139. name: t('Source Maps'),
  140. description: t('Learn how to enable readable stack traces in your Sentry errors.'),
  141. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/sourcemaps/',
  142. },
  143. {
  144. id: 'performance-monitoring',
  145. name: t('Performance Monitoring'),
  146. description: t(
  147. 'Track down transactions to connect the dots between 10-second page loads and poor-performing API calls or slow database queries.'
  148. ),
  149. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/performance/',
  150. },
  151. {
  152. id: 'session-replay',
  153. name: t('Session Replay'),
  154. description: t(
  155. '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.'
  156. ),
  157. link: 'https://docs.sentry.io/platforms/javascript/guides/nextjs/session-replay/',
  158. },
  159. ];
  160. // Configuration End
  161. export function GettingStartedWithNextJs({
  162. dsn,
  163. activeProductSelection = [],
  164. ...props
  165. }: ModuleProps) {
  166. const integrations: string[] = [];
  167. const otherConfigs: string[] = [];
  168. let nextStepDocs = [...nextSteps];
  169. if (activeProductSelection.includes(ProductSolution.PERFORMANCE_MONITORING)) {
  170. integrations.push(performanceIntegration.trim());
  171. otherConfigs.push(performanceOtherConfig.trim());
  172. nextStepDocs = nextStepDocs.filter(
  173. step => step.id !== ProductSolution.PERFORMANCE_MONITORING
  174. );
  175. }
  176. if (activeProductSelection.includes(ProductSolution.SESSION_REPLAY)) {
  177. integrations.push(replayIntegration.trim());
  178. otherConfigs.push(replayOtherConfig.trim());
  179. nextStepDocs = nextStepDocs.filter(
  180. step => step.id !== ProductSolution.SESSION_REPLAY
  181. );
  182. }
  183. let sentryInitContent: string[] = [`dsn: "${dsn}",`];
  184. if (integrations.length > 0) {
  185. sentryInitContent = sentryInitContent.concat('integrations: [', integrations, '],');
  186. }
  187. if (otherConfigs.length > 0) {
  188. sentryInitContent = sentryInitContent.concat(otherConfigs);
  189. }
  190. return (
  191. <Layout
  192. steps={steps({sentryInitContent: sentryInitContent.join('\n')})}
  193. nextSteps={nextStepDocs}
  194. {...props}
  195. />
  196. );
  197. }
  198. export default GettingStartedWithNextJs;