getNavigationEntry.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const types = require('../../types.js');
  3. /*
  4. * Copyright 2022 Google LLC
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. const getNavigationEntryFromPerformanceTiming = () => {
  19. // eslint-disable-next-line deprecation/deprecation
  20. const timing = types.WINDOW.performance.timing;
  21. // eslint-disable-next-line deprecation/deprecation
  22. const type = types.WINDOW.performance.navigation.type;
  23. const navigationEntry = {
  24. entryType: 'navigation',
  25. startTime: 0,
  26. type: type == 2 ? 'back_forward' : type === 1 ? 'reload' : 'navigate',
  27. };
  28. for (const key in timing) {
  29. if (key !== 'navigationStart' && key !== 'toJSON') {
  30. // eslint-disable-next-line deprecation/deprecation
  31. navigationEntry[key] = Math.max((timing[key ] ) - timing.navigationStart, 0);
  32. }
  33. }
  34. return navigationEntry ;
  35. };
  36. const getNavigationEntry = () => {
  37. if (types.WINDOW.__WEB_VITALS_POLYFILL__) {
  38. return (
  39. types.WINDOW.performance &&
  40. ((performance.getEntriesByType && performance.getEntriesByType('navigation')[0]) ||
  41. getNavigationEntryFromPerformanceTiming())
  42. );
  43. } else {
  44. return types.WINDOW.performance && performance.getEntriesByType && performance.getEntriesByType('navigation')[0];
  45. }
  46. };
  47. exports.getNavigationEntry = getNavigationEntry;
  48. //# sourceMappingURL=getNavigationEntry.js.map