ssr-window.esm.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * SSR Window 4.0.2
  3. * Better handling for window object in SSR environment
  4. * https://github.com/nolimits4web/ssr-window
  5. *
  6. * Copyright 2021, Vladimir Kharlampidi
  7. *
  8. * Licensed under MIT
  9. *
  10. * Released on: December 13, 2021
  11. */
  12. /* eslint-disable no-param-reassign */
  13. function isObject(obj) {
  14. return (obj !== null &&
  15. typeof obj === 'object' &&
  16. 'constructor' in obj &&
  17. obj.constructor === Object);
  18. }
  19. function extend(target = {}, src = {}) {
  20. Object.keys(src).forEach((key) => {
  21. if (typeof target[key] === 'undefined')
  22. target[key] = src[key];
  23. else if (isObject(src[key]) &&
  24. isObject(target[key]) &&
  25. Object.keys(src[key]).length > 0) {
  26. extend(target[key], src[key]);
  27. }
  28. });
  29. }
  30. const ssrDocument = {
  31. body: {},
  32. addEventListener() { },
  33. removeEventListener() { },
  34. activeElement: {
  35. blur() { },
  36. nodeName: '',
  37. },
  38. querySelector() {
  39. return null;
  40. },
  41. querySelectorAll() {
  42. return [];
  43. },
  44. getElementById() {
  45. return null;
  46. },
  47. createEvent() {
  48. return {
  49. initEvent() { },
  50. };
  51. },
  52. createElement() {
  53. return {
  54. children: [],
  55. childNodes: [],
  56. style: {},
  57. setAttribute() { },
  58. getElementsByTagName() {
  59. return [];
  60. },
  61. };
  62. },
  63. createElementNS() {
  64. return {};
  65. },
  66. importNode() {
  67. return null;
  68. },
  69. location: {
  70. hash: '',
  71. host: '',
  72. hostname: '',
  73. href: '',
  74. origin: '',
  75. pathname: '',
  76. protocol: '',
  77. search: '',
  78. },
  79. };
  80. function getDocument() {
  81. const doc = typeof document !== 'undefined' ? document : {};
  82. extend(doc, ssrDocument);
  83. return doc;
  84. }
  85. const ssrWindow = {
  86. document: ssrDocument,
  87. navigator: {
  88. userAgent: '',
  89. },
  90. location: {
  91. hash: '',
  92. host: '',
  93. hostname: '',
  94. href: '',
  95. origin: '',
  96. pathname: '',
  97. protocol: '',
  98. search: '',
  99. },
  100. history: {
  101. replaceState() { },
  102. pushState() { },
  103. go() { },
  104. back() { },
  105. },
  106. CustomEvent: function CustomEvent() {
  107. return this;
  108. },
  109. addEventListener() { },
  110. removeEventListener() { },
  111. getComputedStyle() {
  112. return {
  113. getPropertyValue() {
  114. return '';
  115. },
  116. };
  117. },
  118. Image() { },
  119. Date() { },
  120. screen: {},
  121. setTimeout() { },
  122. clearTimeout() { },
  123. matchMedia() {
  124. return {};
  125. },
  126. requestAnimationFrame(callback) {
  127. if (typeof setTimeout === 'undefined') {
  128. callback();
  129. return null;
  130. }
  131. return setTimeout(callback, 0);
  132. },
  133. cancelAnimationFrame(id) {
  134. if (typeof setTimeout === 'undefined') {
  135. return;
  136. }
  137. clearTimeout(id);
  138. },
  139. };
  140. function getWindow() {
  141. const win = typeof window !== 'undefined' ? window : {};
  142. extend(win, ssrWindow);
  143. return win;
  144. }
  145. export { extend, getDocument, getWindow, ssrDocument, ssrWindow };