UNPKG

next

Version:

The React Framework

6 lines 2 kB
"use strict";exports.__esModule=true;exports.default=resolveRewrites;var _pathMatch=_interopRequireDefault(require("./path-match"));var _prepareDestination=_interopRequireDefault(require("./prepare-destination"));var _normalizeTrailingSlash=require("../../../../client/normalize-trailing-slash");var _normalizeLocalePath=require("../../i18n/normalize-locale-path");var _parseRelativeUrl=require("./parse-relative-url");var _router=require("../router");function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}const customRouteMatcher=(0,_pathMatch.default)(true);function resolveRewrites(asPath,pages,rewrites,query,resolveHref,locales){let matchedPage=false;let parsedAs=(0,_parseRelativeUrl.parseRelativeUrl)(asPath);let fsPathname=(0,_normalizeTrailingSlash.removePathTrailingSlash)((0,_normalizeLocalePath.normalizeLocalePath)((0,_router.delBasePath)(parsedAs.pathname),locales).pathname);let resolvedHref;if(!pages.includes(fsPathname)){for(const rewrite of rewrites){const matcher=customRouteMatcher(rewrite.source);const params=matcher(parsedAs.pathname);if(params){if(!rewrite.destination){// this is a proxied rewrite which isn't handled on the client break;}const destRes=(0,_prepareDestination.default)(rewrite.destination,params,query,true);parsedAs=destRes.parsedDestination;asPath=destRes.newUrl;Object.assign(query,destRes.parsedDestination.query);fsPathname=(0,_normalizeTrailingSlash.removePathTrailingSlash)((0,_normalizeLocalePath.normalizeLocalePath)((0,_router.delBasePath)(asPath),locales).pathname);if(pages.includes(fsPathname)){// check if we now match a page as this means we are done // resolving the rewrites matchedPage=true;resolvedHref=fsPathname;break;}// check if we match a dynamic-route, if so we break the rewrites chain resolvedHref=resolveHref(fsPathname);if(resolvedHref!==asPath&&pages.includes(resolvedHref)){matchedPage=true;break;}}}}return{asPath,parsedAs,matchedPage,resolvedHref};} //# sourceMappingURL=resolve-rewrites.js.map