@next/routing 16.2.4 → 16.3.0-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- (()=>{"use strict";var e={};(()=>{e.d=(t,n)=>{for(var a in n){if(e.o(n,a)&&!e.o(t,a)){Object.defineProperty(t,a,{enumerable:true,get:n[a]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();(()=>{e.r=e=>{if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})}})();if(typeof e!=="undefined")e.ab=__dirname+"/";var t={};e.r(t);e.d(t,{detectDomainLocale:()=>detectDomainLocale,detectLocale:()=>detectLocale,getAcceptLanguageLocale:()=>getAcceptLanguageLocale,getCookieLocale:()=>getCookieLocale,normalizeLocalePath:()=>normalizeLocalePath,resolveRoutes:()=>resolveRoutes,responseToMiddlewareResult:()=>responseToMiddlewareResult});function matchesCondition(e,t){if(e===undefined){return{matched:false}}if(t===undefined){return{matched:true,capturedValue:e}}try{const n=new RegExp(t);const a=e.match(n);if(a){return{matched:true,capturedValue:a[0]}}}catch(e){}if(e===t){return{matched:true,capturedValue:e}}return{matched:false}}function getConditionValue(e,t,n){switch(e.type){case"header":return n.get(e.key)||undefined;case"cookie":{const t=n.get("cookie");if(!t)return undefined;const a=t.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n){e[n]=a.join("=")}return e}),{});return a[e.key]}case"query":return t.searchParams.get(e.key)||undefined;case"host":return t.hostname;default:return""}}function normalizeCaptureKey(e){return e.replace(/[^a-zA-Z]/g,"")}function checkHasConditions(e,t,n){if(!e||e.length===0){return{matched:true,captures:{}}}const a={};for(const r of e){const e=getConditionValue(r,t,n);const s=matchesCondition(e,r.value);if(!s.matched){return{matched:false,captures:{}}}if(s.capturedValue!==undefined&&r.type!=="host"){const e=normalizeCaptureKey(r.key);a[e]=s.capturedValue}}return{matched:true,captures:a}}function checkMissingConditions(e,t,n){if(!e||e.length===0){return true}for(const a of e){const e=getConditionValue(a,t,n);const r=matchesCondition(e,a.value);if(r.matched){return false}}return true}function replaceDestination(e,t,n){let a=e;if(t){for(let e=1;e<t.length;e++){const n=t[e];if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}if(t.groups){for(const[e,n]of Object.entries(t.groups)){if(n!==undefined){a=a.replace(new RegExp(`\\$${e}`,"g"),n)}}}}for(const[e,t]of Object.entries(n)){a=a.replace(new RegExp(`\\$${e}`,"g"),t)}return a}function isExternalDestination(e){return e.startsWith("http://")||e.startsWith("https://")}function applyDestination(e,t){if(isExternalDestination(t)){return new URL(t)}const n=new URL(e.toString());const[a,r]=t.split("?");n.pathname=a;if(r){const e=new URLSearchParams(r);for(const[t,a]of e.entries()){n.searchParams.set(t,a)}}return n}function isRedirectStatus(e){if(!e)return false;return e>=300&&e<400}function hasRedirectHeaders(e){const t=Object.keys(e).map((e=>e.toLowerCase()));return t.includes("location")||t.includes("refresh")}function normalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let r=a.pathname;const s=`${t}/_next/data/${n}/`;if(r.startsWith(s)){let e=r.slice(s.length);if(e.endsWith(".json")){e=e.slice(0,-5)}r=t?`${t}/${e}`:`/${e}`;a.pathname=r}return a}function denormalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let r=a.pathname;const s=`${t}/_next/data/${n}/`;if(!r.startsWith(s)){let e=r;if(t&&r.startsWith(t)){e=r.slice(t.length)}r=`${t}/_next/data/${n}${e}.json`;a.pathname=r}return a}function detectDomainLocale(e,t,n){if(!e)return undefined;const a=t?.toLowerCase();const r=n?.toLowerCase();for(const t of e){const e=t.domain.split(":",1)[0].toLowerCase();if(a===e||r===t.defaultLocale.toLowerCase()||t.locales?.some((e=>e.toLowerCase()===r))){return t}}return undefined}function normalizeLocalePath(e,t){if(!t||t.length===0){return{pathname:e}}const n=e.split("/",2);if(!n[1]){return{pathname:e}}const a=n[1].toLowerCase();const r=t.map((e=>e.toLowerCase()));const s=r.indexOf(a);if(s<0){return{pathname:e}}const o=t[s];const i=e.slice(o.length+1)||"/";return{pathname:i,detectedLocale:o}}function getAcceptLanguageLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(",").map((e=>{const t=e.trim().split(";");const n=t[0];let a=1;if(t[1]){const e=t[1].match(/q=([0-9.]+)/);if(e&&e[1]){a=parseFloat(e[1])}}return{locale:n,quality:a}})).filter((e=>e.quality>0)).sort(((e,t)=>t.quality-e.quality));const a=new Map;for(const e of t){a.set(e.toLowerCase(),e)}for(const{locale:e}of n){const t=e.toLowerCase();if(a.has(t)){return a.get(t)}}for(const{locale:e}of n){const t=e.toLowerCase().split("-")[0];if(a.has(t)){return a.get(t)}for(const[e,n]of a){if(e.startsWith(t+"-")){return n}}}return undefined}catch(e){return undefined}}function getCookieLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n&&a.length>0){e[n]=decodeURIComponent(a.join("="))}return e}),{});const a=n.NEXT_LOCALE?.toLowerCase();if(!a){return undefined}return t.find((e=>e.toLowerCase()===a))}catch(e){return undefined}}function detectLocale(e){const{pathname:t,hostname:n,cookieHeader:a,acceptLanguageHeader:r,i18n:s}=e;const o=normalizeLocalePath(t,s.locales);if(o.detectedLocale){return{locale:o.detectedLocale,pathnameWithoutLocale:o.pathname,localeInPath:true}}if(s.localeDetection===false){const e=detectDomainLocale(s.domains,n);return{locale:e?.defaultLocale||s.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}const i=getCookieLocale(a,s.locales);if(i){return{locale:i,pathnameWithoutLocale:t,localeInPath:false}}const c=getAcceptLanguageLocale(r||"",s.locales);if(c){return{locale:c,pathnameWithoutLocale:t,localeInPath:false}}const l=detectDomainLocale(s.domains,n);if(l){return{locale:l.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}return{locale:s.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}function matchRoute(e,t,n){const a=new RegExp(e.sourceRegex);const r=t.pathname.match(a);if(!r){return{matched:false}}const s=checkHasConditions(e.has,t,n);if(!s.matched){return{matched:false}}const o=checkMissingConditions(e.missing,t,n);if(!o){return{matched:false}}const i=e.destination?replaceDestination(e.destination,r,s.captures):undefined;const c=e.headers?Object.fromEntries(Object.entries(e.headers).map((([e,t])=>[replaceDestination(e,r,s.captures),replaceDestination(t,r,s.captures)]))):undefined;return{matched:true,destination:i,headers:c,regexMatches:r,hasCaptures:s.captures}}function processRoutes(e,t,n,a,r){let s=t;let o;for(const t of e){const e=matchRoute(t,s,n);if(e.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){a.set(t,n)}}if(t.status){o=t.status}if(e.destination){if(isRedirectStatus(t.status)&&e.headers&&hasRedirectHeaders(e.headers)){const n=isExternalDestination(e.destination)?new URL(e.destination):applyDestination(s,e.destination);return{url:s,redirect:{url:n,status:t.status},stopped:true,status:o}}if(isExternalDestination(e.destination)){return{url:s,externalRewrite:new URL(e.destination),stopped:true,status:o}}s=applyDestination(s,e.destination);if(s.origin!==r){return{url:s,externalRewrite:s,stopped:true,status:o}}}}}return{url:s,stopped:false,status:o}}function matchesPathname(e,t){for(const n of t){if(e===n){return n}}return undefined}function toResolvedQuery(e){const t={};for(const[n,a]of e.searchParams.entries()){const e=t[n];if(e===undefined){t[n]=a;continue}t[n]=Array.isArray(e)?[...e,a]:[e,a]}return t}function mergeDestinationQueryIntoUrl(e,t){const n=new URL(e.toString());const a=t.split("?")[1];if(!a){return n}const r=new URLSearchParams(a);for(const[e,t]of r.entries()){n.searchParams.set(e,t)}return n}function withResolvedInvocationTarget({result:e,url:t,resolvedPathname:n,invocationPathname:a}){const r=toResolvedQuery(t);return{...e,resolvedPathname:n,resolvedQuery:r,invocationTarget:{pathname:a,query:r}}}function matchDynamicRoute(e,t){const n=new RegExp(t.sourceRegex);const a=e.match(n);if(!a){return{matched:false}}const r={};for(let e=1;e<a.length;e++){if(a[e]!==undefined){r[String(e)]=a[e]}}if(a.groups){Object.assign(r,a.groups)}return{matched:true,params:r,regexMatches:a}}function applyOnMatchHeaders(e,t,n,a){const r=new Headers(a);for(const a of e){const e=matchRoute(a,t,n);if(e.matched&&e.headers){for(const[t,n]of Object.entries(e.headers)){r.set(t,n)}}}return r}function checkDynamicRoutes(e,t,n,a,r,s,o,i,c,l){let d=t;if(l&&c){d=denormalizeNextDataUrl(t,o,i)}for(const t of e){const e=matchDynamicRoute(d.pathname,t);if(e.matched){const o=checkHasConditions(t.has,d,a);const i=checkMissingConditions(t.missing,d,a);if(o.matched&&i){const i=t.destination?replaceDestination(t.destination,e.regexMatches||null,o.captures):undefined;const c=i?i.split("?")[0]:d.pathname;const l=matchesPathname(c,n);if(l){const t=i?mergeDestinationQueryIntoUrl(d,i):d;const n=applyOnMatchHeaders(s,t,a,r);const o=withResolvedInvocationTarget({result:{routeMatches:e.params,resolvedHeaders:n},url:t,resolvedPathname:l,invocationPathname:d.pathname});return{matched:true,result:o,resetUrl:d}}}}}return{matched:false}}async function resolveRoutes(e){const{url:t,basePath:n,requestBody:a,headers:r,pathnames:s,routes:o,invokeMiddleware:i,buildId:c,i18n:l}=e;const{shouldNormalizeNextData:d}=o;let u=new URL(t.toString());let f=new Headers(r);let h=new Headers;let m;const p=t.origin;let g=false;if(d){const e=`${n}/_next/data/${c}/`;g=t.pathname.startsWith(e);if(g){u=normalizeNextDataUrl(u,n,c)}}if(l&&!g){const e=u.pathname.startsWith(n)?u.pathname.slice(n.length)||"/":u.pathname;if(!e.startsWith("/_next/")&&!e.startsWith("/api/")){const t=u.hostname;const a=f.get("cookie")||undefined;const r=f.get("accept-language")||undefined;const s=normalizeLocalePath(e,l.locales);const o=!!s.detectedLocale;const i=detectDomainLocale(l.domains,t);const c=i?.defaultLocale||l.defaultLocale;let d=s.detectedLocale||c;if(l.localeDetection!==false&&!o){const s=detectLocale({pathname:e,hostname:t,cookieHeader:a,acceptLanguageHeader:r,i18n:l});d=s.locale;if(d!==c){const a=detectDomainLocale(l.domains,undefined,d);if(a&&a.domain!==t){const t=a.http?"http":"https";const r=d===a.defaultLocale?"":`/${d}`;const s=new URL(`${t}://${a.domain}${n}${r}${e}${u.search}`);return{redirect:{url:s,status:307},resolvedHeaders:h}}if(!a||a&&a.domain===t){const t=new URL(u.toString());t.pathname=`${n}/${d}${e}`;return{redirect:{url:t,status:307},resolvedHeaders:h}}}}if(!o){const t=d||i?.defaultLocale||l.defaultLocale;u.pathname=`${n}/${t}${e}`}}}const w=processRoutes(o.beforeMiddleware,u,f,h,p);if(w.status){m=w.status}if(w.redirect){return{redirect:w.redirect,resolvedHeaders:h,status:m}}if(w.externalRewrite){return{externalRewrite:w.externalRewrite,resolvedHeaders:h,status:m}}u=w.url;if(g&&d){u=denormalizeNextDataUrl(u,n,c)}const L=await i({url:u,headers:f,requestBody:a});if(L.bodySent){return{middlewareResponded:true}}if(L.requestHeaders){f=new Headers(L.requestHeaders)}if(L.responseHeaders){L.responseHeaders.forEach(((e,t)=>{if(t.toLowerCase()==="set-cookie"){h.append(t,e)}else{h.set(t,e)}}))}if(L.redirect){if(!h.has("location")){h.set("Location",L.redirect.url.toString())}return{resolvedHeaders:h,status:L.redirect.status}}if(L.rewrite){u=L.rewrite;if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}}if(g&&d){u=normalizeNextDataUrl(u,n,c)}const R=processRoutes(o.beforeFiles,u,f,h,p);if(R.status){m=R.status}if(R.redirect){return{redirect:R.redirect,resolvedHeaders:h,status:m}}if(R.externalRewrite){return{externalRewrite:R.externalRewrite,resolvedHeaders:h,status:m}}u=R.url;if(g&&d){u=denormalizeNextDataUrl(u,n,c)}let y=matchesPathname(u.pathname,s);if(y){for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const a=e.destination?replaceDestination(e.destination,t.regexMatches||null,n.captures):undefined;const r=a?mergeDestinationQueryIntoUrl(u,a):u;const s=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:s,status:m},url:r,resolvedPathname:y,invocationPathname:u.pathname})}}}const e=applyOnMatchHeaders(o.onMatch,u,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:u,resolvedPathname:y,invocationPathname:u.pathname})}if(g&&d){u=normalizeNextDataUrl(u,n,c)}for(const e of o.afterFiles){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,h,o.onMatch,n,c,d,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let r=u;if(g&&d){r=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(r.pathname,s);if(y){const e=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:r,resolvedPathname:y,invocationPathname:r.pathname})}}}}for(const e of o.dynamicRoutes){const t=matchDynamicRoute(u.pathname,e);if(t.matched){const n=checkHasConditions(e.has,u,f);const a=checkMissingConditions(e.missing,u,f);if(n.matched&&a){const a=e.destination?replaceDestination(e.destination,t.regexMatches||null,n.captures):undefined;const r=a?a.split("?")[0]:u.pathname;y=matchesPathname(r,s);if(y){const e=a?mergeDestinationQueryIntoUrl(u,a):u;const n=applyOnMatchHeaders(o.onMatch,e,f,h);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:n,status:m},url:e,resolvedPathname:y,invocationPathname:u.pathname})}}}}for(const e of o.fallback){const t=matchRoute(e,u,f);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){h.set(e,n)}}if(e.status){m=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(u,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:h,status:m}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:h,status:m}}u=applyDestination(u,t.destination);if(u.origin!==p){return{externalRewrite:u,resolvedHeaders:h,status:m}}const a=checkDynamicRoutes(o.dynamicRoutes,u,s,f,h,o.onMatch,n,c,d,g);if(a.matched&&a.result){if(a.resetUrl){u=a.resetUrl}return{...a.result,status:m}}let r=u;if(g&&d){r=denormalizeNextDataUrl(u,n,c)}y=matchesPathname(r.pathname,s);if(y){const e=applyOnMatchHeaders(o.onMatch,r,f,h);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:m},url:r,resolvedPathname:y,invocationPathname:r.pathname})}}}}return{resolvedHeaders:h,status:m}}function responseToMiddlewareResult(e,t,a){const r={};const s={};e.headers.forEach(((e,t)=>{if(s[t]){const n=s[t];if(Array.isArray(n)){n.push(e)}else{s[t]=[n,e]}}else{s[t]=e}}));if(s["x-middleware-override-headers"]){const e=new Set;let n=s["x-middleware-override-headers"];if(typeof n==="string"){n=n.split(",")}for(const t of n){e.add(t.trim())}delete s["x-middleware-override-headers"];const a=[];t.forEach(((t,n)=>{if(!e.has(n)){a.push(n)}}));for(const e of a){t.delete(e)}for(const n of e.keys()){const e="x-middleware-request-"+n;const a=s[e];if(a===undefined||a===null){t.delete(n)}else if(Array.isArray(a)){t.set(n,a[0]);for(let e=1;e<a.length;e++){t.append(n,a[e])}}else{t.set(n,a)}delete s[e]}}if(!s["x-middleware-rewrite"]&&!s["x-middleware-next"]&&!s["location"]){s["x-middleware-refresh"]="1"}delete s["x-middleware-next"];const o=new Headers;for(const[e,n]of Object.entries(s)){if(["content-length","x-middleware-rewrite","x-middleware-redirect","x-middleware-refresh"].includes(e)){continue}if(e==="x-middleware-set-cookie"){if(n!==undefined){if(Array.isArray(n)){for(const a of n){t.append(e,a)}}else{t.set(e,n)}}continue}if(n!==undefined){if(Array.isArray(n)){for(const a of n){o.append(e,a);t.append(e,a)}}else{o.set(e,n);t.set(e,n)}}}r.responseHeaders=o;r.requestHeaders=t;if(s["x-middleware-rewrite"]){const e=s["x-middleware-rewrite"];const t=getRelativeURL(e,a.toString());o.set("x-middleware-rewrite",t);try{const e=new URL(t,a);if(e.origin!==a.origin){r.rewrite=e;return r}r.rewrite=e}catch{r.rewrite=new URL(t,a)}}if(s["location"]){const t=s["location"];const i=n.has(e.status);if(i){const n=getRelativeURL(t,a.toString());o.set("location",n);try{const t=new URL(n,a);r.redirect={url:t,status:e.status};return r}catch{r.redirect={url:new URL(n,a),status:e.status};return r}}else{o.set("location",t);return r}}if(s["x-middleware-refresh"]){r.bodySent=true;return r}return r}function getRelativeURL(e,t){try{const n=new URL(e,t);const a=new URL(t);if(n.origin===a.origin){return n.pathname+n.search+n.hash}return n.toString()}catch{return e}}const n=new Set([301,302,303,307,308]);module.exports=t})();
1
+ (()=>{"use strict";var e={};(()=>{e.d=(t,n)=>{for(var a in n){if(e.o(n,a)&&!e.o(t,a)){Object.defineProperty(t,a,{enumerable:true,get:n[a]})}}}})();(()=>{e.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t)})();(()=>{e.r=e=>{if(typeof Symbol!=="undefined"&&Symbol.toStringTag){Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}Object.defineProperty(e,"__esModule",{value:true})}})();if(typeof e!=="undefined")e.ab=__dirname+"/";var t={};e.r(t);e.d(t,{detectDomainLocale:()=>detectDomainLocale,detectLocale:()=>detectLocale,getAcceptLanguageLocale:()=>getAcceptLanguageLocale,getCookieLocale:()=>getCookieLocale,normalizeLocalePath:()=>normalizeLocalePath,resolveRoutes:()=>resolveRoutes,responseToMiddlewareResult:()=>responseToMiddlewareResult});function matchesCondition(e,t){if(e===undefined){return{matched:false}}if(t===undefined){return{matched:true,capturedValue:e}}try{const n=new RegExp(`^(?:${t})$`);const a=new RegExp(t);const s=e.match(n)??e.match(a);if(s){const e={};if(s.groups){for(const[t,n]of Object.entries(s.groups)){if(typeof n==="string"&&n.length>0){e[t]=n}}}return{matched:true,capturedValue:s[0],...Object.keys(e).length>0?{namedCaptures:e}:{}}}}catch(e){}if(e===t){return{matched:true,capturedValue:e}}return{matched:false}}function getConditionValue(e,t,n){switch(e.type){case"header":return n.get(e.key)||undefined;case"cookie":{const t=n.get("cookie");if(!t)return undefined;const a=t.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n){e[n]=a.join("=")}return e}),{});return a[e.key]}case"query":return t.searchParams.get(e.key)||undefined;case"host":return t.hostname;default:return""}}function normalizeCaptureKey(e){return e.replace(/[^a-zA-Z]/g,"")}function checkHasConditions(e,t,n){if(!e||e.length===0){return{matched:true,captures:{}}}const a={};for(const s of e){const e=getConditionValue(s,t,n);const r=matchesCondition(e,s.value);if(!r.matched){return{matched:false,captures:{}}}if(r.namedCaptures){for(const[e,t]of Object.entries(r.namedCaptures)){a[e]=t}}if(r.capturedValue!==undefined&&s.type!=="host"){const e=normalizeCaptureKey(s.key);a[e]=r.capturedValue}}return{matched:true,captures:a}}function checkMissingConditions(e,t,n){if(!e||e.length===0){return true}for(const a of e){const e=getConditionValue(a,t,n);const s=matchesCondition(e,a.value);if(s.matched){return false}}return true}function replaceDestination(e,t,n){let a=e;if(t){for(let e=1;e<t.length;e++){const n=t[e]??"";a=a.replace(new RegExp(`\\$${e}`,"g"),n)}if(t.groups){for(const[e,n]of Object.entries(t.groups)){a=a.replace(new RegExp(`\\$${e}`,"g"),n??"")}}}for(const[e,t]of Object.entries(n)){a=a.replace(new RegExp(`\\$${e}`,"g"),t)}return a}function isExternalDestination(e){return e.startsWith("http://")||e.startsWith("https://")}function applyDestination(e,t){if(isExternalDestination(t)){return new URL(t)}const n=new URL(e.toString());const[a,s]=t.split("?");n.pathname=a;if(s){const e=new URLSearchParams(s);for(const[t,a]of e.entries()){n.searchParams.set(t,a)}}return n}function isRedirectStatus(e){if(!e)return false;return e>=300&&e<400}function hasRedirectHeaders(e){const t=Object.keys(e).map((e=>e.toLowerCase()));return t.includes("location")||t.includes("refresh")}function normalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let s=a.pathname;const r=`${t}/_next/data/${n}/`;if(s.startsWith(r)){let e=s.slice(r.length);if(e.endsWith(".json")){e=e.slice(0,-5)}if(e==="index"){s=t||"/"}else{s=t?`${t}/${e}`:`/${e}`}a.pathname=s}return a}function denormalizeNextDataUrl(e,t,n){const a=new URL(e.toString());let s=a.pathname;const r=`${t}/_next/data/${n}/`;if(!s.startsWith(r)){let e=s;if(t&&s.startsWith(t)){e=s.slice(t.length)}if(e===""||e==="/"){e="/index"}s=`${t}/_next/data/${n}${e}.json`;a.pathname=s}return a}function detectDomainLocale(e,t,n){if(!e)return undefined;const a=t?.toLowerCase();const s=n?.toLowerCase();for(const t of e){const e=t.domain.split(":",1)[0].toLowerCase();if(a===e||s===t.defaultLocale.toLowerCase()||t.locales?.some((e=>e.toLowerCase()===s))){return t}}return undefined}function normalizeLocalePath(e,t){if(!t||t.length===0){return{pathname:e}}const n=e.split("/",2);if(!n[1]){return{pathname:e}}const a=n[1].toLowerCase();const s=t.map((e=>e.toLowerCase()));const r=s.indexOf(a);if(r<0){return{pathname:e}}const o=t[r];const i=e.slice(o.length+1)||"/";return{pathname:i,detectedLocale:o}}function getAcceptLanguageLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(",").map((e=>{const t=e.trim().split(";");const n=t[0];let a=1;if(t[1]){const e=t[1].match(/q=([0-9.]+)/);if(e&&e[1]){a=parseFloat(e[1])}}return{locale:n,quality:a}})).filter((e=>e.quality>0)).sort(((e,t)=>t.quality-e.quality));const a=new Map;for(const e of t){a.set(e.toLowerCase(),e)}for(const{locale:e}of n){const t=e.toLowerCase();if(a.has(t)){return a.get(t)}}for(const{locale:e}of n){const t=e.toLowerCase().split("-")[0];if(a.has(t)){return a.get(t)}for(const[e,n]of a){if(e.startsWith(t+"-")){return n}}}return undefined}catch(e){return undefined}}function getCookieLocale(e,t){if(!e||!t.length){return undefined}try{const n=e.split(";").reduce(((e,t)=>{const[n,...a]=t.trim().split("=");if(n&&a.length>0){e[n]=decodeURIComponent(a.join("="))}return e}),{});const a=n.NEXT_LOCALE?.toLowerCase();if(!a){return undefined}return t.find((e=>e.toLowerCase()===a))}catch(e){return undefined}}function detectLocale(e){const{pathname:t,hostname:n,cookieHeader:a,acceptLanguageHeader:s,i18n:r}=e;const o=normalizeLocalePath(t,r.locales);if(o.detectedLocale){return{locale:o.detectedLocale,pathnameWithoutLocale:o.pathname,localeInPath:true}}if(r.localeDetection===false){const e=detectDomainLocale(r.domains,n);return{locale:e?.defaultLocale||r.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}const i=getCookieLocale(a,r.locales);if(i){return{locale:i,pathnameWithoutLocale:t,localeInPath:false}}const c=getAcceptLanguageLocale(s||"",r.locales);if(c){return{locale:c,pathnameWithoutLocale:t,localeInPath:false}}const l=detectDomainLocale(r.domains,n);if(l){return{locale:l.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}return{locale:r.defaultLocale,pathnameWithoutLocale:t,localeInPath:false}}function getHeaderValueCaseInsensitive(e,t){const n=t.toLowerCase();for(const[t,a]of Object.entries(e)){if(t.toLowerCase()===n){return a}}return undefined}function resolveRedirectLocationWithRequestQuery(e,t){if(!t.search){return e}try{const n=new URL(e,t);if(n.search){return e}n.search=t.search;if(n.origin!==t.origin){return n.toString()}return`${n.pathname}${n.search}${n.hash}`}catch{return e}}function matchRoute(e,t,n,a){const s=new RegExp(e.sourceRegex,a?"":"i");const r=t.pathname.match(s);if(!r){return{matched:false}}const o=checkHasConditions(e.has,t,n);if(!o.matched){return{matched:false}}const i=checkMissingConditions(e.missing,t,n);if(!i){return{matched:false}}const c=e.destination?replaceDestination(e.destination,r,o.captures):undefined;const l=e.headers?Object.fromEntries(Object.entries(e.headers).map((([e,t])=>[replaceDestination(e,r,o.captures),replaceDestination(t,r,o.captures)]))):undefined;return{matched:true,destination:c,headers:l,regexMatches:r,hasCaptures:o.captures}}function processRoutes(e,t,n,a,s,r){let o=t;let i;for(const t of e){const e=matchRoute(t,o,n,r);if(e.matched){if(e.headers){for(const[t,n]of Object.entries(e.headers)){a.set(t,n)}}if(t.status){i=t.status}if(isRedirectStatus(t.status)&&e.headers&&hasRedirectHeaders(e.headers)){if(e.destination){const n=isExternalDestination(e.destination)?new URL(e.destination):applyDestination(o,e.destination);return{url:o,redirect:{url:n,status:t.status},stopped:true,status:i}}const n=getHeaderValueCaseInsensitive(e.headers,"location");if(n){a.set("location",resolveRedirectLocationWithRequestQuery(n,o))}return{url:o,stopped:true,status:i}}if(e.destination){if(isExternalDestination(e.destination)){return{url:o,externalRewrite:new URL(e.destination),stopped:true,status:i}}o=applyDestination(o,e.destination);if(o.origin!==s){return{url:o,externalRewrite:o,stopped:true,status:i}}}}}return{url:o,stopped:false,status:i}}function matchesPathname(e,t){for(const n of t){if(e===n){return n}}return undefined}function matchesPathnameWithLocaleFallback({pathname:e,pathnames:t,basePath:n,i18n:a}){const s=matchesPathname(e,t);if(s||!a){return s}const r=n&&e.startsWith(n)?e.slice(n.length)||"/":e;for(const e of a.locales){const a=`/${e}`;if(r!==a&&!r.startsWith(`${a}/`)){continue}const s=r===a?"/":r.slice(a.length)||"/";const o=n?`${n}${s}`:s;const i=matchesPathname(o,t);if(i){return i}}return undefined}function isDynamicTemplatePathname(e){return/\[[^/]+?\]/.test(e)}function toResolvedQuery(e){const t={};for(const[n,a]of e.searchParams.entries()){const e=t[n];if(e===undefined){t[n]=a;continue}t[n]=Array.isArray(e)?[...e,a]:[e,a]}return t}function mergeDestinationQueryIntoUrl(e,t){const n=new URL(e.toString());const a=t.split("?")[1];if(!a){return n}const s=new URLSearchParams(a);for(const[e,t]of s.entries()){n.searchParams.set(e,t)}return n}function withResolvedInvocationTarget({result:e,url:t,resolvedPathname:n,invocationPathname:a}){const s=toResolvedQuery(t);return{...e,resolvedPathname:n,resolvedQuery:s,invocationTarget:{pathname:a,query:s}}}function matchDynamicRoute(e,t,n){const a=new RegExp(t.sourceRegex,n?"":"i");const s=e.match(a);if(!s){return{matched:false}}const r={};for(let e=1;e<s.length;e++){if(s[e]!==undefined){r[String(e)]=s[e]}}if(s.groups){Object.assign(r,s.groups)}return{matched:true,params:r,regexMatches:s}}function applyOnMatchHeaders(e,t,n,a,s){const r=new Headers(a);for(const a of e){const e=matchRoute(a,t,n,s);if(e.matched&&e.headers){for(const[t,n]of Object.entries(e.headers)){r.set(t,n)}}}return r}function checkDynamicRoutes(e,t,n,a,s,r,o,i,c,l,u,d=false){let f=t;if(u&&l){f=denormalizeNextDataUrl(t,o,i)}for(const t of e){const e=matchDynamicRoute(f.pathname,t,d);if(e.matched){const i=checkHasConditions(t.has,f,a);const l=checkMissingConditions(t.missing,f,a);if(i.matched&&l){const l=t.destination?replaceDestination(t.destination,e.regexMatches||null,i.captures):undefined;const u=l?l.split("?")[0]:f.pathname;const h=matchesPathnameWithLocaleFallback({pathname:u,pathnames:n,basePath:o,i18n:c});if(h){const t=l?mergeDestinationQueryIntoUrl(f,l):f;const n=applyOnMatchHeaders(r,t,a,s,d);const o=withResolvedInvocationTarget({result:{routeMatches:e.params,resolvedHeaders:n},url:t,resolvedPathname:h,invocationPathname:f.pathname});return{matched:true,result:o,resetUrl:f}}}}}return{matched:false}}function shouldInvokeMiddlewareForRequest(e,t,n,a){const matchesMiddlewareMatchers=s=>{if(!e||e.length===0){return false}for(const r of e){const e=new RegExp(r.sourceRegex,a?"":"i");const o=s.match(e);if(!o){continue}const i=checkHasConditions(r.has,t,n);if(!i.matched){continue}const c=checkMissingConditions(r.missing,t,n);if(!c){continue}return true}return false};if(e===undefined){return true}if(e.length===0){return false}if(matchesMiddlewareMatchers(t.pathname)){return true}let s=t.pathname;try{s=decodeURIComponent(t.pathname)}catch{return false}if(s===t.pathname){return false}return matchesMiddlewareMatchers(s)}async function resolveRoutes(e){const{url:t,basePath:n,requestBody:a,headers:s,pathnames:r,routes:o,invokeMiddleware:i,buildId:c,i18n:l}=e;const{shouldNormalizeNextData:u,caseSensitive:d=false}=o;let f=new URL(t.toString());let h=new Headers(s);let m=new Headers;let p;let g;let w;const R=t.origin;let L=false;if(u){const e=`${n}/_next/data/${c}/`;L=t.pathname.startsWith(e);if(L){f=normalizeNextDataUrl(f,n,c)}}if(l&&!L){const e=f.pathname.startsWith(n)?f.pathname.slice(n.length)||"/":f.pathname;if(!e.startsWith("/_next/")&&!e.startsWith("/api/")){const t=f.hostname;const a=h.get("cookie")||undefined;const s=h.get("accept-language")||undefined;const r=normalizeLocalePath(e,l.locales);const o=!!r.detectedLocale;const i=detectDomainLocale(l.domains,t);const c=i?.defaultLocale||l.defaultLocale;let u=r.detectedLocale||c;const d=l.localeDetection!==false&&!o&&r.pathname==="/";if(d){const r=detectLocale({pathname:e,hostname:t,cookieHeader:a,acceptLanguageHeader:s,i18n:l});u=r.locale;if(u!==c){const a=detectDomainLocale(l.domains,undefined,u);if(a&&a.domain!==t){const t=a.http?"http":"https";const s=u===a.defaultLocale?"":`/${u}`;const r=new URL(`${t}://${a.domain}${n}${s}${e}${f.search}`);g={url:r,status:307}}if(!a||a&&a.domain===t){const t=new URL(f.toString());t.pathname=`${n}/${u}${e}`;g={url:t,status:307}}}}if(!o&&!g){const t=u||i?.defaultLocale||l.defaultLocale;f.pathname=`${n}/${t}${e}`}}}const y=processRoutes(o.beforeMiddleware,f,h,m,R,d);if(y.status){p=y.status}if(y.redirect){return{redirect:y.redirect,resolvedHeaders:m,status:p}}if(y.externalRewrite){return{externalRewrite:y.externalRewrite,resolvedHeaders:m,status:p}}if(y.stopped){w={status:p}}f=y.url;let v=f;if(L&&u){v=denormalizeNextDataUrl(f,n,c)}const x=shouldInvokeMiddlewareForRequest(o.middlewareMatchers,f,h,d);if(x){const e=await i({url:v,headers:h,requestBody:a});if(e.bodySent){return{middlewareResponded:true}}if(e.requestHeaders){h=new Headers(e.requestHeaders)}if(e.responseHeaders){e.responseHeaders.forEach(((e,t)=>{if(t.toLowerCase()==="set-cookie"){m.append(t,e)}else{m.set(t,e)}}))}if(e.redirect){if(!m.has("location")){m.set("Location",e.redirect.url.toString())}return{resolvedHeaders:m,status:e.redirect.status}}if(e.rewrite){f=e.rewrite;if(f.origin!==R){return{externalRewrite:f,resolvedHeaders:m,status:p}}}}if(g){if(!m.has("location")){m.set("location",g.url.toString())}return{redirect:g,resolvedHeaders:m}}if(w){return{resolvedHeaders:m,status:w.status}}if(L&&u){f=normalizeNextDataUrl(f,n,c)}const H=processRoutes(o.beforeFiles,f,h,m,R,d);if(H.status){p=H.status}if(H.redirect){return{redirect:H.redirect,resolvedHeaders:m,status:p}}if(H.externalRewrite){return{externalRewrite:H.externalRewrite,resolvedHeaders:m,status:p}}if(H.stopped){return{resolvedHeaders:m,status:p}}f=H.url;if(L&&u){f=denormalizeNextDataUrl(f,n,c)}let D=matchesPathname(f.pathname,r);if(D){for(const e of o.dynamicRoutes){const t=matchDynamicRoute(f.pathname,e,d);if(!t.matched){continue}const a=checkHasConditions(e.has,f,h);const s=checkMissingConditions(e.missing,f,h);if(!a.matched||!s){continue}const i=e.destination?replaceDestination(e.destination,t.regexMatches||null,a.captures):undefined;const c=i?i.split("?")[0]:f.pathname;const u=matchesPathnameWithLocaleFallback({pathname:c,pathnames:r,basePath:n,i18n:l});if(!u){if(isDynamicTemplatePathname(c)){continue}const e=i?mergeDestinationQueryIntoUrl(f,i):f;const n=applyOnMatchHeaders(o.onMatch,e,h,m,d);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:n,status:p},url:e,resolvedPathname:D,invocationPathname:f.pathname})}const g=u===D||isDynamicTemplatePathname(D);if(!g){continue}const w=i?mergeDestinationQueryIntoUrl(f,i):f;const R=applyOnMatchHeaders(o.onMatch,w,h,m,d);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:R,status:p},url:w,resolvedPathname:u,invocationPathname:f.pathname})}const e=applyOnMatchHeaders(o.onMatch,f,h,m,d);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:p},url:f,resolvedPathname:D,invocationPathname:f.pathname})}if(L&&u){f=normalizeNextDataUrl(f,n,c)}for(const e of o.afterFiles){const t=matchRoute(e,f,h,d);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){m.set(e,n)}}if(e.status){p=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(f,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:m,status:p}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:m,status:p}}f=applyDestination(f,t.destination);if(f.origin!==R){return{externalRewrite:f,resolvedHeaders:m,status:p}}const a=checkDynamicRoutes(o.dynamicRoutes,f,r,h,m,o.onMatch,n,c,l,u,L,d);if(a.matched&&a.result){if(a.resetUrl){f=a.resetUrl}return{...a.result,status:p}}let s=f;if(L&&u){s=denormalizeNextDataUrl(f,n,c)}D=matchesPathname(s.pathname,r);if(D){const e=applyOnMatchHeaders(o.onMatch,s,h,m,d);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:p},url:s,resolvedPathname:D,invocationPathname:s.pathname})}}}}for(const e of o.dynamicRoutes){const t=matchDynamicRoute(f.pathname,e,d);if(t.matched){const a=checkHasConditions(e.has,f,h);const s=checkMissingConditions(e.missing,f,h);if(a.matched&&s){const s=e.destination?replaceDestination(e.destination,t.regexMatches||null,a.captures):undefined;const i=s?s.split("?")[0]:f.pathname;D=matchesPathnameWithLocaleFallback({pathname:i,pathnames:r,basePath:n,i18n:l});if(D){const e=s?mergeDestinationQueryIntoUrl(f,s):f;const n=applyOnMatchHeaders(o.onMatch,e,h,m,d);return withResolvedInvocationTarget({result:{routeMatches:t.params,resolvedHeaders:n,status:p},url:e,resolvedPathname:D,invocationPathname:f.pathname})}}}}for(const e of o.fallback){const t=matchRoute(e,f,h,d);if(t.matched){if(t.headers){for(const[e,n]of Object.entries(t.headers)){m.set(e,n)}}if(e.status){p=e.status}if(t.destination){if(isRedirectStatus(e.status)&&t.headers&&hasRedirectHeaders(t.headers)){const n=isExternalDestination(t.destination)?new URL(t.destination):applyDestination(f,t.destination);return{redirect:{url:n,status:e.status},resolvedHeaders:m,status:p}}if(isExternalDestination(t.destination)){return{externalRewrite:new URL(t.destination),resolvedHeaders:m,status:p}}f=applyDestination(f,t.destination);if(f.origin!==R){return{externalRewrite:f,resolvedHeaders:m,status:p}}const a=checkDynamicRoutes(o.dynamicRoutes,f,r,h,m,o.onMatch,n,c,l,u,L,d);if(a.matched&&a.result){if(a.resetUrl){f=a.resetUrl}return{...a.result,status:p}}let s=f;if(L&&u){s=denormalizeNextDataUrl(f,n,c)}D=matchesPathname(s.pathname,r);if(D){const e=applyOnMatchHeaders(o.onMatch,s,h,m,d);return withResolvedInvocationTarget({result:{resolvedHeaders:e,status:p},url:s,resolvedPathname:D,invocationPathname:s.pathname})}}}}return{resolvedHeaders:m,status:p}}function responseToMiddlewareResult(e,t,a){const s={};const r={};e.headers.forEach(((e,t)=>{if(r[t]){const n=r[t];if(Array.isArray(n)){n.push(e)}else{r[t]=[n,e]}}else{r[t]=e}}));if(r["x-middleware-override-headers"]){const e=new Set;let n=r["x-middleware-override-headers"];if(typeof n==="string"){n=n.split(",")}for(const t of n){e.add(t.trim())}delete r["x-middleware-override-headers"];const a=[];t.forEach(((t,n)=>{if(!e.has(n)){a.push(n)}}));for(const e of a){t.delete(e)}for(const n of e.keys()){const e="x-middleware-request-"+n;const a=r[e];if(a===undefined||a===null){t.delete(n)}else if(Array.isArray(a)){t.set(n,a[0]);for(let e=1;e<a.length;e++){t.append(n,a[e])}}else{t.set(n,a)}delete r[e]}}if(!r["x-middleware-rewrite"]&&!r["x-middleware-next"]&&!r["location"]){r["x-middleware-refresh"]="1"}delete r["x-middleware-next"];const o=new Headers;for(const[e,n]of Object.entries(r)){if(["content-length","x-middleware-rewrite","x-middleware-redirect","x-middleware-refresh"].includes(e)){continue}if(e==="x-middleware-set-cookie"){if(n!==undefined){if(Array.isArray(n)){for(const a of n){t.append(e,a)}}else{t.set(e,n)}}continue}if(n!==undefined){if(Array.isArray(n)){for(const a of n){o.append(e,a);t.append(e,a)}}else{o.set(e,n);t.set(e,n)}}}s.responseHeaders=o;s.requestHeaders=t;if(r["x-middleware-rewrite"]){const e=r["x-middleware-rewrite"];const t=getRelativeURL(e,a.toString());o.set("x-middleware-rewrite",t);try{const e=new URL(t,a);if(e.origin!==a.origin){s.rewrite=e;return s}s.rewrite=e}catch{s.rewrite=new URL(t,a)}}if(r["location"]){const t=r["location"];const i=n.has(e.status);if(i){const n=getRelativeURL(t,a.toString());o.set("location",n);try{const t=new URL(n,a);s.redirect={url:t,status:e.status};return s}catch{s.redirect={url:new URL(n,a),status:e.status};return s}}else{o.set("location",t);return s}}if(r["x-middleware-refresh"]){s.bodySent=true;return s}return s}function getRelativeURL(e,t){try{const n=new URL(e,t);const a=new URL(t);if(n.origin===a.origin){return n.pathname+n.search+n.hash}return n.toString()}catch{return e}}const n=new Set([301,302,303,307,308]);module.exports=t})();
package/dist/types.d.ts CHANGED
@@ -49,7 +49,17 @@ export type ResolveRoutesParams = {
49
49
  locales: string[];
50
50
  };
51
51
  routes: {
52
+ /**
53
+ * When false (default), route matching is case-insensitive to mirror
54
+ * Next.js default behavior. When true, matches are case-sensitive.
55
+ */
56
+ caseSensitive?: boolean;
52
57
  beforeMiddleware: Array<Route>;
58
+ /**
59
+ * Middleware matcher definitions used to decide whether middleware should
60
+ * be invoked for the current request.
61
+ */
62
+ middlewareMatchers?: Array<Route>;
53
63
  beforeFiles: Array<Route>;
54
64
  afterFiles: Array<Route>;
55
65
  dynamicRoutes: Array<Route>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next/routing",
3
- "version": "16.2.4",
3
+ "version": "16.3.0-canary.1",
4
4
  "keywords": [
5
5
  "react",
6
6
  "next",