next-intl 3.9.5 → 3.11.0-beta.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.
@@ -24,10 +24,12 @@ function createMiddleware(config) {
24
24
  const configWithDefaults = receiveConfig(config);
25
25
  return function middleware(request) {
26
26
  var _request$cookies$get, _configWithDefaults$d;
27
+ // Resolve potential foreign symbols (e.g. /ja/%E7%B4%84 → /ja/約))
28
+ const nextPathname = decodeURI(request.nextUrl.pathname);
27
29
  const {
28
30
  domain,
29
31
  locale
30
- } = resolveLocale.default(configWithDefaults, request.headers, request.cookies, request.nextUrl.pathname);
32
+ } = resolveLocale.default(configWithDefaults, request.headers, request.cookies, nextPathname);
31
33
  const hasOutdatedCookie = configWithDefaults.localeDetection && ((_request$cookies$get = request.cookies.get(constants.COOKIE_LOCALE_NAME)) === null || _request$cookies$get === void 0 ? void 0 : _request$cookies$get.value) !== locale;
32
34
  const hasMatchedDefaultLocale = domain ? domain.defaultLocale === locale : locale === configWithDefaults.defaultLocale;
33
35
  const domainConfigs = ((_configWithDefaults$d = configWithDefaults.domains) === null || _configWithDefaults$d === void 0 ? void 0 : _configWithDefaults$d.filter(curDomain => utils.isLocaleSupportedOnDomain(locale, curDomain))) || [];
@@ -72,23 +74,32 @@ function createMiddleware(config) {
72
74
  }
73
75
  return server.NextResponse.redirect(urlObj.toString());
74
76
  }
75
- const normalizedPathname = utils.getNormalizedPathname(request.nextUrl.pathname, configWithDefaults.locales);
76
- const pathLocale = utils.getPathnameLocale(request.nextUrl.pathname, configWithDefaults.locales);
77
+ const normalizedPathname = utils.getNormalizedPathname(nextPathname, configWithDefaults.locales);
78
+ const pathLocale = utils.getPathnameLocale(nextPathname, configWithDefaults.locales);
77
79
  const hasLocalePrefix = pathLocale != null;
78
80
  let response;
79
81
  let internalTemplateName;
80
- let pathname = request.nextUrl.pathname;
82
+ let pathname = nextPathname;
81
83
  if (configWithDefaults.pathnames) {
82
84
  let resolvedTemplateLocale;
83
- [resolvedTemplateLocale = locale, internalTemplateName] = utils.getInternalTemplate(configWithDefaults.pathnames, normalizedPathname);
85
+ [resolvedTemplateLocale, internalTemplateName] = utils.getInternalTemplate(configWithDefaults.pathnames, normalizedPathname, locale);
84
86
  if (internalTemplateName) {
85
87
  const pathnameConfig = configWithDefaults.pathnames[internalTemplateName];
86
88
  const localeTemplate = typeof pathnameConfig === 'string' ? pathnameConfig : pathnameConfig[locale];
87
89
  if (utils$1.matchesPathname(localeTemplate, normalizedPathname)) {
88
90
  pathname = utils.formatTemplatePathname(normalizedPathname, localeTemplate, internalTemplateName, pathLocale);
89
91
  } else {
90
- const isDefaultLocale = configWithDefaults.defaultLocale === locale || (domain === null || domain === void 0 ? void 0 : domain.defaultLocale) === locale;
91
- response = redirect(utils.getPathWithSearch(utils.formatTemplatePathname(normalizedPathname, typeof pathnameConfig === 'string' ? pathnameConfig : pathnameConfig[resolvedTemplateLocale], localeTemplate, pathLocale || !isDefaultLocale ? locale : undefined), request.nextUrl.search));
92
+ let sourceTemplate;
93
+ if (resolvedTemplateLocale) {
94
+ // A localized pathname from another locale has matched
95
+ sourceTemplate = typeof pathnameConfig === 'string' ? pathnameConfig : pathnameConfig[resolvedTemplateLocale];
96
+ } else {
97
+ // An internal pathname has matched that
98
+ // doesn't have a localized pathname
99
+ sourceTemplate = internalTemplateName;
100
+ }
101
+ const localePrefix = (hasLocalePrefix || !hasMatchedDefaultLocale) && configWithDefaults.localePrefix !== 'never' ? locale : undefined;
102
+ response = redirect(utils.getPathWithSearch(utils.formatTemplatePathname(normalizedPathname, sourceTemplate, localeTemplate, localePrefix), request.nextUrl.search));
92
103
  }
93
104
  }
94
105
  }
@@ -7,7 +7,8 @@ var utils = require('../shared/utils.js');
7
7
  function getFirstPathnameSegment(pathname) {
8
8
  return pathname.split('/')[1];
9
9
  }
10
- function getInternalTemplate(pathnames, pathname) {
10
+ function getInternalTemplate(pathnames, pathname, locale) {
11
+ // Try to find a localized pathname that matches
11
12
  for (const [internalPathname, localizedPathnamesOrPathname] of Object.entries(pathnames)) {
12
13
  if (typeof localizedPathnamesOrPathname === 'string') {
13
14
  const localizedPathname = localizedPathnamesOrPathname;
@@ -15,13 +16,33 @@ function getInternalTemplate(pathnames, pathname) {
15
16
  return [undefined, internalPathname];
16
17
  }
17
18
  } else {
18
- for (const [locale, localizedPathname] of Object.entries(localizedPathnamesOrPathname)) {
19
- if (utils.matchesPathname(localizedPathname, pathname)) {
20
- return [locale, internalPathname];
19
+ // Prefer the entry with the current locale in case multiple
20
+ // localized pathnames match the current pathname
21
+ const sortedEntries = Object.entries(localizedPathnamesOrPathname);
22
+ const curLocaleIndex = sortedEntries.findIndex(_ref => {
23
+ let [entryLocale] = _ref;
24
+ return entryLocale === locale;
25
+ });
26
+ if (curLocaleIndex > 0) {
27
+ sortedEntries.unshift(sortedEntries.splice(curLocaleIndex, 1)[0]);
28
+ }
29
+ for (const [entryLocale, entryPathname] of sortedEntries) {
30
+ if (utils.matchesPathname(entryPathname, pathname)) {
31
+ return [entryLocale, internalPathname];
21
32
  }
22
33
  }
23
34
  }
24
35
  }
36
+
37
+ // Try to find an internal pathname that matches (this can be the case
38
+ // if all localized pathnames are different from the internal pathnames).
39
+ for (const internalPathname of Object.keys(pathnames)) {
40
+ if (utils.matchesPathname(internalPathname, pathname)) {
41
+ return [undefined, internalPathname];
42
+ }
43
+ }
44
+
45
+ // No match
25
46
  return [undefined, undefined];
26
47
  }
27
48
  function formatTemplatePathname(sourcePathname, sourceTemplate, targetTemplate, localePrefix) {
@@ -78,8 +99,8 @@ function formatPathname(template, params) {
78
99
  // we can replace the value with simple interpolation
79
100
  template = template.replace(/\[\[/g, '[').replace(/\]\]/g, ']');
80
101
  let result = template;
81
- Object.entries(params).forEach(_ref => {
82
- let [key, value] = _ref;
102
+ Object.entries(params).forEach(_ref2 => {
103
+ let [key, value] = _ref2;
83
104
  result = result.replace("[".concat(key, "]"), value);
84
105
  });
85
106
  return result;
@@ -92,8 +113,8 @@ function getPathWithSearch(pathname, search) {
92
113
  return pathWithSearch;
93
114
  }
94
115
  function getHost(requestHeaders) {
95
- var _ref2, _requestHeaders$get;
96
- return (_ref2 = (_requestHeaders$get = requestHeaders.get('x-forwarded-host')) !== null && _requestHeaders$get !== void 0 ? _requestHeaders$get : requestHeaders.get('host')) !== null && _ref2 !== void 0 ? _ref2 : undefined;
116
+ var _ref3, _requestHeaders$get;
117
+ return (_ref3 = (_requestHeaders$get = requestHeaders.get('x-forwarded-host')) !== null && _requestHeaders$get !== void 0 ? _requestHeaders$get : requestHeaders.get('host')) !== null && _ref3 !== void 0 ? _ref3 : undefined;
97
118
  }
98
119
  function isLocaleSupportedOnDomain(locale, domain) {
99
120
  return domain.defaultLocale === locale || !domain.locales || domain.locales.includes(locale);
@@ -1 +1 @@
1
- import{NextResponse as e}from"next/server";import{COOKIE_LOCALE_NAME as t,COOKIE_SAME_SITE as a,COOKIE_MAX_AGE as n,HEADER_LOCALE_NAME as l}from"../shared/constants.js";import{matchesPathname as o}from"../shared/utils.js";import r from"./getAlternateLinksHeaderValue.js";import s from"./resolveLocale.js";import{isLocaleSupportedOnDomain as i,getNormalizedPathname as c,getPathnameLocale as d,getInternalTemplate as f,formatTemplatePathname as u,getPathWithSearch as m,getBestMatchingDomain as h,applyBasePath as p}from"./utils.js";function v(v){const x=function(e){var t,a,n;return{...e,alternateLinks:null===(t=e.alternateLinks)||void 0===t||t,localePrefix:null!==(a=e.localePrefix)&&void 0!==a?a:"always",localeDetection:null===(n=e.localeDetection)||void 0===n||n}}(v);return function(v){var P,U;const{domain:L,locale:g}=s(x,v.headers,v.cookies,v.nextUrl.pathname),k=x.localeDetection&&(null===(P=v.cookies.get(t))||void 0===P?void 0:P.value)!==g,w=L?L.defaultLocale===g:g===x.defaultLocale,b=(null===(U=x.domains)||void 0===U?void 0:U.filter((e=>i(g,e))))||[],j=null!=x.domains&&!L;function y(t){const a=new URL(t,v.url);return v.nextUrl.basePath&&(a.pathname=p(a.pathname,v.nextUrl.basePath)),e.rewrite(a,function(){const e=new Headers(v.headers);return e.set(l,g),{request:{headers:e}}}())}function D(t,a){const n=new URL(t,v.url);if(b.length>0&&!a){const e=h(L,g,b);e&&(a=e.domain,e.defaultLocale===g&&"as-needed"===x.localePrefix&&n.pathname.startsWith("/".concat(g))&&(n.pathname=c(n.pathname,x.locales)))}var l;a&&(n.protocol=null!==(l=v.headers.get("x-forwarded-proto"))&&void 0!==l?l:v.nextUrl.protocol,n.port="",n.host=a);return v.nextUrl.basePath&&(n.pathname=p(n.pathname,v.nextUrl.basePath)),e.redirect(n.toString())}const q=c(v.nextUrl.pathname,x.locales),A=d(v.nextUrl.pathname,x.locales),H=null!=A;let R,S,z=v.nextUrl.pathname;if(x.pathnames){let e;if([e=g,S]=f(x.pathnames,q),S){const t=x.pathnames[S],a="string"==typeof t?t:t[g];if(o(a,q))z=u(q,a,S,A);else{const n=x.defaultLocale===g||(null==L?void 0:L.defaultLocale)===g;R=D(m(u(q,"string"==typeof t?t:t[e],a,A||!n?g:void 0),v.nextUrl.search))}}}if(!R)if("/"===z){const e=m("/".concat(g),v.nextUrl.search);R="never"===x.localePrefix||w&&"as-needed"===x.localePrefix?y(e):D(e)}else{const e=m(z,v.nextUrl.search);if(H){const t=m(q,v.nextUrl.search);if("never"===x.localePrefix)R=D(t);else if(A===g)if(w&&"as-needed"===x.localePrefix)R=D(t);else if(x.domains){const a=h(L,A,b);R=(null==L?void 0:L.domain)===(null==a?void 0:a.domain)||j?y(e):D(t,null==a?void 0:a.domain)}else R=y(e);else R=D("/".concat(g).concat(t))}else R="never"===x.localePrefix||w&&("as-needed"===x.localePrefix||x.domains)?y("/".concat(g).concat(e)):D("/".concat(g).concat(e))}var V;(k&&R.cookies.set(t,g,{path:v.nextUrl.basePath||void 0,sameSite:a,maxAge:n}),"never"!==x.localePrefix&&x.alternateLinks&&x.locales.length>1)&&R.headers.set("Link",r({config:x,localizedPathnames:null!=S?null===(V=x.pathnames)||void 0===V?void 0:V[S]:void 0,request:v,resolvedLocale:g}));return R}}export{v as default};
1
+ import{NextResponse as e}from"next/server";import{COOKIE_LOCALE_NAME as t,COOKIE_SAME_SITE as a,COOKIE_MAX_AGE as n,HEADER_LOCALE_NAME as o}from"../shared/constants.js";import{matchesPathname as l}from"../shared/utils.js";import r from"./getAlternateLinksHeaderValue.js";import s from"./resolveLocale.js";import{isLocaleSupportedOnDomain as i,getNormalizedPathname as c,getPathnameLocale as d,getInternalTemplate as f,formatTemplatePathname as u,getPathWithSearch as m,getBestMatchingDomain as h,applyBasePath as v}from"./utils.js";function p(p){const x=function(e){var t,a,n;return{...e,alternateLinks:null===(t=e.alternateLinks)||void 0===t||t,localePrefix:null!==(a=e.localePrefix)&&void 0!==a?a:"always",localeDetection:null===(n=e.localeDetection)||void 0===n||n}}(p);return function(p){var P,U;const L=decodeURI(p.nextUrl.pathname),{domain:g,locale:k}=s(x,p.headers,p.cookies,L),w=x.localeDetection&&(null===(P=p.cookies.get(t))||void 0===P?void 0:P.value)!==k,b=g?g.defaultLocale===k:k===x.defaultLocale,j=(null===(U=x.domains)||void 0===U?void 0:U.filter((e=>i(k,e))))||[],y=null!=x.domains&&!g;function D(t){const a=new URL(t,p.url);return p.nextUrl.basePath&&(a.pathname=v(a.pathname,p.nextUrl.basePath)),e.rewrite(a,function(){const e=new Headers(p.headers);return e.set(o,k),{request:{headers:e}}}())}function R(t,a){const n=new URL(t,p.url);if(j.length>0&&!a){const e=h(g,k,j);e&&(a=e.domain,e.defaultLocale===k&&"as-needed"===x.localePrefix&&n.pathname.startsWith("/".concat(k))&&(n.pathname=c(n.pathname,x.locales)))}var o;a&&(n.protocol=null!==(o=p.headers.get("x-forwarded-proto"))&&void 0!==o?o:p.nextUrl.protocol,n.port="",n.host=a);return p.nextUrl.basePath&&(n.pathname=v(n.pathname,p.nextUrl.basePath)),e.redirect(n.toString())}const q=c(L,x.locales),A=d(L,x.locales),H=null!=A;let S,z,I=L;if(x.pathnames){let e;if([e,z]=f(x.pathnames,q,k),z){const t=x.pathnames[z],a="string"==typeof t?t:t[k];if(l(a,q))I=u(q,a,z,A);else{let n;n=e?"string"==typeof t?t:t[e]:z;const o=!H&&b||"never"===x.localePrefix?void 0:k;S=R(m(u(q,n,a,o),p.nextUrl.search))}}}if(!S)if("/"===I){const e=m("/".concat(k),p.nextUrl.search);S="never"===x.localePrefix||b&&"as-needed"===x.localePrefix?D(e):R(e)}else{const e=m(I,p.nextUrl.search);if(H){const t=m(q,p.nextUrl.search);if("never"===x.localePrefix)S=R(t);else if(A===k)if(b&&"as-needed"===x.localePrefix)S=R(t);else if(x.domains){const a=h(g,A,j);S=(null==g?void 0:g.domain)===(null==a?void 0:a.domain)||y?D(e):R(t,null==a?void 0:a.domain)}else S=D(e);else S=R("/".concat(k).concat(t))}else S="never"===x.localePrefix||b&&("as-needed"===x.localePrefix||x.domains)?D("/".concat(k).concat(e)):R("/".concat(k).concat(e))}var V;(w&&S.cookies.set(t,k,{path:p.nextUrl.basePath||void 0,sameSite:a,maxAge:n}),"never"!==x.localePrefix&&x.alternateLinks&&x.locales.length>1)&&S.headers.set("Link",r({config:x,localizedPathnames:null!=z?null===(V=x.pathnames)||void 0===V?void 0:V[z]:void 0,request:p,resolvedLocale:k}));return S}}export{p as default};
@@ -1 +1 @@
1
- import{matchesPathname as n,templateToRegex as t}from"../shared/utils.js";function e(n){return n.split("/")[1]}function o(t,e){for(const[o,r]of Object.entries(t))if("string"==typeof r){if(n(r,e))return[void 0,o]}else for(const[t,c]of Object.entries(r))if(n(c,e))return[t,o];return[void 0,void 0]}function r(n,t,e,o){const r=u(t,n);let c="";return o&&(c="/".concat(o)),c+=f(e,r),c=h(c),c}function c(n,t){n.endsWith("/")||(n+="/");const e=n.match(new RegExp("^/(".concat(t.join("|"),")/(.*)"),"i"));let o=e?"/"+e[2]:n;return"/"!==o&&(o=h(o)),o}function i(n,t){return t.find((t=>t.toLowerCase()===n.toLowerCase()))}function l(n,t){const o=e(n);return i(o,t)?o:void 0}function u(n,e){const o=t(n).exec(e);if(!o)return;const r={};for(let t=1;t<o.length;t++){var c;const e=null===(c=n.match(/\[([^\]]+)\]/g))||void 0===c?void 0:c[t-1].replace(/[[\]]/g,"");e&&(r[e]=o[t])}return r}function f(n,t){if(!t)return n;let e=n=n.replace(/\[\[/g,"[").replace(/\]\]/g,"]");return Object.entries(t).forEach((n=>{let[t,o]=n;e=e.replace("[".concat(t,"]"),o)})),e}function s(n,t){let e=n;return t&&(e+=t),e}function a(n){var t,e;return null!==(t=null!==(e=n.get("x-forwarded-host"))&&void 0!==e?e:n.get("host"))&&void 0!==t?t:void 0}function d(n,t){return t.defaultLocale===n||!t.locales||t.locales.includes(n)}function v(n,t,e){let o;return n&&d(t,n)&&(o=n),o||(o=e.find((n=>n.defaultLocale===t))),o||(o=e.find((n=>null!=n.locales&&n.locales.includes(t)))),o||null!=(null==n?void 0:n.locales)||(o=n),o||(o=e.find((n=>!n.locales))),o}function g(n,t){return h(t+n)}function h(n){return n.endsWith("/")&&(n=n.slice(0,-1)),n}export{g as applyBasePath,i as findCaseInsensitiveLocale,f as formatPathname,r as formatTemplatePathname,v as getBestMatchingDomain,e as getFirstPathnameSegment,a as getHost,o as getInternalTemplate,c as getNormalizedPathname,s as getPathWithSearch,l as getPathnameLocale,u as getRouteParams,d as isLocaleSupportedOnDomain};
1
+ import{matchesPathname as n,templateToRegex as t}from"../shared/utils.js";function e(n){return n.split("/")[1]}function o(t,e,o){for(const[r,c]of Object.entries(t))if("string"==typeof c){if(n(c,e))return[void 0,r]}else{const t=Object.entries(c),i=t.findIndex((n=>{let[t]=n;return t===o}));i>0&&t.unshift(t.splice(i,1)[0]);for(const[o,c]of t)if(n(c,e))return[o,r]}for(const o of Object.keys(t))if(n(o,e))return[void 0,o];return[void 0,void 0]}function r(n,t,e,o){const r=u(t,n);let c="";return o&&(c="/".concat(o)),c+=f(e,r),c=p(c),c}function c(n,t){n.endsWith("/")||(n+="/");const e=n.match(new RegExp("^/(".concat(t.join("|"),")/(.*)"),"i"));let o=e?"/"+e[2]:n;return"/"!==o&&(o=p(o)),o}function i(n,t){return t.find((t=>t.toLowerCase()===n.toLowerCase()))}function l(n,t){const o=e(n);return i(o,t)?o:void 0}function u(n,e){const o=t(n).exec(e);if(!o)return;const r={};for(let t=1;t<o.length;t++){var c;const e=null===(c=n.match(/\[([^\]]+)\]/g))||void 0===c?void 0:c[t-1].replace(/[[\]]/g,"");e&&(r[e]=o[t])}return r}function f(n,t){if(!t)return n;let e=n=n.replace(/\[\[/g,"[").replace(/\]\]/g,"]");return Object.entries(t).forEach((n=>{let[t,o]=n;e=e.replace("[".concat(t,"]"),o)})),e}function s(n,t){let e=n;return t&&(e+=t),e}function a(n){var t,e;return null!==(t=null!==(e=n.get("x-forwarded-host"))&&void 0!==e?e:n.get("host"))&&void 0!==t?t:void 0}function d(n,t){return t.defaultLocale===n||!t.locales||t.locales.includes(n)}function v(n,t,e){let o;return n&&d(t,n)&&(o=n),o||(o=e.find((n=>n.defaultLocale===t))),o||(o=e.find((n=>null!=n.locales&&n.locales.includes(t)))),o||null!=(null==n?void 0:n.locales)||(o=n),o||(o=e.find((n=>!n.locales))),o}function h(n,t){return p(t+n)}function p(n){return n.endsWith("/")&&(n=n.slice(0,-1)),n}export{h as applyBasePath,i as findCaseInsensitiveLocale,f as formatPathname,r as formatTemplatePathname,v as getBestMatchingDomain,e as getFirstPathnameSegment,a as getHost,o as getInternalTemplate,c as getNormalizedPathname,s as getPathWithSearch,l as getPathnameLocale,u as getRouteParams,d as isLocaleSupportedOnDomain};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/server"),a=require("../shared/constants.js"),t=require("../shared/utils.js"),n=require("./getAlternateLinksHeaderValue.js"),l=require("./resolveLocale.js"),o=require("./utils.js");exports.default=function(r){const s=function(e){var a,t,n;return{...e,alternateLinks:null===(a=e.alternateLinks)||void 0===a||a,localePrefix:null!==(t=e.localePrefix)&&void 0!==t?t:"always",localeDetection:null===(n=e.localeDetection)||void 0===n||n}}(r);return function(r){var i,c;const{domain:d,locale:h}=l.default(s,r.headers,r.cookies,r.nextUrl.pathname),u=s.localeDetection&&(null===(i=r.cookies.get(a.COOKIE_LOCALE_NAME))||void 0===i?void 0:i.value)!==h,m=d?d.defaultLocale===h:h===s.defaultLocale,f=(null===(c=s.domains)||void 0===c?void 0:c.filter((e=>o.isLocaleSupportedOnDomain(h,e))))||[],p=null!=s.domains&&!d;function v(t){const n=new URL(t,r.url);return r.nextUrl.basePath&&(n.pathname=o.applyBasePath(n.pathname,r.nextUrl.basePath)),e.NextResponse.rewrite(n,function(){const e=new Headers(r.headers);return e.set(a.HEADER_LOCALE_NAME,h),{request:{headers:e}}}())}function x(a,t){const n=new URL(a,r.url);if(f.length>0&&!t){const e=o.getBestMatchingDomain(d,h,f);e&&(t=e.domain,e.defaultLocale===h&&"as-needed"===s.localePrefix&&n.pathname.startsWith("/".concat(h))&&(n.pathname=o.getNormalizedPathname(n.pathname,s.locales)))}var l;t&&(n.protocol=null!==(l=r.headers.get("x-forwarded-proto"))&&void 0!==l?l:r.nextUrl.protocol,n.port="",n.host=t);return r.nextUrl.basePath&&(n.pathname=o.applyBasePath(n.pathname,r.nextUrl.basePath)),e.NextResponse.redirect(n.toString())}const P=o.getNormalizedPathname(r.nextUrl.pathname,s.locales),g=o.getPathnameLocale(r.nextUrl.pathname,s.locales),L=null!=g;let U,E,O=r.nextUrl.pathname;if(s.pathnames){let e;if([e=h,E]=o.getInternalTemplate(s.pathnames,P),E){const a=s.pathnames[E],n="string"==typeof a?a:a[h];if(t.matchesPathname(n,P))O=o.formatTemplatePathname(P,n,E,g);else{const t=s.defaultLocale===h||(null==d?void 0:d.defaultLocale)===h;U=x(o.getPathWithSearch(o.formatTemplatePathname(P,"string"==typeof a?a:a[e],n,g||!t?h:void 0),r.nextUrl.search))}}}if(!U)if("/"===O){const e=o.getPathWithSearch("/".concat(h),r.nextUrl.search);U="never"===s.localePrefix||m&&"as-needed"===s.localePrefix?v(e):x(e)}else{const e=o.getPathWithSearch(O,r.nextUrl.search);if(L){const a=o.getPathWithSearch(P,r.nextUrl.search);if("never"===s.localePrefix)U=x(a);else if(g===h)if(m&&"as-needed"===s.localePrefix)U=x(a);else if(s.domains){const t=o.getBestMatchingDomain(d,g,f);U=(null==d?void 0:d.domain)===(null==t?void 0:t.domain)||p?v(e):x(a,null==t?void 0:t.domain)}else U=v(e);else U=x("/".concat(h).concat(a))}else U="never"===s.localePrefix||m&&("as-needed"===s.localePrefix||s.domains)?v("/".concat(h).concat(e)):x("/".concat(h).concat(e))}var A;(u&&U.cookies.set(a.COOKIE_LOCALE_NAME,h,{path:r.nextUrl.basePath||void 0,sameSite:a.COOKIE_SAME_SITE,maxAge:a.COOKIE_MAX_AGE}),"never"!==s.localePrefix&&s.alternateLinks&&s.locales.length>1)&&U.headers.set("Link",n.default({config:s,localizedPathnames:null!=E?null===(A=s.pathnames)||void 0===A?void 0:A[E]:void 0,request:r,resolvedLocale:h}));return U}};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/server"),a=require("../shared/constants.js"),t=require("../shared/utils.js"),n=require("./getAlternateLinksHeaderValue.js"),l=require("./resolveLocale.js"),o=require("./utils.js");exports.default=function(r){const s=function(e){var a,t,n;return{...e,alternateLinks:null===(a=e.alternateLinks)||void 0===a||a,localePrefix:null!==(t=e.localePrefix)&&void 0!==t?t:"always",localeDetection:null===(n=e.localeDetection)||void 0===n||n}}(r);return function(r){var i,c;const d=decodeURI(r.nextUrl.pathname),{domain:h,locale:u}=l.default(s,r.headers,r.cookies,d),f=s.localeDetection&&(null===(i=r.cookies.get(a.COOKIE_LOCALE_NAME))||void 0===i?void 0:i.value)!==u,m=h?h.defaultLocale===u:u===s.defaultLocale,p=(null===(c=s.domains)||void 0===c?void 0:c.filter((e=>o.isLocaleSupportedOnDomain(u,e))))||[],v=null!=s.domains&&!h;function P(t){const n=new URL(t,r.url);return r.nextUrl.basePath&&(n.pathname=o.applyBasePath(n.pathname,r.nextUrl.basePath)),e.NextResponse.rewrite(n,function(){const e=new Headers(r.headers);return e.set(a.HEADER_LOCALE_NAME,u),{request:{headers:e}}}())}function x(a,t){const n=new URL(a,r.url);if(p.length>0&&!t){const e=o.getBestMatchingDomain(h,u,p);e&&(t=e.domain,e.defaultLocale===u&&"as-needed"===s.localePrefix&&n.pathname.startsWith("/".concat(u))&&(n.pathname=o.getNormalizedPathname(n.pathname,s.locales)))}var l;t&&(n.protocol=null!==(l=r.headers.get("x-forwarded-proto"))&&void 0!==l?l:r.nextUrl.protocol,n.port="",n.host=t);return r.nextUrl.basePath&&(n.pathname=o.applyBasePath(n.pathname,r.nextUrl.basePath)),e.NextResponse.redirect(n.toString())}const g=o.getNormalizedPathname(d,s.locales),L=o.getPathnameLocale(d,s.locales),E=null!=L;let U,O,A=d;if(s.pathnames){let e;if([e,O]=o.getInternalTemplate(s.pathnames,g,u),O){const a=s.pathnames[O],n="string"==typeof a?a:a[u];if(t.matchesPathname(n,g))A=o.formatTemplatePathname(g,n,O,L);else{let t;t=e?"string"==typeof a?a:a[e]:O;const l=!E&&m||"never"===s.localePrefix?void 0:u;U=x(o.getPathWithSearch(o.formatTemplatePathname(g,t,n,l),r.nextUrl.search))}}}if(!U)if("/"===A){const e=o.getPathWithSearch("/".concat(u),r.nextUrl.search);U="never"===s.localePrefix||m&&"as-needed"===s.localePrefix?P(e):x(e)}else{const e=o.getPathWithSearch(A,r.nextUrl.search);if(E){const a=o.getPathWithSearch(g,r.nextUrl.search);if("never"===s.localePrefix)U=x(a);else if(L===u)if(m&&"as-needed"===s.localePrefix)U=x(a);else if(s.domains){const t=o.getBestMatchingDomain(h,L,p);U=(null==h?void 0:h.domain)===(null==t?void 0:t.domain)||v?P(e):x(a,null==t?void 0:t.domain)}else U=P(e);else U=x("/".concat(u).concat(a))}else U="never"===s.localePrefix||m&&("as-needed"===s.localePrefix||s.domains)?P("/".concat(u).concat(e)):x("/".concat(u).concat(e))}var _;(f&&U.cookies.set(a.COOKIE_LOCALE_NAME,u,{path:r.nextUrl.basePath||void 0,sameSite:a.COOKIE_SAME_SITE,maxAge:a.COOKIE_MAX_AGE}),"never"!==s.localePrefix&&s.alternateLinks&&s.locales.length>1)&&U.headers.set("Link",n.default({config:s,localizedPathnames:null!=O?null===(_=s.pathnames)||void 0===_?void 0:_[O]:void 0,request:r,resolvedLocale:u}));return U}};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../shared/utils.js");function t(e){return e.split("/")[1]}function n(e,t){return t.find((t=>t.toLowerCase()===e.toLowerCase()))}function o(t,n){const o=e.templateToRegex(t).exec(n);if(!o)return;const r={};for(let e=1;e<o.length;e++){var a;const n=null===(a=t.match(/\[([^\]]+)\]/g))||void 0===a?void 0:a[e-1].replace(/[[\]]/g,"");n&&(r[n]=o[e])}return r}function r(e,t){if(!t)return e;let n=e=e.replace(/\[\[/g,"[").replace(/\]\]/g,"]");return Object.entries(t).forEach((e=>{let[t,o]=e;n=n.replace("[".concat(t,"]"),o)})),n}function a(e,t){return t.defaultLocale===e||!t.locales||t.locales.includes(e)}function s(e){return e.endsWith("/")&&(e=e.slice(0,-1)),e}exports.applyBasePath=function(e,t){return s(t+e)},exports.findCaseInsensitiveLocale=n,exports.formatPathname=r,exports.formatTemplatePathname=function(e,t,n,a){const c=o(t,e);let i="";return a&&(i="/".concat(a)),i+=r(n,c),i=s(i),i},exports.getBestMatchingDomain=function(e,t,n){let o;return e&&a(t,e)&&(o=e),o||(o=n.find((e=>e.defaultLocale===t))),o||(o=n.find((e=>null!=e.locales&&e.locales.includes(t)))),o||null!=(null==e?void 0:e.locales)||(o=e),o||(o=n.find((e=>!e.locales))),o},exports.getFirstPathnameSegment=t,exports.getHost=function(e){var t,n;return null!==(t=null!==(n=e.get("x-forwarded-host"))&&void 0!==n?n:e.get("host"))&&void 0!==t?t:void 0},exports.getInternalTemplate=function(t,n){for(const[o,r]of Object.entries(t))if("string"==typeof r){const t=r;if(e.matchesPathname(t,n))return[void 0,o]}else for(const[t,a]of Object.entries(r))if(e.matchesPathname(a,n))return[t,o];return[void 0,void 0]},exports.getNormalizedPathname=function(e,t){e.endsWith("/")||(e+="/");const n=e.match(new RegExp("^/(".concat(t.join("|"),")/(.*)"),"i"));let o=n?"/"+n[2]:e;return"/"!==o&&(o=s(o)),o},exports.getPathWithSearch=function(e,t){let n=e;return t&&(n+=t),n},exports.getPathnameLocale=function(e,o){const r=t(e);return n(r,o)?r:void 0},exports.getRouteParams=o,exports.isLocaleSupportedOnDomain=a;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../shared/utils.js");function t(e){return e.split("/")[1]}function n(e,t){return t.find((t=>t.toLowerCase()===e.toLowerCase()))}function o(t,n){const o=e.templateToRegex(t).exec(n);if(!o)return;const r={};for(let e=1;e<o.length;e++){var a;const n=null===(a=t.match(/\[([^\]]+)\]/g))||void 0===a?void 0:a[e-1].replace(/[[\]]/g,"");n&&(r[n]=o[e])}return r}function r(e,t){if(!t)return e;let n=e=e.replace(/\[\[/g,"[").replace(/\]\]/g,"]");return Object.entries(t).forEach((e=>{let[t,o]=e;n=n.replace("[".concat(t,"]"),o)})),n}function a(e,t){return t.defaultLocale===e||!t.locales||t.locales.includes(e)}function s(e){return e.endsWith("/")&&(e=e.slice(0,-1)),e}exports.applyBasePath=function(e,t){return s(t+e)},exports.findCaseInsensitiveLocale=n,exports.formatPathname=r,exports.formatTemplatePathname=function(e,t,n,a){const c=o(t,e);let i="";return a&&(i="/".concat(a)),i+=r(n,c),i=s(i),i},exports.getBestMatchingDomain=function(e,t,n){let o;return e&&a(t,e)&&(o=e),o||(o=n.find((e=>e.defaultLocale===t))),o||(o=n.find((e=>null!=e.locales&&e.locales.includes(t)))),o||null!=(null==e?void 0:e.locales)||(o=e),o||(o=n.find((e=>!e.locales))),o},exports.getFirstPathnameSegment=t,exports.getHost=function(e){var t,n;return null!==(t=null!==(n=e.get("x-forwarded-host"))&&void 0!==n?n:e.get("host"))&&void 0!==t?t:void 0},exports.getInternalTemplate=function(t,n,o){for(const[r,a]of Object.entries(t))if("string"==typeof a){const t=a;if(e.matchesPathname(t,n))return[void 0,r]}else{const t=Object.entries(a),s=t.findIndex((e=>{let[t]=e;return t===o}));s>0&&t.unshift(t.splice(s,1)[0]);for(const[o,a]of t)if(e.matchesPathname(a,n))return[o,r]}for(const o of Object.keys(t))if(e.matchesPathname(o,n))return[void 0,o];return[void 0,void 0]},exports.getNormalizedPathname=function(e,t){e.endsWith("/")||(e+="/");const n=e.match(new RegExp("^/(".concat(t.join("|"),")/(.*)"),"i"));let o=n?"/"+n[2]:e;return"/"!==o&&(o=s(o)),o},exports.getPathWithSearch=function(e,t){let n=e;return t&&(n+=t),n},exports.getPathnameLocale=function(e,o){const r=t(e);return n(r,o)?r:void 0},exports.getRouteParams=o,exports.isLocaleSupportedOnDomain=a;
@@ -1,7 +1,7 @@
1
1
  import { AllLocales } from '../shared/types';
2
2
  import { DomainConfig, MiddlewareConfigWithDefaults } from './NextIntlMiddlewareConfig';
3
3
  export declare function getFirstPathnameSegment(pathname: string): string;
4
- export declare function getInternalTemplate<Locales extends AllLocales, Pathnames extends NonNullable<MiddlewareConfigWithDefaults<Locales>['pathnames']>>(pathnames: Pathnames, pathname: string): [Locales[number] | undefined, keyof Pathnames | undefined];
4
+ export declare function getInternalTemplate<Locales extends AllLocales, Pathnames extends NonNullable<MiddlewareConfigWithDefaults<Locales>['pathnames']>>(pathnames: Pathnames, pathname: string, locale: Locales[number]): [Locales[number] | undefined, keyof Pathnames | undefined];
5
5
  export declare function formatTemplatePathname(sourcePathname: string, sourceTemplate: string, targetTemplate: string, localePrefix?: string): string;
6
6
  /**
7
7
  * Removes potential locales from the pathname.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "3.9.5",
3
+ "version": "3.11.0-beta.1",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "funding": [
@@ -16,16 +16,6 @@
16
16
  "type": "git",
17
17
  "url": "https://github.com/amannn/next-intl"
18
18
  },
19
- "scripts": {
20
- "build": "rm -rf dist && rollup -c",
21
- "test": "TZ=Europe/Berlin vitest",
22
- "lint": "pnpm run lint:source && pnpm run lint:package",
23
- "lint:source": "eslint src test && tsc --noEmit",
24
- "lint:package": "publint && attw --pack",
25
- "prepublishOnly": "CI=true turbo test && turbo lint && turbo build && cp ../../README.md .",
26
- "postpublish": "git checkout . && rm ./README.md",
27
- "size": "size-limit"
28
- },
29
19
  "main": "./dist/index.react-client.js",
30
20
  "module": "./dist/esm/index.react-client.js",
31
21
  "typings": "./dist/types/src/index.react-client.d.ts",
@@ -82,7 +72,7 @@
82
72
  "dependencies": {
83
73
  "@formatjs/intl-localematcher": "^0.2.32",
84
74
  "negotiator": "^0.6.3",
85
- "use-intl": "^3.9.5"
75
+ "use-intl": "^3.10.0"
86
76
  },
87
77
  "peerDependencies": {
88
78
  "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0",
@@ -138,8 +128,15 @@
138
128
  },
139
129
  {
140
130
  "path": "dist/production/middleware.js",
141
- "limit": "5.855 KB"
131
+ "limit": "5.95 KB"
142
132
  }
143
133
  ],
144
- "gitHead": "35069bac25c03cd6fd10c802fcd8627e3eac75dd"
145
- }
134
+ "scripts": {
135
+ "build": "rm -rf dist && rollup -c",
136
+ "test": "TZ=Europe/Berlin vitest",
137
+ "lint": "pnpm run lint:source && pnpm run lint:package",
138
+ "lint:source": "eslint src test && tsc --noEmit",
139
+ "lint:package": "publint && attw --pack",
140
+ "size": "size-limit"
141
+ }
142
+ }