{"ast":null,"code":"\"use strict\";\n\nvar _regeneratorRuntime = require(\"@babel/runtime-corejs2/regenerator\");\n\nvar _slicedToArray = require(\"@babel/runtime-corejs2/helpers/slicedToArray\");\n\nvar _Object$assign = require(\"@babel/runtime-corejs2/core-js/object/assign\");\n\nvar _Promise = require(\"@babel/runtime-corejs2/core-js/promise\");\n\nvar _classCallCheck = require(\"@babel/runtime-corejs2/helpers/classCallCheck\");\n\nvar _createClass = require(\"@babel/runtime-corejs2/helpers/createClass\");\n\nvar _Object$defineProperty = require(\"@babel/runtime-corejs2/core-js/object/define-property\");\n\nvar __importDefault = this && this.__importDefault || function (mod) {\n  return mod && mod.__esModule ? mod : {\n    \"default\": mod\n  };\n};\n\n_Object$defineProperty(exports, \"__esModule\", {\n  value: true\n});\n\nvar url_1 = require(\"url\");\n\nvar mitt_1 = __importDefault(require(\"../mitt\"));\n\nvar utils_1 = require(\"../utils\");\n\nvar is_dynamic_1 = require(\"./utils/is-dynamic\");\n\nvar route_matcher_1 = require(\"./utils/route-matcher\");\n\nvar route_regex_1 = require(\"./utils/route-regex\");\n\nfunction addBasePath(path) {\n  // @ts-ignore variable is always a string\n  var p = process.env.__NEXT_ROUTER_BASEPATH;\n  return path.indexOf(p) !== 0 ? p + path : path;\n}\n\nfunction toRoute(path) {\n  return path.replace(/\\/$/, '') || '/';\n}\n\nvar Router =\n/*#__PURE__*/\nfunction () {\n  function Router(pathname, query, as, _ref) {\n    var _this = this;\n\n    var initialProps = _ref.initialProps,\n        pageLoader = _ref.pageLoader,\n        App = _ref.App,\n        wrapApp = _ref.wrapApp,\n        Component = _ref.Component,\n        err = _ref.err,\n        subscription = _ref.subscription;\n\n    _classCallCheck(this, Router);\n\n    // Static Data Cache\n    this.sdc = {};\n\n    this.onPopState = function (e) {\n      if (!e.state) {\n        // We get state as undefined for two reasons.\n        //  1. With older safari (< 8) and older chrome (< 34)\n        //  2. When the URL changed with #\n        //\n        // In the both cases, we don't need to proceed and change the route.\n        // (as it's already changed)\n        // But we can simply replace the state with the new changes.\n        // Actually, for (1) we don't need to nothing. But it's hard to detect that event.\n        // So, doing the following for (1) does no harm.\n        var _pathname = _this.pathname,\n            _query = _this.query;\n\n        _this.changeState('replaceState', utils_1.formatWithValidation({\n          pathname: _pathname,\n          query: _query\n        }), utils_1.getURL());\n\n        return;\n      } // Make sure we don't re-render on initial load,\n      // can be caused by navigating back from an external site\n\n\n      if (e.state && _this.isSsr && e.state.url === _this.pathname && e.state.as === _this.asPath) {\n        return;\n      } // If the downstream application returns falsy, return.\n      // They will then be responsible for handling the event.\n\n\n      if (_this._bps && !_this._bps(e.state)) {\n        return;\n      }\n\n      var _e$state = e.state,\n          url = _e$state.url,\n          as = _e$state.as,\n          options = _e$state.options;\n\n      if (false) {\n        if (typeof url === 'undefined' || typeof as === 'undefined') {\n          console.warn('`popstate` event triggered but `event.state` did not have `url` or `as` https://err.sh/zeit/next.js/popstate-state-empty');\n        }\n      }\n\n      _this.replace(url, as, options);\n    };\n\n    this._getStaticData = function (asPath, _cachedData) {\n      var pathname = url_1.parse(asPath).pathname;\n      pathname = !pathname || pathname === '/' ? '/index' : pathname;\n      return true && (_cachedData = _this.sdc[pathname]) ? _Promise.resolve(_cachedData) : fetch( // @ts-ignore __NEXT_DATA__\n      \"/_next/data/\".concat(__NEXT_DATA__.buildId).concat(pathname, \".json\")).then(function (res) {\n        if (!res.ok) {\n          throw new Error(\"Failed to load static props\");\n        }\n\n        return res.json();\n      }).then(function (data) {\n        _this.sdc[pathname] = data;\n        return data;\n      })[\"catch\"](function (err) {\n        ;\n        err.code = 'PAGE_LOAD_ERROR';\n        throw err;\n      });\n    }; // represents the current component key\n\n\n    this.route = toRoute(pathname); // set up the component cache (by route keys)\n\n    this.components = {}; // We should not keep the cache, if there's an error\n    // Otherwise, this cause issues when when going back and\n    // come again to the errored page.\n\n    if (pathname !== '/_error') {\n      this.components[this.route] = {\n        Component: Component,\n        props: initialProps,\n        err: err\n      };\n    }\n\n    this.components['/_app'] = {\n      Component: App\n    }; // Backwards compat for Router.router.events\n    // TODO: Should be remove the following major version as it was never documented\n    // @ts-ignore backwards compatibility\n\n    this.events = Router.events;\n    this.pageLoader = pageLoader;\n    this.pathname = pathname;\n    this.query = query; // if auto prerendered and dynamic route wait to update asPath\n    // until after mount to prevent hydration mismatch\n\n    this.asPath = // @ts-ignore this is temporarily global (attached to window)\n    is_dynamic_1.isDynamicRoute(pathname) && __NEXT_DATA__.autoExport ? pathname : as;\n    this.sub = subscription;\n    this.clc = null;\n    this._wrapApp = wrapApp; // make sure to ignore extra popState in safari on navigating\n    // back from external site\n\n    this.isSsr = true;\n\n    if (true) {\n      // in order for `e.state` to work on the `onpopstate` event\n      // we have to register the initial route upon initialization\n      this.changeState('replaceState', utils_1.formatWithValidation({\n        pathname: pathname,\n        query: query\n      }), as);\n      window.addEventListener('popstate', this.onPopState);\n    }\n  } // @deprecated backwards compatibility even though it's a private method.\n\n\n  _createClass(Router, [{\n    key: \"update\",\n    value: function update(route, mod) {\n      var Component = mod[\"default\"] || mod;\n      var data = this.components[route];\n\n      if (!data) {\n        throw new Error(\"Cannot update unavailable route: \".concat(route));\n      }\n\n      var newData = _Object$assign(_Object$assign({}, data), {\n        Component: Component\n      });\n\n      this.components[route] = newData; // pages/_app.js updated\n\n      if (route === '/_app') {\n        this.notify(this.components[this.route]);\n        return;\n      }\n\n      if (route === this.route) {\n        this.notify(newData);\n      }\n    }\n  }, {\n    key: \"reload\",\n    value: function reload() {\n      window.location.reload();\n    }\n    /**\n     * Go back in history\n     */\n\n  }, {\n    key: \"back\",\n    value: function back() {\n      window.history.back();\n    }\n    /**\n     * Performs a `pushState` with arguments\n     * @param url of the route\n     * @param as masks `url` for the browser\n     * @param options object you can define `shallow` and other options\n     */\n\n  }, {\n    key: \"push\",\n    value: function push(url) {\n      var as = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : url;\n      var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n      return this.change('pushState', url, as, options);\n    }\n    /**\n     * Performs a `replaceState` with arguments\n     * @param url of the route\n     * @param as masks `url` for the browser\n     * @param options object you can define `shallow` and other options\n     */\n\n  }, {\n    key: \"replace\",\n    value: function replace(url) {\n      var as = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : url;\n      var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};\n      return this.change('replaceState', url, as, options);\n    }\n  }, {\n    key: \"change\",\n    value: function change(method, _url, _as, options) {\n      var _this2 = this;\n\n      return new _Promise(function (resolve, reject) {\n        if (!options._h) {\n          _this2.isSsr = false;\n        } // marking route changes as a navigation start entry\n\n\n        if (utils_1.ST) {\n          performance.mark('routeChange');\n        } // If url and as provided as an object representation,\n        // we'll format them into the string version here.\n\n\n        var url = typeof _url === 'object' ? utils_1.formatWithValidation(_url) : _url;\n        var as = typeof _as === 'object' ? utils_1.formatWithValidation(_as) : _as; // Add the ending slash to the paths. So, we can serve the\n        // \"<page>/index.html\" directly for the SSR page.\n\n        if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {\n          var rewriteUrlForNextExport = require('./rewrite-url-for-export').rewriteUrlForNextExport; // @ts-ignore this is temporarily global (attached to window)\n\n\n          if (__NEXT_DATA__.nextExport) {\n            as = rewriteUrlForNextExport(as);\n          }\n        }\n\n        _this2.abortComponentLoad(as); // If the url change is only related to a hash change\n        // We should not proceed. We should only change the state.\n        // WARNING: `_h` is an internal option for handing Next.js client-side\n        // hydration. Your app should _never_ use this property. It may change at\n        // any time without notice.\n\n\n        if (!options._h && _this2.onlyAHashChange(as)) {\n          _this2.asPath = as;\n          Router.events.emit('hashChangeStart', as);\n\n          _this2.changeState(method, url, addBasePath(as));\n\n          _this2.scrollToHash(as);\n\n          Router.events.emit('hashChangeComplete', as);\n          return resolve(true);\n        }\n\n        var _url_1$parse = url_1.parse(url, true),\n            pathname = _url_1$parse.pathname,\n            query = _url_1$parse.query,\n            protocol = _url_1$parse.protocol;\n\n        if (!pathname || protocol) {\n          if (false) {\n            throw new Error(\"Invalid href passed to router: \".concat(url, \" https://err.sh/zeit/next.js/invalid-href-passed\"));\n          }\n\n          return resolve(false);\n        } // If asked to change the current URL we should reload the current page\n        // (not location.reload() but reload getInitialProps and other Next.js stuffs)\n        // We also need to set the method = replaceState always\n        // as this should not go into the history (That's how browsers work)\n        // We should compare the new asPath to the current asPath, not the url\n\n\n        if (!_this2.urlIsNew(as)) {\n          method = 'replaceState';\n        } // @ts-ignore pathname is always a string\n\n\n        var route = toRoute(pathname);\n        var _options$shallow = options.shallow,\n            shallow = _options$shallow === void 0 ? false : _options$shallow;\n\n        if (is_dynamic_1.isDynamicRoute(route)) {\n          var _url_1$parse2 = url_1.parse(as),\n              asPathname = _url_1$parse2.pathname;\n\n          var routeMatch = route_matcher_1.getRouteMatcher(route_regex_1.getRouteRegex(route))(asPathname);\n\n          if (!routeMatch) {\n            var error = \"The provided `as` value (\".concat(asPathname, \") is incompatible with the `href` value (\").concat(route, \"). \") + \"Read more: https://err.sh/zeit/next.js/incompatible-href-as\";\n\n            if (false) {\n              throw new Error(error);\n            } else {\n              console.error(error);\n            }\n\n            return resolve(false);\n          } // Merge params into `query`, overwriting any specified in search\n\n\n          _Object$assign(query, routeMatch);\n        }\n\n        Router.events.emit('routeChangeStart', as); // If shallow is true and the route exists in the router cache we reuse the previous result\n        // @ts-ignore pathname is always a string\n\n        _this2.getRouteInfo(route, pathname, query, as, shallow).then(function (routeInfo) {\n          var error = routeInfo.error;\n\n          if (error && error.cancelled) {\n            return resolve(false);\n          }\n\n          Router.events.emit('beforeHistoryChange', as);\n\n          _this2.changeState(method, url, addBasePath(as), options);\n\n          var hash = window.location.hash.substring(1);\n\n          if (false) {\n            var appComp = _this2.components['/_app'].Component;\n            window.next.isPrerendered = appComp.getInitialProps === appComp.origGetInitialProps && !routeInfo.Component.getInitialProps;\n          } // @ts-ignore pathname is always defined\n\n\n          _this2.set(route, pathname, query, as, _Object$assign(_Object$assign({}, routeInfo), {\n            hash: hash\n          }));\n\n          if (error) {\n            Router.events.emit('routeChangeError', error, as);\n            throw error;\n          }\n\n          Router.events.emit('routeChangeComplete', as);\n          return resolve(true);\n        }, reject);\n      });\n    }\n  }, {\n    key: \"changeState\",\n    value: function changeState(method, url, as) {\n      var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};\n\n      if (false) {\n        if (typeof window.history === 'undefined') {\n          console.error(\"Warning: window.history is not available.\");\n          return;\n        } // @ts-ignore method should always exist on history\n\n\n        if (typeof window.history[method] === 'undefined') {\n          console.error(\"Warning: window.history.\".concat(method, \" is not available\"));\n          return;\n        }\n      }\n\n      if (method !== 'pushState' || utils_1.getURL() !== as) {\n        // @ts-ignore method should always exist on history\n        window.history[method]({\n          url: url,\n          as: as,\n          options: options\n        }, null, as);\n      }\n    }\n  }, {\n    key: \"getRouteInfo\",\n    value: function getRouteInfo(route, pathname, query, as) {\n      var _this3 = this;\n\n      var shallow = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;\n      var cachedRouteInfo = this.components[route]; // If there is a shallow route transition possible\n      // If the route is already rendered on the screen.\n\n      if (shallow && cachedRouteInfo && this.route === route) {\n        return _Promise.resolve(cachedRouteInfo);\n      }\n\n      return new _Promise(function (resolve, reject) {\n        if (cachedRouteInfo) {\n          return resolve(cachedRouteInfo);\n        }\n\n        _this3.fetchComponent(route).then(function (Component) {\n          return resolve({\n            Component: Component\n          });\n        }, reject);\n      }).then(function (routeInfo) {\n        var Component = routeInfo.Component;\n\n        if (false) {\n          var _require = require('react-is'),\n              isValidElementType = _require.isValidElementType;\n\n          if (!isValidElementType(Component)) {\n            throw new Error(\"The default export is not a React Component in page: \\\"\".concat(pathname, \"\\\"\"));\n          }\n        }\n\n        return _this3._getData(function () {\n          return Component.__N_SSG ? _this3._getStaticData(as) : _this3.getInitialProps(Component, // we provide AppTree later so this needs to be `any`\n          {\n            pathname: pathname,\n            query: query,\n            asPath: as\n          });\n        }).then(function (props) {\n          routeInfo.props = props;\n          _this3.components[route] = routeInfo;\n          return routeInfo;\n        });\n      })[\"catch\"](function (err) {\n        return new _Promise(function (resolve) {\n          if (err.code === 'PAGE_LOAD_ERROR') {\n            // If we can't load the page it could be one of following reasons\n            //  1. Page doesn't exists\n            //  2. Page does exist in a different zone\n            //  3. Internal error while loading the page\n            // So, doing a hard reload is the proper way to deal with this.\n            window.location.href = as; // Changing the URL doesn't block executing the current code path.\n            // So, we need to mark it as a cancelled error and stop the routing logic.\n\n            err.cancelled = true; // @ts-ignore TODO: fix the control flow here\n\n            return resolve({\n              error: err\n            });\n          }\n\n          if (err.cancelled) {\n            // @ts-ignore TODO: fix the control flow here\n            return resolve({\n              error: err\n            });\n          }\n\n          resolve(_this3.fetchComponent('/_error').then(function (Component) {\n            var routeInfo = {\n              Component: Component,\n              err: err\n            };\n            return new _Promise(function (resolve) {\n              _this3.getInitialProps(Component, {\n                err: err,\n                pathname: pathname,\n                query: query\n              }).then(function (props) {\n                routeInfo.props = props;\n                routeInfo.error = err;\n                resolve(routeInfo);\n              }, function (gipErr) {\n                console.error('Error in error page `getInitialProps`: ', gipErr);\n                routeInfo.error = err;\n                routeInfo.props = {};\n                resolve(routeInfo);\n              });\n            });\n          }));\n        });\n      });\n    }\n  }, {\n    key: \"set\",\n    value: function set(route, pathname, query, as, data) {\n      this.route = route;\n      this.pathname = pathname;\n      this.query = query;\n      this.asPath = as;\n      this.notify(data);\n    }\n    /**\n     * Callback to execute before replacing router state\n     * @param cb callback to be executed\n     */\n\n  }, {\n    key: \"beforePopState\",\n    value: function beforePopState(cb) {\n      this._bps = cb;\n    }\n  }, {\n    key: \"onlyAHashChange\",\n    value: function onlyAHashChange(as) {\n      if (!this.asPath) return false;\n\n      var _this$asPath$split = this.asPath.split('#'),\n          _this$asPath$split2 = _slicedToArray(_this$asPath$split, 2),\n          oldUrlNoHash = _this$asPath$split2[0],\n          oldHash = _this$asPath$split2[1];\n\n      var _as$split = as.split('#'),\n          _as$split2 = _slicedToArray(_as$split, 2),\n          newUrlNoHash = _as$split2[0],\n          newHash = _as$split2[1]; // Makes sure we scroll to the provided hash if the url/hash are the same\n\n\n      if (newHash && oldUrlNoHash === newUrlNoHash && oldHash === newHash) {\n        return true;\n      } // If the urls are change, there's more than a hash change\n\n\n      if (oldUrlNoHash !== newUrlNoHash) {\n        return false;\n      } // If the hash has changed, then it's a hash only change.\n      // This check is necessary to handle both the enter and\n      // leave hash === '' cases. The identity case falls through\n      // and is treated as a next reload.\n\n\n      return oldHash !== newHash;\n    }\n  }, {\n    key: \"scrollToHash\",\n    value: function scrollToHash(as) {\n      var _as$split3 = as.split('#'),\n          _as$split4 = _slicedToArray(_as$split3, 2),\n          hash = _as$split4[1]; // Scroll to top if the hash is just `#` with no value\n\n\n      if (hash === '') {\n        window.scrollTo(0, 0);\n        return;\n      } // First we check if the element by id is found\n\n\n      var idEl = document.getElementById(hash);\n\n      if (idEl) {\n        idEl.scrollIntoView();\n        return;\n      } // If there's no element with the id, we check the `name` property\n      // To mirror browsers\n\n\n      var nameEl = document.getElementsByName(hash)[0];\n\n      if (nameEl) {\n        nameEl.scrollIntoView();\n      }\n    }\n  }, {\n    key: \"urlIsNew\",\n    value: function urlIsNew(asPath) {\n      return this.asPath !== asPath;\n    }\n    /**\n     * Prefetch `page` code, you may wait for the data during `page` rendering.\n     * This feature only works in production!\n     * @param url of prefetched `page`\n     */\n\n  }, {\n    key: \"prefetch\",\n    value: function prefetch(url) {\n      var _this4 = this;\n\n      return new _Promise(function (resolve, reject) {\n        var _url_1$parse3 = url_1.parse(url),\n            pathname = _url_1$parse3.pathname,\n            protocol = _url_1$parse3.protocol;\n\n        if (!pathname || protocol) {\n          if (false) {\n            throw new Error(\"Invalid href passed to router: \".concat(url, \" https://err.sh/zeit/next.js/invalid-href-passed\"));\n          }\n\n          return;\n        } // Prefetch is not supported in development mode because it would trigger on-demand-entries\n\n\n        if (false) {\n          return;\n        } // @ts-ignore pathname is always defined\n\n\n        var route = toRoute(pathname);\n\n        _this4.pageLoader.prefetch(route).then(resolve, reject);\n      });\n    }\n  }, {\n    key: \"fetchComponent\",\n    value: function fetchComponent(route) {\n      var cancelled, cancel, Component, error;\n      return _regeneratorRuntime.async(function fetchComponent$(_context) {\n        while (1) {\n          switch (_context.prev = _context.next) {\n            case 0:\n              cancelled = false;\n\n              cancel = this.clc = function () {\n                cancelled = true;\n              };\n\n              _context.next = 4;\n              return _regeneratorRuntime.awrap(this.pageLoader.loadPage(route));\n\n            case 4:\n              Component = _context.sent;\n\n              if (!cancelled) {\n                _context.next = 9;\n                break;\n              }\n\n              error = new Error(\"Abort fetching component for route: \\\"\".concat(route, \"\\\"\"));\n              error.cancelled = true;\n              throw error;\n\n            case 9:\n              if (cancel === this.clc) {\n                this.clc = null;\n              }\n\n              return _context.abrupt(\"return\", Component);\n\n            case 11:\n            case \"end\":\n              return _context.stop();\n          }\n        }\n      }, null, this);\n    }\n  }, {\n    key: \"_getData\",\n    value: function _getData(fn) {\n      var _this5 = this;\n\n      var cancelled = false;\n\n      var cancel = function cancel() {\n        cancelled = true;\n      };\n\n      this.clc = cancel;\n      return fn().then(function (data) {\n        if (cancel === _this5.clc) {\n          _this5.clc = null;\n        }\n\n        if (cancelled) {\n          var err = new Error('Loading initial props cancelled');\n          err.cancelled = true;\n          throw err;\n        }\n\n        return data;\n      });\n    }\n  }, {\n    key: \"getInitialProps\",\n    value: function getInitialProps(Component, ctx) {\n      var App = this.components['/_app'].Component;\n\n      var AppTree = this._wrapApp(App);\n\n      ctx.AppTree = AppTree;\n      return utils_1.loadGetInitialProps(App, {\n        AppTree: AppTree,\n        Component: Component,\n        router: this,\n        ctx: ctx\n      });\n    }\n  }, {\n    key: \"abortComponentLoad\",\n    value: function abortComponentLoad(as) {\n      if (this.clc) {\n        var e = new Error('Route Cancelled');\n        e.cancelled = true;\n        Router.events.emit('routeChangeError', e, as);\n        this.clc();\n        this.clc = null;\n      }\n    }\n  }, {\n    key: \"notify\",\n    value: function notify(data) {\n      this.sub(data, this.components['/_app'].Component);\n    }\n  }], [{\n    key: \"_rewriteUrlForNextExport\",\n    value: function _rewriteUrlForNextExport(url) {\n      if (process.env.__NEXT_EXPORT_TRAILING_SLASH) {\n        var rewriteUrlForNextExport = require('./rewrite-url-for-export').rewriteUrlForNextExport;\n\n        return rewriteUrlForNextExport(url);\n      } else {\n        return url;\n      }\n    }\n  }]);\n\n  return Router;\n}();\n\nexports[\"default\"] = Router;\nRouter.events = mitt_1[\"default\"]();","map":null,"metadata":{},"sourceType":"script"}