gt-next 11.1.11 → 11.1.13

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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # gt-next
2
2
 
3
+ ## 11.1.13
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`4422032`](https://github.com/generaltranslation/gt/commit/4422032bb5d365aabe095ee1ff103fd5bfdee578), [`4422032`](https://github.com/generaltranslation/gt/commit/4422032bb5d365aabe095ee1ff103fd5bfdee578)]:
8
+ - @generaltranslation/react-core@11.1.13
9
+ - gt-react@11.1.13
10
+
11
+ ## 11.1.12
12
+
13
+ ### Patch Changes
14
+
15
+ - [#2105](https://github.com/generaltranslation/gt/pull/2105) [`ff8a52e`](https://github.com/generaltranslation/gt/commit/ff8a52e4e318aab9ca3f8a6c21958d2643b09996) Thanks [@eoinest](https://github.com/eoinest)! - Use a browser navigation when switching back to an unprefixed default locale.
16
+
17
+ - Updated dependencies []:
18
+ - @generaltranslation/react-core@11.1.12
19
+ - gt-react@11.1.12
20
+
3
21
  ## 11.1.11
4
22
 
5
23
  ### Patch Changes
@@ -1 +1 @@
1
- {"version":3,"file":"client-boundary.d.ts","sourceRoot":"","sources":["../../src/utils/client-boundary.tsx"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AAEH,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACnE,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGnE,OAAO,EAAc,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAsBlE;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,qBAAqB,2CAS7D"}
1
+ {"version":3,"file":"client-boundary.d.ts","sourceRoot":"","sources":["../../src/utils/client-boundary.tsx"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AAEH,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACnE,OAAO,EAAE,cAAc,IAAI,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGnE,OAAO,EAAc,KAAK,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAsBlE;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,qBAAqB,2CA+C7D"}
@@ -22,23 +22,41 @@ if (typeof window !== "undefined") require_setup_initGT_client.initializeGTClien
22
22
  */
23
23
  function Client_GTProvider(props) {
24
24
  const router = (0, next_navigation_js.useRouter)();
25
- const reload = (0, react.useCallback)(() => {
25
+ const refreshServerComponents = (0, react.useCallback)(() => {
26
26
  router.refresh();
27
27
  }, [router]);
28
+ const reloadBrowserPage = (0, react.useCallback)(() => {
29
+ globalThis.location.reload();
30
+ }, []);
31
+ const syncServerContent = (0, react.useCallback)(({ locale }) => {
32
+ const i18nConfig = (0, gt_i18n_internal.getI18nConfig)();
33
+ const localeRoutingEnabled = (0, gt_i18n_internal.getCookieValue)(document.cookie, require_utils_cookies.defaultLocaleRoutingEnabledCookieName) === "true";
34
+ const defaultLocale = i18nConfig.getDefaultLocale();
35
+ const currentPathname = globalThis.location.pathname;
36
+ const localeRoutingApplies = localeRoutingEnabled && require_utils_pathRegex.pathnameMatchesRegex(currentPathname, pathRegex);
37
+ const currentPathLocale = localeRoutingApplies ? extractLocale(currentPathname, i18nConfig) : null;
38
+ if (localeRoutingApplies && locale === defaultLocale && currentPathLocale && currentPathLocale !== defaultLocale) {
39
+ reloadBrowserPage();
40
+ return;
41
+ }
42
+ refreshServerComponents();
43
+ }, [refreshServerComponents, reloadBrowserPage]);
28
44
  usePathCheck({
29
- reloadServer: reload,
45
+ reloadBrowserPage,
46
+ refreshServerComponents,
30
47
  locale: props.locale
31
48
  });
32
49
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(gt_react.GTProvider, {
33
50
  ...props,
34
- _reload: reload
51
+ _reload: syncServerContent
35
52
  });
36
53
  }
37
54
  /**
38
- * Reloads the server components if
55
+ * Synchronizes server content if the URL locale does not match the selected
56
+ * locale.
39
57
  * TODO: optimize this hook
40
58
  */
41
- function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = require_utils_cookies.defaultReferrerLocaleCookieName, localeRoutingEnabledCookieName = require_utils_cookies.defaultLocaleRoutingEnabledCookieName }) {
59
+ function usePathCheck({ reloadBrowserPage, refreshServerComponents, locale, referrerLocaleCookieName = require_utils_cookies.defaultReferrerLocaleCookieName, localeRoutingEnabledCookieName = require_utils_cookies.defaultLocaleRoutingEnabledCookieName }) {
42
60
  const pathname = (0, next_navigation_js.usePathname)();
43
61
  (0, react.useEffect)(() => {
44
62
  const i18nConfig = (0, gt_i18n_internal.getI18nConfig)();
@@ -47,11 +65,12 @@ function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = require
47
65
  const defaultLocale = i18nConfig.getDefaultLocale();
48
66
  if ((0, gt_i18n_internal.getCookieValue)(document.cookie, localeRoutingEnabledCookieName) === "true" && require_utils_pathRegex.pathnameMatchesRegex(pathname, pathRegex)) {
49
67
  const extractedLocale = extractLocale(pathname, i18nConfig) || defaultLocale;
50
- let pathLocale = i18nConfig.determineLocale([i18nConfig.isGTServicesEnabled() ? i18nConfig.standardizeLocale(extractedLocale) : extractedLocale, defaultLocale], locales);
51
- if (pathLocale) pathLocale = i18nConfig.resolveAliasLocale(pathLocale);
52
- if (pathLocale && locales.includes(pathLocale) && pathLocale !== locale) {
68
+ let currentPathLocale = i18nConfig.determineLocale([i18nConfig.isGTServicesEnabled() ? i18nConfig.standardizeLocale(extractedLocale) : extractedLocale, defaultLocale], locales);
69
+ if (currentPathLocale) currentPathLocale = i18nConfig.resolveAliasLocale(currentPathLocale);
70
+ if (currentPathLocale && locales.includes(currentPathLocale) && currentPathLocale !== locale) {
53
71
  document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;
54
- reloadServer();
72
+ if (locale === defaultLocale) reloadBrowserPage();
73
+ else refreshServerComponents();
55
74
  }
56
75
  }
57
76
  }, [
@@ -59,7 +78,8 @@ function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = require
59
78
  locale,
60
79
  referrerLocaleCookieName,
61
80
  localeRoutingEnabledCookieName,
62
- reloadServer
81
+ reloadBrowserPage,
82
+ refreshServerComponents
63
83
  ]);
64
84
  }
65
85
  function extractLocale(pathname, i18nConfig) {
@@ -1 +1 @@
1
- {"version":3,"file":"client-boundary.js","names":["compilePathRegex","GTProvider","defaultReferrerLocaleCookieName","defaultLocaleRoutingEnabledCookieName","pathnameMatchesRegex"],"sources":["../../src/utils/client-boundary.tsx"],"sourcesContent":["'use client';\n\n/**\n * This is a small boundary for RSC consumption of client components\n * Cannot be consumed through gt-react/index.rsc as deciding btwn\n * gt-react/index.server and gt-react/index.client can only happen\n * here. This matters for GTProvider, but not for LocaleSelector.\n * This pattern is just good to follow for carrying over different\n * behaviors between client and server from gt-react.\n */\n\nexport { LocaleSelector as Client_LocaleSelector } from 'gt-react';\nexport { RegionSelector as Client_RegionSelector } from 'gt-react';\n\nimport { getCookieValue, getI18nConfig, I18nConfig } from 'gt-i18n/internal';\nimport { GTProvider, type SharedGTProviderProps } from 'gt-react';\nimport { usePathname, useRouter } from 'next/navigation';\nimport { useCallback, useEffect } from 'react';\nimport { initializeGTClient } from '../setup/initGT.client';\nimport {\n defaultLocaleRoutingEnabledCookieName,\n defaultReferrerLocaleCookieName,\n} from './cookies';\nimport { compilePathRegex, pathnameMatchesRegex } from './pathRegex';\n\n// withGTConfig exposes this build-time value to both middleware and client code.\nconst pathRegex = compilePathRegex(process.env._GENERALTRANSLATION_PATH_REGEX);\n\n/**\n * Only need to initalize client. We know server was already\n * inialaized by index.rsc.ts. We do not know yet if client\n * has been initialized by index.client.ts.\n */\nif (typeof window !== 'undefined') {\n initializeGTClient();\n}\n\n/**\n * Small wrapper to embed nextjs app router behavior\n */\nexport function Client_GTProvider(props: SharedGTProviderProps) {\n const router = useRouter();\n const reload = useCallback(() => {\n // Reload server components\n router.refresh();\n }, [router]);\n // TODO: when routing is enabled, validate the path matches the locale\n usePathCheck({ reloadServer: reload, locale: props.locale });\n return <GTProvider {...props} _reload={reload} />;\n}\n\n/**\n * Reloads the server components if\n * TODO: optimize this hook\n */\nfunction usePathCheck({\n reloadServer,\n locale,\n referrerLocaleCookieName = defaultReferrerLocaleCookieName,\n localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName,\n}: {\n reloadServer: () => void;\n locale: string;\n referrerLocaleCookieName?: string;\n localeRoutingEnabledCookieName?: string;\n}) {\n const pathname = usePathname();\n\n useEffect(() => {\n // Track the referrer locale for middleware\n const i18nConfig = getI18nConfig();\n document.cookie = `${referrerLocaleCookieName}=${i18nConfig.resolveAliasLocale(locale)};path=/`;\n\n // Reload the server components if the pathname changes\n const locales = i18nConfig.getLocales();\n const defaultLocale = i18nConfig.getDefaultLocale();\n const middlewareEnabled =\n getCookieValue(document.cookie, localeRoutingEnabledCookieName) ===\n 'true';\n if (middlewareEnabled && pathnameMatchesRegex(pathname, pathRegex)) {\n // Extract locale from pathname\n const extractedLocale =\n extractLocale(pathname, i18nConfig) || defaultLocale;\n let pathLocale = i18nConfig.determineLocale(\n [\n i18nConfig.isGTServicesEnabled()\n ? i18nConfig.standardizeLocale(extractedLocale)\n : extractedLocale,\n defaultLocale,\n ],\n locales\n );\n if (pathLocale) {\n pathLocale = i18nConfig.resolveAliasLocale(pathLocale);\n }\n\n if (pathLocale && locales.includes(pathLocale) && pathLocale !== locale) {\n // clear cookie (avoids infinite loop when there is no middleware)\n document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;\n\n // reload page\n reloadServer();\n }\n }\n }, [\n pathname,\n locale,\n referrerLocaleCookieName,\n localeRoutingEnabledCookieName,\n reloadServer,\n ]);\n}\n\nfunction extractLocale(\n pathname: string,\n i18nConfig: I18nConfig\n): string | null {\n const matches = pathname.match(/^\\/([^/]+)(?:\\/|$)/);\n return matches ? i18nConfig.resolveAliasLocale(matches[1]) : null;\n}\n"],"mapp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,cACP,UACA,YACe;CACf,MAAM,UAAU,SAAS,MAAM,qBAAqB;AACpD,QAAO,UAAU,WAAW,mBAAmB,QAAQ,GAAG,GAAG"}
1
+ {"version":3,"file":"client-boundary.js","names":["compilePathRegex","defaultLocaleRoutingEnabledCookieName","pathnameMatchesRegex","GTProvider","defaultReferrerLocaleCookieName"],"sources":["../../src/utils/client-boundary.tsx"],"sourcesContent":["'use client';\n\n/**\n * This is a small boundary for RSC consumption of client components\n * Cannot be consumed through gt-react/index.rsc as deciding btwn\n * gt-react/index.server and gt-react/index.client can only happen\n * here. This matters for GTProvider, but not for LocaleSelector.\n * This pattern is just good to follow for carrying over different\n * behaviors between client and server from gt-react.\n */\n\nexport { LocaleSelector as Client_LocaleSelector } from 'gt-react';\nexport { RegionSelector as Client_RegionSelector } from 'gt-react';\n\nimport { getCookieValue, getI18nConfig, I18nConfig } from 'gt-i18n/internal';\nimport { GTProvider, type SharedGTProviderProps } from 'gt-react';\nimport { usePathname, useRouter } from 'next/navigation';\nimport { useCallback, useEffect } from 'react';\nimport { initializeGTClient } from '../setup/initGT.client';\nimport {\n defaultLocaleRoutingEnabledCookieName,\n defaultReferrerLocaleCookieName,\n} from './cookies';\nimport { compilePathRegex, pathnameMatchesRegex } from './pathRegex';\n\n// withGTConfig exposes this build-time value to both middleware and client code.\nconst pathRegex = compilePathRegex(process.env._GENERALTRANSLATION_PATH_REGEX);\n\n/**\n * Only need to initalize client. We know server was already\n * inialaized by index.rsc.ts. We do not know yet if client\n * has been initialized by index.client.ts.\n */\nif (typeof window !== 'undefined') {\n initializeGTClient();\n}\n\n/**\n * Small wrapper to embed nextjs app router behavior\n */\nexport function Client_GTProvider(props: SharedGTProviderProps) {\n const router = useRouter();\n const refreshServerComponents = useCallback(() => {\n router.refresh();\n }, [router]);\n const reloadBrowserPage = useCallback(() => {\n globalThis.location.reload();\n }, []);\n const syncServerContent = useCallback<\n NonNullable<SharedGTProviderProps['_reload']>\n >(\n ({ locale }) => {\n const i18nConfig = getI18nConfig();\n const localeRoutingEnabled =\n getCookieValue(\n document.cookie,\n defaultLocaleRoutingEnabledCookieName\n ) === 'true';\n const defaultLocale = i18nConfig.getDefaultLocale();\n const currentPathname = globalThis.location.pathname;\n const localeRoutingApplies =\n localeRoutingEnabled &&\n pathnameMatchesRegex(currentPathname, pathRegex);\n const currentPathLocale = localeRoutingApplies\n ? extractLocale(currentPathname, i18nConfig)\n : null;\n\n if (\n localeRoutingApplies &&\n locale === defaultLocale &&\n currentPathLocale &&\n currentPathLocale !== defaultLocale\n ) {\n reloadBrowserPage();\n return;\n }\n\n refreshServerComponents();\n },\n [refreshServerComponents, reloadBrowserPage]\n );\n usePathCheck({\n reloadBrowserPage,\n refreshServerComponents,\n locale: props.locale,\n });\n return <GTProvider {...props} _reload={syncServerContent} />;\n}\n\n/**\n * Synchronizes server content if the URL locale does not match the selected\n * locale.\n * TODO: optimize this hook\n */\nfunction usePathCheck({\n reloadBrowserPage,\n refreshServerComponents,\n locale,\n referrerLocaleCookieName = defaultReferrerLocaleCookieName,\n localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName,\n}: {\n reloadBrowserPage: () => void;\n refreshServerComponents: () => void;\n locale: string;\n referrerLocaleCookieName?: string;\n localeRoutingEnabledCookieName?: string;\n}) {\n const pathname = usePathname();\n\n useEffect(() => {\n // Track the referrer locale for middleware\n const i18nConfig = getI18nConfig();\n document.cookie = `${referrerLocaleCookieName}=${i18nConfig.resolveAliasLocale(locale)};path=/`;\n\n // Synchronize server content if the pathname changes\n const locales = i18nConfig.getLocales();\n const defaultLocale = i18nConfig.getDefaultLocale();\n const localeRoutingEnabled =\n getCookieValue(document.cookie, localeRoutingEnabledCookieName) ===\n 'true';\n if (localeRoutingEnabled && pathnameMatchesRegex(pathname, pathRegex)) {\n // Extract locale from pathname\n const extractedLocale =\n extractLocale(pathname, i18nConfig) || defaultLocale;\n let currentPathLocale = i18nConfig.determineLocale(\n [\n i18nConfig.isGTServicesEnabled()\n ? i18nConfig.standardizeLocale(extractedLocale)\n : extractedLocale,\n defaultLocale,\n ],\n locales\n );\n if (currentPathLocale) {\n currentPathLocale = i18nConfig.resolveAliasLocale(currentPathLocale);\n }\n\n if (\n currentPathLocale &&\n locales.includes(currentPathLocale) &&\n currentPathLocale !== locale\n ) {\n // clear cookie (avoids infinite loop when there is no middleware)\n document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;\n\n if (locale === defaultLocale) {\n // A browser navigation follows the middleware redirect that removes\n // the default locale prefix. Next.js router.refresh() does not.\n reloadBrowserPage();\n } else {\n refreshServerComponents();\n }\n }\n }\n }, [\n pathname,\n locale,\n referrerLocaleCookieName,\n localeRoutingEnabledCookieName,\n reloadBrowserPage,\n refreshServerComponents,\n ]);\n}\n\nfunction extractLocale(\n pathname: string,\n i18nConfig: I18nConfig\n): string | null {\n const matches = pathname.match(/^\\/([^/]+)(?:\\/|$)/);\n return matches ? i18nConfig.resolveAliasLocale(matches[1]) : null;\n}\n"],"mappings":";;;;;;;;;;;;AA0BA,MAAM,YAAYA,wBAAAA,iBAAiB,QAAQ,IAAI,+BAA+B;;;;;;AAO9E,IAAI,OAAO,WAAW,YACpB,6BAAA,oBAAoB;;;;AAMtB,SAAgB,kBAAkB,OAA8B;CAC9D,MAAM,UAAA,GAAA,mBAAA,YAAoB;CAC1B,MAAM,2BAAA,GAAA,MAAA,mBAA4C;AAChD,SAAO,SAAS;IACf,CAAC,OAAO,CAAC;CACZ,MAAM,qBAAA,GAAA,MAAA,mBAAsC;AAC1C,aAAW,SAAS,QAAQ;IAC3B,EAAE,CAAC;CACN,MAAM,qBAAA,GAAA,MAAA,cAGH,EAAE,aAAa;EACd,MAAM,cAAA,GAAA,iBAAA,gBAA4B;EAClC,MAAM,wBAAA,GAAA,iBAAA,gBAEF,SAAS,QACTC,sBAAAA,sCACD,KAAK;EACR,MAAM,gBAAgB,WAAW,kBAAkB;EACnD,MAAM,kBAAkB,WAAW,SAAS;EAC5C,MAAM,uBACJ,wBACAC,w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AAU,SAAS,MAAM,qBAAqB;AACpD,QAAO,UAAU,WAAW,mBAAmB,QAAQ,GAAG,GAAG"}
@@ -20,23 +20,41 @@ if (typeof window !== "undefined") initializeGTClient();
20
20
  */
21
21
  function Client_GTProvider(props) {
22
22
  const router = useRouter();
23
- const reload = useCallback(() => {
23
+ const refreshServerComponents = useCallback(() => {
24
24
  router.refresh();
25
25
  }, [router]);
26
+ const reloadBrowserPage = useCallback(() => {
27
+ globalThis.location.reload();
28
+ }, []);
29
+ const syncServerContent = useCallback(({ locale }) => {
30
+ const i18nConfig = getI18nConfig();
31
+ const localeRoutingEnabled = getCookieValue(document.cookie, defaultLocaleRoutingEnabledCookieName) === "true";
32
+ const defaultLocale = i18nConfig.getDefaultLocale();
33
+ const currentPathname = globalThis.location.pathname;
34
+ const localeRoutingApplies = localeRoutingEnabled && pathnameMatchesRegex(currentPathname, pathRegex);
35
+ const currentPathLocale = localeRoutingApplies ? extractLocale(currentPathname, i18nConfig) : null;
36
+ if (localeRoutingApplies && locale === defaultLocale && currentPathLocale && currentPathLocale !== defaultLocale) {
37
+ reloadBrowserPage();
38
+ return;
39
+ }
40
+ refreshServerComponents();
41
+ }, [refreshServerComponents, reloadBrowserPage]);
26
42
  usePathCheck({
27
- reloadServer: reload,
43
+ reloadBrowserPage,
44
+ refreshServerComponents,
28
45
  locale: props.locale
29
46
  });
30
47
  return /* @__PURE__ */ jsx(GTProvider, {
31
48
  ...props,
32
- _reload: reload
49
+ _reload: syncServerContent
33
50
  });
34
51
  }
35
52
  /**
36
- * Reloads the server components if
53
+ * Synchronizes server content if the URL locale does not match the selected
54
+ * locale.
37
55
  * TODO: optimize this hook
38
56
  */
39
- function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = defaultReferrerLocaleCookieName, localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName }) {
57
+ function usePathCheck({ reloadBrowserPage, refreshServerComponents, locale, referrerLocaleCookieName = defaultReferrerLocaleCookieName, localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName }) {
40
58
  const pathname = usePathname();
41
59
  useEffect(() => {
42
60
  const i18nConfig = getI18nConfig();
@@ -45,11 +63,12 @@ function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = default
45
63
  const defaultLocale = i18nConfig.getDefaultLocale();
46
64
  if (getCookieValue(document.cookie, localeRoutingEnabledCookieName) === "true" && pathnameMatchesRegex(pathname, pathRegex)) {
47
65
  const extractedLocale = extractLocale(pathname, i18nConfig) || defaultLocale;
48
- let pathLocale = i18nConfig.determineLocale([i18nConfig.isGTServicesEnabled() ? i18nConfig.standardizeLocale(extractedLocale) : extractedLocale, defaultLocale], locales);
49
- if (pathLocale) pathLocale = i18nConfig.resolveAliasLocale(pathLocale);
50
- if (pathLocale && locales.includes(pathLocale) && pathLocale !== locale) {
66
+ let currentPathLocale = i18nConfig.determineLocale([i18nConfig.isGTServicesEnabled() ? i18nConfig.standardizeLocale(extractedLocale) : extractedLocale, defaultLocale], locales);
67
+ if (currentPathLocale) currentPathLocale = i18nConfig.resolveAliasLocale(currentPathLocale);
68
+ if (currentPathLocale && locales.includes(currentPathLocale) && currentPathLocale !== locale) {
51
69
  document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;
52
- reloadServer();
70
+ if (locale === defaultLocale) reloadBrowserPage();
71
+ else refreshServerComponents();
53
72
  }
54
73
  }
55
74
  }, [
@@ -57,7 +76,8 @@ function usePathCheck({ reloadServer, locale, referrerLocaleCookieName = default
57
76
  locale,
58
77
  referrerLocaleCookieName,
59
78
  localeRoutingEnabledCookieName,
60
- reloadServer
79
+ reloadBrowserPage,
80
+ refreshServerComponents
61
81
  ]);
62
82
  }
63
83
  function extractLocale(pathname, i18nConfig) {
@@ -1 +1 @@
1
- {"version":3,"file":"client-boundary.mjs","names":[],"sources":["../../src/utils/client-boundary.tsx"],"sourcesContent":["'use client';\n\n/**\n * This is a small boundary for RSC consumption of client components\n * Cannot be consumed through gt-react/index.rsc as deciding btwn\n * gt-react/index.server and gt-react/index.client can only happen\n * here. This matters for GTProvider, but not for LocaleSelector.\n * This pattern is just good to follow for carrying over different\n * behaviors between client and server from gt-react.\n */\n\nexport { LocaleSelector as Client_LocaleSelector } from 'gt-react';\nexport { RegionSelector as Client_RegionSelector } from 'gt-react';\n\nimport { getCookieValue, getI18nConfig, I18nConfig } from 'gt-i18n/internal';\nimport { GTProvider, type SharedGTProviderProps } from 'gt-react';\nimport { usePathname, useRouter } from 'next/navigation';\nimport { useCallback, useEffect } from 'react';\nimport { initializeGTClient } from '../setup/initGT.client';\nimport {\n defaultLocaleRoutingEnabledCookieName,\n defaultReferrerLocaleCookieName,\n} from './cookies';\nimport { compilePathRegex, pathnameMatchesRegex } from './pathRegex';\n\n// withGTConfig exposes this build-time value to both middleware and client code.\nconst pathRegex = compilePathRegex(process.env._GENERALTRANSLATION_PATH_REGEX);\n\n/**\n * Only need to initalize client. We know server was already\n * inialaized by index.rsc.ts. We do not know yet if client\n * has been initialized by index.client.ts.\n */\nif (typeof window !== 'undefined') {\n initializeGTClient();\n}\n\n/**\n * Small wrapper to embed nextjs app router behavior\n */\nexport function Client_GTProvider(props: SharedGTProviderProps) {\n const router = useRouter();\n const reload = useCallback(() => {\n // Reload server components\n router.refresh();\n }, [router]);\n // TODO: when routing is enabled, validate the path matches the locale\n usePathCheck({ reloadServer: reload, locale: props.locale });\n return <GTProvider {...props} _reload={reload} />;\n}\n\n/**\n * Reloads the server components if\n * TODO: optimize this hook\n */\nfunction usePathCheck({\n reloadServer,\n locale,\n referrerLocaleCookieName = defaultReferrerLocaleCookieName,\n localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName,\n}: {\n reloadServer: () => void;\n locale: string;\n referrerLocaleCookieName?: string;\n localeRoutingEnabledCookieName?: string;\n}) {\n const pathname = usePathname();\n\n useEffect(() => {\n // Track the referrer locale for middleware\n const i18nConfig = getI18nConfig();\n document.cookie = `${referrerLocaleCookieName}=${i18nConfig.resolveAliasLocale(locale)};path=/`;\n\n // Reload the server components if the pathname changes\n const locales = i18nConfig.getLocales();\n const defaultLocale = i18nConfig.getDefaultLocale();\n const middlewareEnabled =\n getCookieValue(document.cookie, localeRoutingEnabledCookieName) ===\n 'true';\n if (middlewareEnabled && pathnameMatchesRegex(pathname, pathRegex)) {\n // Extract locale from pathname\n const extractedLocale =\n extractLocale(pathname, i18nConfig) || defaultLocale;\n let pathLocale = i18nConfig.determineLocale(\n [\n i18nConfig.isGTServicesEnabled()\n ? i18nConfig.standardizeLocale(extractedLocale)\n : extractedLocale,\n defaultLocale,\n ],\n locales\n );\n if (pathLocale) {\n pathLocale = i18nConfig.resolveAliasLocale(pathLocale);\n }\n\n if (pathLocale && locales.includes(pathLocale) && pathLocale !== locale) {\n // clear cookie (avoids infinite loop when there is no middleware)\n document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;\n\n // reload page\n reloadServer();\n }\n }\n }, [\n pathname,\n locale,\n referrerLocaleCookieName,\n localeRoutingEnabledCookieName,\n reloadServer,\n ]);\n}\n\nfunction extractLocale(\n pathname: string,\n i18nConfig: I18nConfig\n): string | null {\n const matches = pathname.match(/^\\/([^/]+)(?:\\/|$)/);\n return matches ? i18nConfig.resolveAliasLocale(matches[1]) : null;\n}\n"],"mappings":";;;;;;;;;;AA0BA,MAAM,YAAY,iBAAiB,QAAQ,IAAI,+BAA+B;;;;;;AAO9E,IAAI,OAAO,WAAW,YACpB,qBAAoB;;;;AAMtB,SAAgB,kBAAkB,OAA8B;CAC9D,MAAM,SAAS,WAAW;CAC1B,MAAM,SAAS,kBAAkB;AAE/B,SAAO,SAAS;IACf,CAAC,OAAO,CAAC;AAEZ,cAAa;EAAE,cAAc;EAAQ,QAAQ,MAAM;EAAQ,CAAC;AAC5D,QAAO,oBAAC,YAAD;EAAY,GAAI;EAAO,SAAS;EAAU,CAAA;;;;;;AAOnD,SAAS,aAAa,EACpB,cACA,QACA,2BAA2B,iCAC3B,iCAAiC,yCAMhC;CACD,MAAM,WAAW,aAAa;AAE9B,iBAAgB;EAEd,MAAM,aAAa,eAAe;AAClC,WAAS,SAAS,GAAG,yBAAyB,GAAG,WAAW,mBAAmB,OAAO,CAAC;EAGvF,MAAM,UAAU,WAAW,YAAY;EACvC,MAAM,gBAAgB,WAAW,kBAAkB;AAInD,MAFE,eAAe,SAAS,QAAQ,+BAA+B,KAC/D,UACuB,qBAAqB,UAAU,UAAU,EAAE;GAElE,MAAM,kBACJ,cAAc,UAAU,WAAW,IAAI;GACzC,IAAI,aAAa,WAAW,gBAC1B,CACE,WAAW,qBAAqB,GAC5B,WAAW,kBAAkB,gBAAgB,GAC7C,iBACJ,cACD,EACD,QACD;AACD,OAAI,WACF,cAAa,WAAW,mBAAmB,WAAW;AAGxD,OAAI,cAAc,QAAQ,SAAS,WAAW,IAAI,eAAe,QAAQ;AAEvE,aAAS,SAAS,GAAG,+BAA+B;AAGpD,kBAAc;;;IAGjB;EACD;EACA;EACA;EACA;EACA;EACD,CAAC;;AAGJ,SAAS,cACP,UACA,YACe;CACf,MAAM,UAAU,SAAS,MAAM,qBAAqB;AACpD,QAAO,UAAU,WAAW,mBAAmB,QAAQ,GAAG,GAAG"}
1
+ {"version":3,"file":"client-boundary.mjs","names":[],"sources":["../../src/utils/client-boundary.tsx"],"sourcesContent":["'use client';\n\n/**\n * This is a small boundary for RSC consumption of client components\n * Cannot be consumed through gt-react/index.rsc as deciding btwn\n * gt-react/index.server and gt-react/index.client can only happen\n * here. This matters for GTProvider, but not for LocaleSelector.\n * This pattern is just good to follow for carrying over different\n * behaviors between client and server from gt-react.\n */\n\nexport { LocaleSelector as Client_LocaleSelector } from 'gt-react';\nexport { RegionSelector as Client_RegionSelector } from 'gt-react';\n\nimport { getCookieValue, getI18nConfig, I18nConfig } from 'gt-i18n/internal';\nimport { GTProvider, type SharedGTProviderProps } from 'gt-react';\nimport { usePathname, useRouter } from 'next/navigation';\nimport { useCallback, useEffect } from 'react';\nimport { initializeGTClient } from '../setup/initGT.client';\nimport {\n defaultLocaleRoutingEnabledCookieName,\n defaultReferrerLocaleCookieName,\n} from './cookies';\nimport { compilePathRegex, pathnameMatchesRegex } from './pathRegex';\n\n// withGTConfig exposes this build-time value to both middleware and client code.\nconst pathRegex = compilePathRegex(process.env._GENERALTRANSLATION_PATH_REGEX);\n\n/**\n * Only need to initalize client. We know server was already\n * inialaized by index.rsc.ts. We do not know yet if client\n * has been initialized by index.client.ts.\n */\nif (typeof window !== 'undefined') {\n initializeGTClient();\n}\n\n/**\n * Small wrapper to embed nextjs app router behavior\n */\nexport function Client_GTProvider(props: SharedGTProviderProps) {\n const router = useRouter();\n const refreshServerComponents = useCallback(() => {\n router.refresh();\n }, [router]);\n const reloadBrowserPage = useCallback(() => {\n globalThis.location.reload();\n }, []);\n const syncServerContent = useCallback<\n NonNullable<SharedGTProviderProps['_reload']>\n >(\n ({ locale }) => {\n const i18nConfig = getI18nConfig();\n const localeRoutingEnabled =\n getCookieValue(\n document.cookie,\n defaultLocaleRoutingEnabledCookieName\n ) === 'true';\n const defaultLocale = i18nConfig.getDefaultLocale();\n const currentPathname = globalThis.location.pathname;\n const localeRoutingApplies =\n localeRoutingEnabled &&\n pathnameMatchesRegex(currentPathname, pathRegex);\n const currentPathLocale = localeRoutingApplies\n ? extractLocale(currentPathname, i18nConfig)\n : null;\n\n if (\n localeRoutingApplies &&\n locale === defaultLocale &&\n currentPathLocale &&\n currentPathLocale !== defaultLocale\n ) {\n reloadBrowserPage();\n return;\n }\n\n refreshServerComponents();\n },\n [refreshServerComponents, reloadBrowserPage]\n );\n usePathCheck({\n reloadBrowserPage,\n refreshServerComponents,\n locale: props.locale,\n });\n return <GTProvider {...props} _reload={syncServerContent} />;\n}\n\n/**\n * Synchronizes server content if the URL locale does not match the selected\n * locale.\n * TODO: optimize this hook\n */\nfunction usePathCheck({\n reloadBrowserPage,\n refreshServerComponents,\n locale,\n referrerLocaleCookieName = defaultReferrerLocaleCookieName,\n localeRoutingEnabledCookieName = defaultLocaleRoutingEnabledCookieName,\n}: {\n reloadBrowserPage: () => void;\n refreshServerComponents: () => void;\n locale: string;\n referrerLocaleCookieName?: string;\n localeRoutingEnabledCookieName?: string;\n}) {\n const pathname = usePathname();\n\n useEffect(() => {\n // Track the referrer locale for middleware\n const i18nConfig = getI18nConfig();\n document.cookie = `${referrerLocaleCookieName}=${i18nConfig.resolveAliasLocale(locale)};path=/`;\n\n // Synchronize server content if the pathname changes\n const locales = i18nConfig.getLocales();\n const defaultLocale = i18nConfig.getDefaultLocale();\n const localeRoutingEnabled =\n getCookieValue(document.cookie, localeRoutingEnabledCookieName) ===\n 'true';\n if (localeRoutingEnabled && pathnameMatchesRegex(pathname, pathRegex)) {\n // Extract locale from pathname\n const extractedLocale =\n extractLocale(pathname, i18nConfig) || defaultLocale;\n let currentPathLocale = i18nConfig.determineLocale(\n [\n i18nConfig.isGTServicesEnabled()\n ? i18nConfig.standardizeLocale(extractedLocale)\n : extractedLocale,\n defaultLocale,\n ],\n locales\n );\n if (currentPathLocale) {\n currentPathLocale = i18nConfig.resolveAliasLocale(currentPathLocale);\n }\n\n if (\n currentPathLocale &&\n locales.includes(currentPathLocale) &&\n currentPathLocale !== locale\n ) {\n // clear cookie (avoids infinite loop when there is no middleware)\n document.cookie = `${localeRoutingEnabledCookieName}=;path=/`;\n\n if (locale === defaultLocale) {\n // A browser navigation follows the middleware redirect that removes\n // the default locale prefix. Next.js router.refresh() does not.\n reloadBrowserPage();\n } else {\n refreshServerComponents();\n }\n }\n }\n }, [\n pathname,\n locale,\n referrerLocaleCookieName,\n localeRoutingEnabledCookieName,\n reloadBrowserPage,\n refreshServerComponents,\n ]);\n}\n\nfunction extractLocale(\n pathname: string,\n i18nConfig: I18nConfig\n): string | null {\n const matches = pathname.match(/^\\/([^/]+)(?:\\/|$)/);\n return matches ? i18nConfig.resolveAliasLocale(matches[1]) : null;\n}\n"],"mappings":";;;;;;;;;;AA0BA,MAAM,YAAY,iBAAiB,QAAQ,IAAI,+BAA+B;;;;;;AAO9E,IAAI,OAAO,WAAW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cACD,EACD,QACD;AACD,OAAI,kBACF,qBAAoB,WAAW,mBAAmB,kBAAkB;AAGtE,OACE,qBACA,QAAQ,SAAS,kBAAkB,IACnC,sBAAsB,QACtB;AAEA,aAAS,SAAS,GAAG,+BAA+B;AAEpD,QAAI,WAAW,cAGb,oBAAmB;QAEnB,0BAAyB;;;IAI9B;EACD;EACA;EACA;EACA;EACA;EACA;EACD,CAAC;;AAGJ,SAAS,cACP,UACA,YACe;CACf,MAAM,UAAU,SAAS,MAAM,qBAAqB;AACpD,QAAO,UAAU,WAAW,mBAAmB,QAAQ,GAAG,GAAG"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gt-next",
3
- "version": "11.1.11",
3
+ "version": "11.1.13",
4
4
  "description": "A Next.js library for automatic internationalization.",
5
5
  "main": "dist/index.server.js",
6
6
  "peerDependencies": {
@@ -29,10 +29,10 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@generaltranslation/format": "0.1.7",
32
- "@generaltranslation/react-core": "11.1.11",
33
- "generaltranslation": "9.1.6",
34
- "gt-react": "11.1.11",
35
- "gt-i18n": "1.0.16"
32
+ "@generaltranslation/react-core": "11.1.13",
33
+ "gt-react": "11.1.13",
34
+ "gt-i18n": "1.0.16",
35
+ "generaltranslation": "9.1.6"
36
36
  },
37
37
  "repository": {
38
38
  "type": "git",