serverComponentWrapperTemplate.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { _nullishCoalesce, _optionalChain } from '@sentry/utils';
  2. import * as Sentry from '@sentry/nextjs';
  3. import { requestAsyncStorage } from '__SENTRY_NEXTJS_REQUEST_ASYNC_STORAGE_SHIM__';
  4. import * as serverComponentModule from '__SENTRY_WRAPPING_TARGET_FILE__';
  5. export * from '__SENTRY_WRAPPING_TARGET_FILE__';
  6. const serverComponent = serverComponentModule.default;
  7. let wrappedServerComponent;
  8. if (typeof serverComponent === 'function') {
  9. // For some odd Next.js magic reason, `headers()` will not work if used inside `wrapServerComponentsWithSentry`.
  10. // Current assumption is that Next.js applies some loader magic to userfiles, but not files in node_modules. This file
  11. // is technically a userfile so it gets the loader magic applied.
  12. wrappedServerComponent = new Proxy(serverComponent, {
  13. apply: (originalFunction, thisArg, args) => {
  14. let sentryTraceHeader = undefined;
  15. let baggageHeader = undefined;
  16. let headers = undefined;
  17. // We try-catch here just in `requestAsyncStorage` is undefined since it may not be defined
  18. try {
  19. const requestAsyncStore = requestAsyncStorage.getStore();
  20. sentryTraceHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _ => _.headers, 'access', _2 => _2.get, 'call', _3 => _3('sentry-trace')]), () => ( undefined));
  21. baggageHeader = _nullishCoalesce(_optionalChain([requestAsyncStore, 'optionalAccess', _4 => _4.headers, 'access', _5 => _5.get, 'call', _6 => _6('baggage')]), () => ( undefined));
  22. headers = _optionalChain([requestAsyncStore, 'optionalAccess', _7 => _7.headers]);
  23. } catch (e) {
  24. /** empty */
  25. }
  26. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
  27. return Sentry.wrapServerComponentWithSentry(originalFunction , {
  28. componentRoute: '__ROUTE__',
  29. componentType: '__COMPONENT_TYPE__',
  30. sentryTraceHeader,
  31. baggageHeader,
  32. headers,
  33. }).apply(thisArg, args);
  34. },
  35. });
  36. } else {
  37. wrappedServerComponent = serverComponent;
  38. }
  39. const generateMetadata = serverComponentModule.generateMetadata
  40. ? Sentry.wrapGenerationFunctionWithSentry(serverComponentModule.generateMetadata, {
  41. componentRoute: '__ROUTE__',
  42. componentType: '__COMPONENT_TYPE__',
  43. generationFunctionIdentifier: 'generateMetadata',
  44. requestAsyncStorage,
  45. })
  46. : undefined;
  47. const generateImageMetadata = serverComponentModule.generateImageMetadata
  48. ? Sentry.wrapGenerationFunctionWithSentry(serverComponentModule.generateImageMetadata, {
  49. componentRoute: '__ROUTE__',
  50. componentType: '__COMPONENT_TYPE__',
  51. generationFunctionIdentifier: 'generateImageMetadata',
  52. requestAsyncStorage,
  53. })
  54. : undefined;
  55. const generateViewport = serverComponentModule.generateViewport
  56. ? Sentry.wrapGenerationFunctionWithSentry(serverComponentModule.generateViewport, {
  57. componentRoute: '__ROUTE__',
  58. componentType: '__COMPONENT_TYPE__',
  59. generationFunctionIdentifier: 'generateViewport',
  60. requestAsyncStorage,
  61. })
  62. : undefined;
  63. const wrappedServerComponent$1 = wrappedServerComponent;
  64. export { wrappedServerComponent$1 as default, generateImageMetadata, generateMetadata, generateViewport };