performance-relayer.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _webVitals = require("next/dist/compiled/web-vitals");
  7. const initialHref = location.href;
  8. let isRegistered = false;
  9. let userReportHandler;
  10. function onReport(metric) {
  11. if (userReportHandler) {
  12. userReportHandler(metric);
  13. }
  14. // This code is not shipped, executed, or present in the client-side
  15. // JavaScript bundle unless explicitly enabled in your application.
  16. //
  17. // When this feature is enabled, we'll make it very clear by printing a
  18. // message during the build (`next build`).
  19. if (process.env.NODE_ENV === 'production' && // This field is empty unless you explicitly configure it:
  20. process.env.__NEXT_ANALYTICS_ID) {
  21. var ref;
  22. const body = {
  23. dsn: process.env.__NEXT_ANALYTICS_ID,
  24. id: metric.id,
  25. page: (ref = window.__NEXT_DATA__) == null ? void 0 : ref.page,
  26. href: initialHref,
  27. event_name: metric.name,
  28. value: metric.value.toString(),
  29. speed: 'connection' in navigator && navigator['connection'] && 'effectiveType' in navigator['connection'] ? navigator['connection']['effectiveType'] : ''
  30. };
  31. const blob = new Blob([
  32. new URLSearchParams(body).toString()
  33. ], {
  34. // This content type is necessary for `sendBeacon`:
  35. type: 'application/x-www-form-urlencoded'
  36. });
  37. const vitalsUrl = 'https://vitals.vercel-insights.com/v1/vitals';
  38. // Navigator has to be bound to ensure it does not error in some browsers
  39. // https://xgwang.me/posts/you-may-not-know-beacon/#it-may-throw-error%2C-be-sure-to-catch
  40. const send = navigator.sendBeacon && navigator.sendBeacon.bind(navigator);
  41. function fallbackSend() {
  42. fetch(vitalsUrl, {
  43. body: blob,
  44. method: 'POST',
  45. credentials: 'omit',
  46. keepalive: true
  47. }).catch(console.error);
  48. }
  49. try {
  50. // If send is undefined it'll throw as well. This reduces output code size.
  51. send(vitalsUrl, blob) || fallbackSend();
  52. } catch (err) {
  53. fallbackSend();
  54. }
  55. }
  56. }
  57. var _default = (onPerfEntry)=>{
  58. // Update function if it changes:
  59. userReportHandler = onPerfEntry;
  60. // Only register listeners once:
  61. if (isRegistered) {
  62. return;
  63. }
  64. isRegistered = true;
  65. (0, _webVitals).onCLS(onReport);
  66. (0, _webVitals).onFID(onReport);
  67. (0, _webVitals).onFCP(onReport);
  68. (0, _webVitals).onLCP(onReport);
  69. (0, _webVitals).onTTFB(onReport);
  70. (0, _webVitals).onINP(onReport);
  71. };
  72. exports.default = _default;
  73. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  74. Object.defineProperty(exports.default, '__esModule', { value: true });
  75. Object.assign(exports.default, exports);
  76. module.exports = exports.default;
  77. }
  78. //# sourceMappingURL=performance-relayer.js.map