utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.execOnce = execOnce;
  6. exports.getLocationOrigin = getLocationOrigin;
  7. exports.getURL = getURL;
  8. exports.getDisplayName = getDisplayName;
  9. exports.isResSent = isResSent;
  10. exports.normalizeRepeatedSlashes = normalizeRepeatedSlashes;
  11. exports.loadGetInitialProps = loadGetInitialProps;
  12. exports.ST = exports.SP = exports.warnOnce = exports.isAbsoluteUrl = void 0;
  13. var _async_to_generator = require("@swc/helpers/lib/_async_to_generator.js").default;
  14. function execOnce(fn) {
  15. let used = false;
  16. let result;
  17. return (...args)=>{
  18. if (!used) {
  19. used = true;
  20. result = fn(...args);
  21. }
  22. return result;
  23. };
  24. }
  25. // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
  26. // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
  27. const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
  28. const isAbsoluteUrl = (url)=>ABSOLUTE_URL_REGEX.test(url);
  29. exports.isAbsoluteUrl = isAbsoluteUrl;
  30. function getLocationOrigin() {
  31. const { protocol , hostname , port } = window.location;
  32. return `${protocol}//${hostname}${port ? ':' + port : ''}`;
  33. }
  34. function getURL() {
  35. const { href } = window.location;
  36. const origin = getLocationOrigin();
  37. return href.substring(origin.length);
  38. }
  39. function getDisplayName(Component) {
  40. return typeof Component === 'string' ? Component : Component.displayName || Component.name || 'Unknown';
  41. }
  42. function isResSent(res) {
  43. return res.finished || res.headersSent;
  44. }
  45. function normalizeRepeatedSlashes(url) {
  46. const urlParts = url.split('?');
  47. const urlNoQuery = urlParts[0];
  48. return urlNoQuery// first we replace any non-encoded backslashes with forward
  49. // then normalize repeated forward slashes
  50. .replace(/\\/g, '/').replace(/\/\/+/g, '/') + (urlParts[1] ? `?${urlParts.slice(1).join('?')}` : '');
  51. }
  52. function loadGetInitialProps(App, ctx) {
  53. return _loadGetInitialProps.apply(this, arguments);
  54. }
  55. function _loadGetInitialProps() {
  56. _loadGetInitialProps = _async_to_generator(function*(App, ctx) {
  57. if (process.env.NODE_ENV !== 'production') {
  58. var ref;
  59. if ((ref = App.prototype) == null ? void 0 : ref.getInitialProps) {
  60. const message = `"${getDisplayName(App)}.getInitialProps()" is defined as an instance method - visit https://nextjs.org/docs/messages/get-initial-props-as-an-instance-method for more information.`;
  61. throw new Error(message);
  62. }
  63. }
  64. // when called from _app `ctx` is nested in `ctx`
  65. const res = ctx.res || ctx.ctx && ctx.ctx.res;
  66. if (!App.getInitialProps) {
  67. if (ctx.ctx && ctx.Component) {
  68. // @ts-ignore pageProps default
  69. return {
  70. pageProps: yield loadGetInitialProps(ctx.Component, ctx.ctx)
  71. };
  72. }
  73. return {};
  74. }
  75. const props = yield App.getInitialProps(ctx);
  76. if (res && isResSent(res)) {
  77. return props;
  78. }
  79. if (!props) {
  80. const message = `"${getDisplayName(App)}.getInitialProps()" should resolve to an object. But found "${props}" instead.`;
  81. throw new Error(message);
  82. }
  83. if (process.env.NODE_ENV !== 'production') {
  84. if (Object.keys(props).length === 0 && !ctx.ctx) {
  85. console.warn(`${getDisplayName(App)} returned an empty object from \`getInitialProps\`. This de-optimizes and prevents automatic static optimization. https://nextjs.org/docs/messages/empty-object-getInitialProps`);
  86. }
  87. }
  88. return props;
  89. });
  90. return _loadGetInitialProps.apply(this, arguments);
  91. }
  92. let warnOnce = (_)=>{};
  93. if (process.env.NODE_ENV !== 'production') {
  94. const warnings = new Set();
  95. exports.warnOnce = warnOnce = (msg)=>{
  96. if (!warnings.has(msg)) {
  97. console.warn(msg);
  98. }
  99. warnings.add(msg);
  100. };
  101. }
  102. const SP = typeof performance !== 'undefined';
  103. exports.SP = SP;
  104. const ST = SP && [
  105. 'mark',
  106. 'measure',
  107. 'getEntriesByName'
  108. ].every((method)=>typeof performance[method] === 'function');
  109. exports.ST = ST;
  110. class DecodeError extends Error {
  111. }
  112. exports.DecodeError = DecodeError;
  113. class NormalizeError extends Error {
  114. }
  115. exports.NormalizeError = NormalizeError;
  116. class PageNotFoundError extends Error {
  117. constructor(page){
  118. super();
  119. this.code = 'ENOENT';
  120. this.message = `Cannot find module for page: ${page}`;
  121. }
  122. }
  123. exports.PageNotFoundError = PageNotFoundError;
  124. class MissingStaticPage extends Error {
  125. constructor(page, message){
  126. super();
  127. this.message = `Failed to load static file for page: ${page} ${message}`;
  128. }
  129. }
  130. exports.MissingStaticPage = MissingStaticPage;
  131. class MiddlewareNotFoundError extends Error {
  132. constructor(){
  133. super();
  134. this.code = 'ENOENT';
  135. this.message = `Cannot find the middleware module`;
  136. }
  137. }
  138. exports.MiddlewareNotFoundError = MiddlewareNotFoundError;
  139. exports.warnOnce = warnOnce;
  140. //# sourceMappingURL=utils.js.map