next-intl 3.2.5 → 3.3.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.
@@ -4,10 +4,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var utils = require('./utils.js');
6
6
 
7
- function getAlternateEntry(url, locale) {
8
- return "<".concat(url, ">; rel=\"alternate\"; hreflang=\"").concat(locale, "\"");
9
- }
10
-
11
7
  /**
12
8
  * See https://developers.google.com/search/docs/specialty/international/localized-versions
13
9
  */
@@ -27,6 +23,13 @@ function getAlternateLinksHeaderValue(_ref) {
27
23
  }
28
24
  normalizedUrl.protocol = (_request$headers$get = request.headers.get('x-forwarded-proto')) !== null && _request$headers$get !== void 0 ? _request$headers$get : normalizedUrl.protocol;
29
25
  normalizedUrl.pathname = utils.getNormalizedPathname(normalizedUrl.pathname, config.locales);
26
+ function getAlternateEntry(url, locale) {
27
+ if (request.nextUrl.basePath) {
28
+ url = new URL(url);
29
+ url.pathname = utils.applyBasePath(url.pathname, request.nextUrl.basePath);
30
+ }
31
+ return "<".concat(url.toString(), ">; rel=\"alternate\"; hreflang=\"").concat(locale, "\"");
32
+ }
30
33
  function getLocalizedPathname(pathname, locale) {
31
34
  if (localizedPathnames && typeof localizedPathnames === 'object') {
32
35
  return utils.formatTemplatePathname(pathname, localizedPathnames[resolvedLocale], localizedPathnames[locale]);
@@ -49,11 +52,14 @@ function getAlternateLinksHeaderValue(_ref) {
49
52
  url = new URL(normalizedUrl);
50
53
  url.port = '';
51
54
  url.host = domainConfig.domain;
52
- url.pathname = getLocalizedPathname(url.pathname, locale);
55
+
56
+ // Important: Use `normalizedUrl` here, as `url` potentially uses
57
+ // a `basePath` that automatically gets applied to the pathname
58
+ url.pathname = getLocalizedPathname(normalizedUrl.pathname, locale);
53
59
  if (locale !== domainConfig.defaultLocale || config.localePrefix === 'always') {
54
60
  url.pathname = prefixPathname(url.pathname);
55
61
  }
56
- return getAlternateEntry(url.toString(), locale);
62
+ return getAlternateEntry(url, locale);
57
63
  });
58
64
  } else {
59
65
  let pathname;
@@ -67,13 +73,13 @@ function getAlternateLinksHeaderValue(_ref) {
67
73
  }
68
74
  url = new URL(pathname, normalizedUrl);
69
75
  }
70
- return getAlternateEntry(url.toString(), locale);
76
+ return getAlternateEntry(url, locale);
71
77
  });
72
78
 
73
79
  // Add x-default entry
74
80
  if (!config.domains) {
75
81
  const url = new URL(getLocalizedPathname(normalizedUrl.pathname, config.defaultLocale), normalizedUrl);
76
- links.push(getAlternateEntry(url.toString(), 'x-default'));
82
+ links.push(getAlternateEntry(url, 'x-default'));
77
83
  }
78
84
  return links.join(', ');
79
85
  }
@@ -42,7 +42,11 @@ function createMiddleware(config) {
42
42
  };
43
43
  }
44
44
  function rewrite(url) {
45
- return server.NextResponse.rewrite(new URL(url, request.url), getResponseInit());
45
+ const urlObj = new URL(url, request.url);
46
+ if (request.nextUrl.basePath) {
47
+ urlObj.pathname = utils.applyBasePath(urlObj.pathname, request.nextUrl.basePath);
48
+ }
49
+ return server.NextResponse.rewrite(urlObj, getResponseInit());
46
50
  }
47
51
  function redirect(url, redirectDomain) {
48
52
  const urlObj = new URL(url, request.url);
@@ -63,6 +67,9 @@ function createMiddleware(config) {
63
67
  urlObj.port = '';
64
68
  urlObj.host = redirectDomain;
65
69
  }
70
+ if (request.nextUrl.basePath) {
71
+ urlObj.pathname = utils.applyBasePath(urlObj.pathname, request.nextUrl.basePath);
72
+ }
66
73
  return server.NextResponse.redirect(urlObj.toString());
67
74
  }
68
75
  const normalizedPathname = utils.getNormalizedPathname(request.nextUrl.pathname, configWithDefaults.locales);
@@ -96,17 +103,17 @@ function createMiddleware(config) {
96
103
  } else {
97
104
  const internalPathWithSearch = utils.getPathWithSearch(pathname, request.nextUrl.search);
98
105
  if (hasLocalePrefix) {
99
- const basePath = utils.getNormalizedPathname(utils.getPathWithSearch(normalizedPathname, request.nextUrl.search), configWithDefaults.locales);
106
+ const normalizedPathnameWithSearch = utils.getPathWithSearch(normalizedPathname, request.nextUrl.search);
100
107
  if (configWithDefaults.localePrefix === 'never') {
101
- response = redirect(basePath);
108
+ response = redirect(normalizedPathnameWithSearch);
102
109
  } else if (pathLocale === locale) {
103
110
  if (hasMatchedDefaultLocale && configWithDefaults.localePrefix === 'as-needed') {
104
- response = redirect(basePath);
111
+ response = redirect(normalizedPathnameWithSearch);
105
112
  } else {
106
113
  if (configWithDefaults.domains) {
107
114
  const pathDomain = utils.getBestMatchingDomain(domain, pathLocale, domainConfigs);
108
115
  if ((domain === null || domain === void 0 ? void 0 : domain.domain) !== (pathDomain === null || pathDomain === void 0 ? void 0 : pathDomain.domain) && !hasUnknownHost) {
109
- response = redirect(basePath, pathDomain === null || pathDomain === void 0 ? void 0 : pathDomain.domain);
116
+ response = redirect(normalizedPathnameWithSearch, pathDomain === null || pathDomain === void 0 ? void 0 : pathDomain.domain);
110
117
  } else {
111
118
  response = rewrite(internalPathWithSearch);
112
119
  }
@@ -115,7 +122,7 @@ function createMiddleware(config) {
115
122
  }
116
123
  }
117
124
  } else {
118
- response = redirect("/".concat(locale).concat(basePath));
125
+ response = redirect("/".concat(locale).concat(normalizedPathnameWithSearch));
119
126
  }
120
127
  } else {
121
128
  if (configWithDefaults.localePrefix === 'never' || hasMatchedDefaultLocale && (configWithDefaults.localePrefix === 'as-needed' || configWithDefaults.domains)) {
@@ -128,6 +135,7 @@ function createMiddleware(config) {
128
135
  }
129
136
  if (hasOutdatedCookie) {
130
137
  response.cookies.set(constants.COOKIE_LOCALE_NAME, locale, {
138
+ path: request.nextUrl.basePath || undefined,
131
139
  sameSite: 'strict',
132
140
  maxAge: 31536000 // 1 year
133
141
  });
@@ -31,9 +31,7 @@ function formatTemplatePathname(sourcePathname, sourceTemplate, targetTemplate,
31
31
  targetPathname = "/".concat(localePrefix);
32
32
  }
33
33
  targetPathname += formatPathname(targetTemplate, params);
34
- if (targetPathname.endsWith('/')) {
35
- targetPathname = targetPathname.slice(0, -1);
36
- }
34
+ targetPathname = normalizeTrailingSlash(targetPathname);
37
35
  return targetPathname;
38
36
  }
39
37
 
@@ -48,10 +46,8 @@ function getNormalizedPathname(pathname, locales) {
48
46
  }
49
47
  const match = pathname.match("^/(".concat(locales.join('|'), ")/(.*)"));
50
48
  let result = match ? '/' + match[2] : pathname;
51
-
52
- // Remove trailing slash
53
- if (result.endsWith('/') && result !== '/') {
54
- result = result.slice(0, -1);
49
+ if (result !== '/') {
50
+ result = normalizeTrailingSlash(result);
55
51
  }
56
52
  return result;
57
53
  }
@@ -128,7 +124,17 @@ function getBestMatchingDomain(curHostDomain, locale, domainConfigs) {
128
124
  }
129
125
  return domainConfig;
130
126
  }
127
+ function applyBasePath(pathname, basePath) {
128
+ return normalizeTrailingSlash(basePath + pathname);
129
+ }
130
+ function normalizeTrailingSlash(pathname) {
131
+ if (pathname.endsWith('/')) {
132
+ pathname = pathname.slice(0, -1);
133
+ }
134
+ return pathname;
135
+ }
131
136
 
137
+ exports.applyBasePath = applyBasePath;
132
138
  exports.formatPathname = formatPathname;
133
139
  exports.formatTemplatePathname = formatTemplatePathname;
134
140
  exports.getBestMatchingDomain = getBestMatchingDomain;
@@ -1 +1 @@
1
- import{getHost as t,getNormalizedPathname as e,isLocaleSupportedOnDomain as a,formatTemplatePathname as o}from"./utils.js";function n(t,e){return"<".concat(t,'>; rel="alternate"; hreflang="').concat(e,'"')}function l(l){var r;let{config:c,localizedPathnames:i,request:f,resolvedLocale:p}=l;const s=f.nextUrl.clone(),u=t(f.headers);function m(t,e){return i&&"object"==typeof i?o(t,i[p],i[e]):t}u&&(s.port="",s.host=u),s.protocol=null!==(r=f.headers.get("x-forwarded-proto"))&&void 0!==r?r:s.protocol,s.pathname=e(s.pathname,c.locales);const d=c.locales.flatMap((t=>{function e(e){return"/"===e?"/".concat(t):"/".concat(t).concat(e)}let o;if(c.domains){return(c.domains.filter((e=>a(t,e)))||[]).map((a=>(o=new URL(s),o.port="",o.host=a.domain,o.pathname=m(o.pathname,t),t===a.defaultLocale&&"always"!==c.localePrefix||(o.pathname=e(o.pathname)),n(o.toString(),t))))}{let a;a=i&&"object"==typeof i?m(s.pathname,t):s.pathname,t===c.defaultLocale&&"always"!==c.localePrefix||(a=e(a)),o=new URL(a,s)}return n(o.toString(),t)}));if(!c.domains){const t=new URL(m(s.pathname,c.defaultLocale),s);d.push(n(t.toString(),"x-default"))}return d.join(", ")}export{l as default};
1
+ import{getHost as e,getNormalizedPathname as a,isLocaleSupportedOnDomain as t,applyBasePath as n,formatTemplatePathname as o}from"./utils.js";function l(l){var r;let{config:c,localizedPathnames:p,request:s,resolvedLocale:f}=l;const h=s.nextUrl.clone(),i=e(s.headers);function m(e,a){return s.nextUrl.basePath&&((e=new URL(e)).pathname=n(e.pathname,s.nextUrl.basePath)),"<".concat(e.toString(),'>; rel="alternate"; hreflang="').concat(a,'"')}function u(e,a){return p&&"object"==typeof p?o(e,p[f],p[a]):e}i&&(h.port="",h.host=i),h.protocol=null!==(r=s.headers.get("x-forwarded-proto"))&&void 0!==r?r:h.protocol,h.pathname=a(h.pathname,c.locales);const d=c.locales.flatMap((e=>{function a(a){return"/"===a?"/".concat(e):"/".concat(e).concat(a)}let n;if(c.domains){return(c.domains.filter((a=>t(e,a)))||[]).map((t=>(n=new URL(h),n.port="",n.host=t.domain,n.pathname=u(h.pathname,e),e===t.defaultLocale&&"always"!==c.localePrefix||(n.pathname=a(n.pathname)),m(n,e))))}{let t;t=p&&"object"==typeof p?u(h.pathname,e):h.pathname,e===c.defaultLocale&&"always"!==c.localePrefix||(t=a(t)),n=new URL(t,h)}return m(n,e)}));if(!c.domains){const e=new URL(u(h.pathname,c.defaultLocale),h);d.push(m(e,"x-default"))}return d.join(", ")}export{l as default};
@@ -1 +1 @@
1
- import{NextResponse as e}from"next/server";import{COOKIE_LOCALE_NAME as o,HEADER_LOCALE_NAME as l}from"../shared/constants.js";import{matchesPathname as t}from"../shared/utils.js";import n from"./getAlternateLinksHeaderValue.js";import a from"./resolveLocale.js";import{isLocaleSupportedOnDomain as r,getNormalizedPathname as i,getKnownLocaleFromPathname as s,getInternalTemplate as c,formatTemplatePathname as d,getPathWithSearch as f,getBestMatchingDomain as u}from"./utils.js";function m(m){const v=function(e){var o,l,t;return{...e,alternateLinks:null===(o=e.alternateLinks)||void 0===o||o,localePrefix:null!==(l=e.localePrefix)&&void 0!==l?l:"always",localeDetection:null===(t=e.localeDetection)||void 0===t||t}}(m);return function(m){var h,p;const{domain:x,locale:L}=a(v,m.headers,m.cookies,m.nextUrl.pathname),P=v.localeDetection&&(null===(h=m.cookies.get(o))||void 0===h?void 0:h.value)!==L,U=x?x.defaultLocale===L:L===v.defaultLocale,g=(null===(p=v.domains)||void 0===p?void 0:p.filter((e=>r(L,e))))||[],k=null!=v.domains&&!x;function w(o){return e.rewrite(new URL(o,m.url),function(){const e=new Headers(m.headers);return e.set(l,L),{request:{headers:e}}}())}function j(o,l){const t=new URL(o,m.url);if(g.length>0&&!l){const e=u(x,L,g);e&&(l=e.domain,e.defaultLocale===L&&"as-needed"===v.localePrefix&&t.pathname.startsWith("/".concat(L))&&(t.pathname=i(t.pathname,v.locales)))}var n;l&&(t.protocol=null!==(n=m.headers.get("x-forwarded-proto"))&&void 0!==n?n:m.nextUrl.protocol,t.port="",t.host=l);return e.redirect(t.toString())}const y=i(m.nextUrl.pathname,v.locales),D=s(m.nextUrl.pathname,v.locales),q=null!=D;let A,H,R=m.nextUrl.pathname;if(v.pathnames){let e;if([e=L,H]=c(v.pathnames,y),H){const o=v.pathnames[H],l="string"==typeof o?o:o[L];if(t(l,y))R=d(y,l,H,D);else{const t=v.defaultLocale===L||(null==x?void 0:x.defaultLocale)===L;A=j(f(d(y,"string"==typeof o?o:o[e],l,D||!t?L:void 0),m.nextUrl.search))}}}if(!A)if("/"===R){const e=f("/".concat(L),m.nextUrl.search);A="never"===v.localePrefix||U&&"as-needed"===v.localePrefix?w(e):j(e)}else{const e=f(R,m.nextUrl.search);if(q){const o=i(f(y,m.nextUrl.search),v.locales);if("never"===v.localePrefix)A=j(o);else if(D===L)if(U&&"as-needed"===v.localePrefix)A=j(o);else if(v.domains){const l=u(x,D,g);A=(null==x?void 0:x.domain)===(null==l?void 0:l.domain)||k?w(e):j(o,null==l?void 0:l.domain)}else A=w(e);else A=j("/".concat(L).concat(o))}else A="never"===v.localePrefix||U&&("as-needed"===v.localePrefix||v.domains)?w("/".concat(L).concat(e)):j("/".concat(L).concat(e))}var S;(P&&A.cookies.set(o,L,{sameSite:"strict",maxAge:31536e3}),"never"!==v.localePrefix&&v.alternateLinks&&v.locales.length>1)&&A.headers.set("Link",n({config:v,localizedPathnames:null!=H?null===(S=v.pathnames)||void 0===S?void 0:S[H]:void 0,request:m,resolvedLocale:L}));return A}}export{m as default};
1
+ import{NextResponse as e}from"next/server";import{COOKIE_LOCALE_NAME as t,HEADER_LOCALE_NAME as a}from"../shared/constants.js";import{matchesPathname as n}from"../shared/utils.js";import l from"./getAlternateLinksHeaderValue.js";import o from"./resolveLocale.js";import{isLocaleSupportedOnDomain as r,getNormalizedPathname as s,getKnownLocaleFromPathname as i,getInternalTemplate as c,formatTemplatePathname as d,getPathWithSearch as f,getBestMatchingDomain as u,applyBasePath as m}from"./utils.js";function h(h){const p=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}}(h);return function(h){var v,x;const{domain:P,locale:U}=o(p,h.headers,h.cookies,h.nextUrl.pathname),L=p.localeDetection&&(null===(v=h.cookies.get(t))||void 0===v?void 0:v.value)!==U,g=P?P.defaultLocale===U:U===p.defaultLocale,k=(null===(x=p.domains)||void 0===x?void 0:x.filter((e=>r(U,e))))||[],w=null!=p.domains&&!P;function b(t){const n=new URL(t,h.url);return h.nextUrl.basePath&&(n.pathname=m(n.pathname,h.nextUrl.basePath)),e.rewrite(n,function(){const e=new Headers(h.headers);return e.set(a,U),{request:{headers:e}}}())}function j(t,a){const n=new URL(t,h.url);if(k.length>0&&!a){const e=u(P,U,k);e&&(a=e.domain,e.defaultLocale===U&&"as-needed"===p.localePrefix&&n.pathname.startsWith("/".concat(U))&&(n.pathname=s(n.pathname,p.locales)))}var l;a&&(n.protocol=null!==(l=h.headers.get("x-forwarded-proto"))&&void 0!==l?l:h.nextUrl.protocol,n.port="",n.host=a);return h.nextUrl.basePath&&(n.pathname=m(n.pathname,h.nextUrl.basePath)),e.redirect(n.toString())}const y=s(h.nextUrl.pathname,p.locales),D=i(h.nextUrl.pathname,p.locales),q=null!=D;let A,H,R=h.nextUrl.pathname;if(p.pathnames){let e;if([e=U,H]=c(p.pathnames,y),H){const t=p.pathnames[H],a="string"==typeof t?t:t[U];if(n(a,y))R=d(y,a,H,D);else{const n=p.defaultLocale===U||(null==P?void 0:P.defaultLocale)===U;A=j(f(d(y,"string"==typeof t?t:t[e],a,D||!n?U:void 0),h.nextUrl.search))}}}if(!A)if("/"===R){const e=f("/".concat(U),h.nextUrl.search);A="never"===p.localePrefix||g&&"as-needed"===p.localePrefix?b(e):j(e)}else{const e=f(R,h.nextUrl.search);if(q){const t=f(y,h.nextUrl.search);if("never"===p.localePrefix)A=j(t);else if(D===U)if(g&&"as-needed"===p.localePrefix)A=j(t);else if(p.domains){const a=u(P,D,k);A=(null==P?void 0:P.domain)===(null==a?void 0:a.domain)||w?b(e):j(t,null==a?void 0:a.domain)}else A=b(e);else A=j("/".concat(U).concat(t))}else A="never"===p.localePrefix||g&&("as-needed"===p.localePrefix||p.domains)?b("/".concat(U).concat(e)):j("/".concat(U).concat(e))}var S;(L&&A.cookies.set(t,U,{path:h.nextUrl.basePath||void 0,sameSite:"strict",maxAge:31536e3}),"never"!==p.localePrefix&&p.alternateLinks&&p.locales.length>1)&&A.headers.set("Link",l({config:p,localizedPathnames:null!=H?null===(S=p.pathnames)||void 0===S?void 0:S[H]:void 0,request:h,resolvedLocale:U}));return A}}export{h as default};
@@ -1 +1 @@
1
- import{matchesPathname as n,templateToRegex as e}from"../shared/utils.js";function t(n){return n.split("/")[1]}function o(e,t){for(const[o,l]of Object.entries(e))if("string"==typeof l){if(n(l,t))return[void 0,o]}else for(const[e,r]of Object.entries(l))if(n(r,t))return[e,o];return[void 0,void 0]}function l(n,e,t,o){const l=i(e,n);let r="";return o&&(r="/".concat(o)),r+=u(t,l),r.endsWith("/")&&(r=r.slice(0,-1)),r}function r(n,e){n.endsWith("/")||(n+="/");const t=n.match("^/(".concat(e.join("|"),")/(.*)"));let o=t?"/"+t[2]:n;return o.endsWith("/")&&"/"!==o&&(o=o.slice(0,-1)),o}function c(n,e){const o=t(n);return e.includes(o)?o:void 0}function i(n,t){const o=e(n).exec(t);if(!o)return;const l={};for(let e=1;e<o.length;e++){var r;const t=null===(r=n.match(/\[([^\]]+)\]/g))||void 0===r?void 0:r[e-1].replace(/[[\]]/g,"");t&&(l[t]=o[e])}return l}function u(n,e){if(!e)return n;let t=n=n.replaceAll("[[","[").replaceAll("]]","]");return Object.entries(e).forEach((n=>{let[e,o]=n;t=t.replace("[".concat(e,"]"),o)})),t}function s(n,e){let t=n;return e&&(t+=e),t}function f(n){var e,t;return null!==(e=null!==(t=n.get("x-forwarded-host"))&&void 0!==t?t:n.get("host"))&&void 0!==e?e:void 0}function a(n,e){return e.defaultLocale===n||!e.locales||e.locales.includes(n)}function d(n,e,t){let o;return n&&a(e,n)&&(o=n),o||(o=t.find((n=>n.defaultLocale===e))),o||(o=t.find((n=>null!=n.locales&&n.locales.includes(e)))),o||null!=(null==n?void 0:n.locales)||(o=n),o||(o=t.find((n=>!n.locales))),o}export{u as formatPathname,l as formatTemplatePathname,d as getBestMatchingDomain,f as getHost,o as getInternalTemplate,c as getKnownLocaleFromPathname,t as getLocaleFromPathname,r as getNormalizedPathname,s as getPathWithSearch,i as getRouteParams,a 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){for(const[o,r]of Object.entries(t))if("string"==typeof r){if(n(r,e))return[void 0,o]}else for(const[t,l]of Object.entries(r))if(n(l,e))return[t,o];return[void 0,void 0]}function r(n,t,e,o){const r=i(t,n);let l="";return o&&(l="/".concat(o)),l+=u(e,r),l=h(l),l}function l(n,t){n.endsWith("/")||(n+="/");const e=n.match("^/(".concat(t.join("|"),")/(.*)"));let o=e?"/"+e[2]:n;return"/"!==o&&(o=h(o)),o}function c(n,t){const o=e(n);return t.includes(o)?o:void 0}function i(n,e){const o=t(n).exec(e);if(!o)return;const r={};for(let t=1;t<o.length;t++){var l;const e=null===(l=n.match(/\[([^\]]+)\]/g))||void 0===l?void 0:l[t-1].replace(/[[\]]/g,"");e&&(r[e]=o[t])}return r}function u(n,t){if(!t)return n;let e=n=n.replaceAll("[[","[").replaceAll("]]","]");return Object.entries(t).forEach((n=>{let[t,o]=n;e=e.replace("[".concat(t,"]"),o)})),e}function f(n,t){let e=n;return t&&(e+=t),e}function s(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 a(n,t){return t.defaultLocale===n||!t.locales||t.locales.includes(n)}function d(n,t,e){let o;return n&&a(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 v(n,t){return h(t+n)}function h(n){return n.endsWith("/")&&(n=n.slice(0,-1)),n}export{v as applyBasePath,u as formatPathname,r as formatTemplatePathname,d as getBestMatchingDomain,s as getHost,o as getInternalTemplate,c as getKnownLocaleFromPathname,e as getLocaleFromPathname,l as getNormalizedPathname,f as getPathWithSearch,i as getRouteParams,a as isLocaleSupportedOnDomain};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./utils.js");function t(e,t){return"<".concat(e,'>; rel="alternate"; hreflang="').concat(t,'"')}exports.default=function(a){var o;let{config:n,localizedPathnames:r,request:l,resolvedLocale:c}=a;const i=l.nextUrl.clone(),s=e.getHost(l.headers);function p(t,a){return r&&"object"==typeof r?e.formatTemplatePathname(t,r[c],r[a]):t}s&&(i.port="",i.host=s),i.protocol=null!==(o=l.headers.get("x-forwarded-proto"))&&void 0!==o?o:i.protocol,i.pathname=e.getNormalizedPathname(i.pathname,n.locales);const u=n.locales.flatMap((a=>{function o(e){return"/"===e?"/".concat(a):"/".concat(a).concat(e)}let l;if(n.domains){return(n.domains.filter((t=>e.isLocaleSupportedOnDomain(a,t)))||[]).map((e=>(l=new URL(i),l.port="",l.host=e.domain,l.pathname=p(l.pathname,a),a===e.defaultLocale&&"always"!==n.localePrefix||(l.pathname=o(l.pathname)),t(l.toString(),a))))}{let e;e=r&&"object"==typeof r?p(i.pathname,a):i.pathname,a===n.defaultLocale&&"always"!==n.localePrefix||(e=o(e)),l=new URL(e,i)}return t(l.toString(),a)}));if(!n.domains){const e=new URL(p(i.pathname,n.defaultLocale),i);u.push(t(e.toString(),"x-default"))}return u.join(", ")};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./utils.js");exports.default=function(a){var t;let{config:o,localizedPathnames:n,request:l,resolvedLocale:r}=a;const c=l.nextUrl.clone(),s=e.getHost(l.headers);function p(a,t){return l.nextUrl.basePath&&((a=new URL(a)).pathname=e.applyBasePath(a.pathname,l.nextUrl.basePath)),"<".concat(a.toString(),'>; rel="alternate"; hreflang="').concat(t,'"')}function i(a,t){return n&&"object"==typeof n?e.formatTemplatePathname(a,n[r],n[t]):a}s&&(c.port="",c.host=s),c.protocol=null!==(t=l.headers.get("x-forwarded-proto"))&&void 0!==t?t:c.protocol,c.pathname=e.getNormalizedPathname(c.pathname,o.locales);const u=o.locales.flatMap((a=>{function t(e){return"/"===e?"/".concat(a):"/".concat(a).concat(e)}let l;if(o.domains){return(o.domains.filter((t=>e.isLocaleSupportedOnDomain(a,t)))||[]).map((e=>(l=new URL(c),l.port="",l.host=e.domain,l.pathname=i(c.pathname,a),a===e.defaultLocale&&"always"!==o.localePrefix||(l.pathname=t(l.pathname)),p(l,a))))}{let e;e=n&&"object"==typeof n?i(c.pathname,a):c.pathname,a===o.defaultLocale&&"always"!==o.localePrefix||(e=t(e)),l=new URL(e,c)}return p(l,a)}));if(!o.domains){const e=new URL(i(c.pathname,o.defaultLocale),c);u.push(p(e,"x-default"))}return u.join(", ")};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("next/server"),t=require("../shared/constants.js"),a=require("../shared/utils.js"),n=require("./getAlternateLinksHeaderValue.js"),l=require("./resolveLocale.js"),o=require("./utils.js");exports.default=function(r){const i=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}}(r);return function(r){var s,c;const{domain:d,locale:u}=l.default(i,r.headers,r.cookies,r.nextUrl.pathname),h=i.localeDetection&&(null===(s=r.cookies.get(t.COOKIE_LOCALE_NAME))||void 0===s?void 0:s.value)!==u,f=d?d.defaultLocale===u:u===i.defaultLocale,m=(null===(c=i.domains)||void 0===c?void 0:c.filter((e=>o.isLocaleSupportedOnDomain(u,e))))||[],v=null!=i.domains&&!d;function p(a){return e.NextResponse.rewrite(new URL(a,r.url),function(){const e=new Headers(r.headers);return e.set(t.HEADER_LOCALE_NAME,u),{request:{headers:e}}}())}function x(t,a){const n=new URL(t,r.url);if(m.length>0&&!a){const e=o.getBestMatchingDomain(d,u,m);e&&(a=e.domain,e.defaultLocale===u&&"as-needed"===i.localePrefix&&n.pathname.startsWith("/".concat(u))&&(n.pathname=o.getNormalizedPathname(n.pathname,i.locales)))}var l;a&&(n.protocol=null!==(l=r.headers.get("x-forwarded-proto"))&&void 0!==l?l:r.nextUrl.protocol,n.port="",n.host=a);return e.NextResponse.redirect(n.toString())}const g=o.getNormalizedPathname(r.nextUrl.pathname,i.locales),P=o.getKnownLocaleFromPathname(r.nextUrl.pathname,i.locales),L=null!=P;let U,E,A=r.nextUrl.pathname;if(i.pathnames){let e;if([e=u,E]=o.getInternalTemplate(i.pathnames,g),E){const t=i.pathnames[E],n="string"==typeof t?t:t[u];if(a.matchesPathname(n,g))A=o.formatTemplatePathname(g,n,E,P);else{const a=i.defaultLocale===u||(null==d?void 0:d.defaultLocale)===u;U=x(o.getPathWithSearch(o.formatTemplatePathname(g,"string"==typeof t?t:t[e],n,P||!a?u:void 0),r.nextUrl.search))}}}if(!U)if("/"===A){const e=o.getPathWithSearch("/".concat(u),r.nextUrl.search);U="never"===i.localePrefix||f&&"as-needed"===i.localePrefix?p(e):x(e)}else{const e=o.getPathWithSearch(A,r.nextUrl.search);if(L){const t=o.getNormalizedPathname(o.getPathWithSearch(g,r.nextUrl.search),i.locales);if("never"===i.localePrefix)U=x(t);else if(P===u)if(f&&"as-needed"===i.localePrefix)U=x(t);else if(i.domains){const a=o.getBestMatchingDomain(d,P,m);U=(null==d?void 0:d.domain)===(null==a?void 0:a.domain)||v?p(e):x(t,null==a?void 0:a.domain)}else U=p(e);else U=x("/".concat(u).concat(t))}else U="never"===i.localePrefix||f&&("as-needed"===i.localePrefix||i.domains)?p("/".concat(u).concat(e)):x("/".concat(u).concat(e))}var O;(h&&U.cookies.set(t.COOKIE_LOCALE_NAME,u,{sameSite:"strict",maxAge:31536e3}),"never"!==i.localePrefix&&i.alternateLinks&&i.locales.length>1)&&U.headers.set("Link",n.default({config:i,localizedPathnames:null!=E?null===(O=i.pathnames)||void 0===O?void 0:O[E]:void 0,request:r,resolvedLocale:u}));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{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.getKnownLocaleFromPathname(r.nextUrl.pathname,s.locales),L=null!=g;let U,E,A=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))A=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("/"===A){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(A,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 O;(u&&U.cookies.set(a.COOKIE_LOCALE_NAME,h,{path:r.nextUrl.basePath||void 0,sameSite:"strict",maxAge:31536e3}),"never"!==s.localePrefix&&s.alternateLinks&&s.locales.length>1)&&U.headers.set("Link",n.default({config:s,localizedPathnames:null!=E?null===(O=s.pathnames)||void 0===O?void 0:O[E]:void 0,request:r,resolvedLocale:h}));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(t,n){const o=e.templateToRegex(t).exec(n);if(!o)return;const r={};for(let e=1;e<o.length;e++){var l;const n=null===(l=t.match(/\[([^\]]+)\]/g))||void 0===l?void 0:l[e-1].replace(/[[\]]/g,"");n&&(r[n]=o[e])}return r}function o(e,t){if(!t)return e;let n=e=e.replaceAll("[[","[").replaceAll("]]","]");return Object.entries(t).forEach((e=>{let[t,o]=e;n=n.replace("[".concat(t,"]"),o)})),n}function r(e,t){return t.defaultLocale===e||!t.locales||t.locales.includes(e)}exports.formatPathname=o,exports.formatTemplatePathname=function(e,t,r,l){const a=n(t,e);let c="";return l&&(c="/".concat(l)),c+=o(r,a),c.endsWith("/")&&(c=c.slice(0,-1)),c},exports.getBestMatchingDomain=function(e,t,n){let o;return e&&r(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.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,l]of Object.entries(r))if(e.matchesPathname(l,n))return[t,o];return[void 0,void 0]},exports.getKnownLocaleFromPathname=function(e,n){const o=t(e);return n.includes(o)?o:void 0},exports.getLocaleFromPathname=t,exports.getNormalizedPathname=function(e,t){e.endsWith("/")||(e+="/");const n=e.match("^/(".concat(t.join("|"),")/(.*)"));let o=n?"/"+n[2]:e;return o.endsWith("/")&&"/"!==o&&(o=o.slice(0,-1)),o},exports.getPathWithSearch=function(e,t){let n=e;return t&&(n+=t),n},exports.getRouteParams=n,exports.isLocaleSupportedOnDomain=r;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("../shared/utils.js");function t(e){return e.split("/")[1]}function n(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 o(e,t){if(!t)return e;let n=e=e.replaceAll("[[","[").replaceAll("]]","]");return Object.entries(t).forEach((e=>{let[t,o]=e;n=n.replace("[".concat(t,"]"),o)})),n}function r(e,t){return t.defaultLocale===e||!t.locales||t.locales.includes(e)}function a(e){return e.endsWith("/")&&(e=e.slice(0,-1)),e}exports.applyBasePath=function(e,t){return a(t+e)},exports.formatPathname=o,exports.formatTemplatePathname=function(e,t,r,l){const c=n(t,e);let s="";return l&&(s="/".concat(l)),s+=o(r,c),s=a(s),s},exports.getBestMatchingDomain=function(e,t,n){let o;return e&&r(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.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.getKnownLocaleFromPathname=function(e,n){const o=t(e);return n.includes(o)?o:void 0},exports.getLocaleFromPathname=t,exports.getNormalizedPathname=function(e,t){e.endsWith("/")||(e+="/");const n=e.match("^/(".concat(t.join("|"),")/(.*)"));let o=n?"/"+n[2]:e;return"/"!==o&&(o=a(o)),o},exports.getPathWithSearch=function(e,t){let n=e;return t&&(n+=t),n},exports.getRouteParams=n,exports.isLocaleSupportedOnDomain=r;
@@ -14,3 +14,4 @@ export declare function getPathWithSearch(pathname: string, search: string | und
14
14
  export declare function getHost(requestHeaders: Headers): string | undefined;
15
15
  export declare function isLocaleSupportedOnDomain<Locales extends AllLocales>(locale: string, domain: DomainConfig<Locales>): boolean;
16
16
  export declare function getBestMatchingDomain<Locales extends AllLocales>(curHostDomain: DomainConfig<Locales> | undefined, locale: string, domainConfigs: Array<DomainConfig<Locales>>): DomainConfig<Locales> | undefined;
17
+ export declare function applyBasePath(pathname: string, basePath: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-intl",
3
- "version": "3.2.5",
3
+ "version": "3.3.1",
4
4
  "sideEffects": false,
5
5
  "author": "Jan Amann <jan@amann.work>",
6
6
  "funding": [
@@ -80,7 +80,7 @@
80
80
  "dependencies": {
81
81
  "@formatjs/intl-localematcher": "^0.2.32",
82
82
  "negotiator": "^0.6.3",
83
- "use-intl": "^3.2.5"
83
+ "use-intl": "^3.3.1"
84
84
  },
85
85
  "peerDependencies": {
86
86
  "next": "^10.0.0 || ^11.0.0 || ^12.0.0 || ^13.0.0 || ^14.0.0",
@@ -110,11 +110,11 @@
110
110
  "size-limit": [
111
111
  {
112
112
  "path": "dist/production/index.react-client.js",
113
- "limit": "12.82 KB"
113
+ "limit": "12.841 KB"
114
114
  },
115
115
  {
116
116
  "path": "dist/production/index.react-server.js",
117
- "limit": "13.58 KB"
117
+ "limit": "13.6 KB"
118
118
  },
119
119
  {
120
120
  "path": "dist/production/navigation.react-client.js",
@@ -134,8 +134,8 @@
134
134
  },
135
135
  {
136
136
  "path": "dist/production/middleware.js",
137
- "limit": "5.72 KB"
137
+ "limit": "5.8 KB"
138
138
  }
139
139
  ],
140
- "gitHead": "bbeec3b77313eba54b526a0e6452a2331f6aae06"
140
+ "gitHead": "8cf73957fc86a395a80fbd389664bebbf0e5ea36"
141
141
  }