fontdue-js 3.14.0 → 3.15.0

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.
Files changed (226) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/__generated__/CartOrderCompleteOrderMutation.graphql.d.ts +1 -1
  3. package/dist/__generated__/CartOrderCompleteOrderMutation.graphql.js +3 -3
  4. package/dist/__generated__/CartOrderRemoveDiscountMutation.graphql.d.ts +1 -1
  5. package/dist/__generated__/CartOrderRemoveDiscountMutation.graphql.js +3 -3
  6. package/dist/__generated__/CartOrderUpdateMutation.graphql.d.ts +1 -1
  7. package/dist/__generated__/CartOrderUpdateMutation.graphql.js +3 -3
  8. package/dist/__generated__/CartQuery.graphql.d.ts +1 -1
  9. package/dist/__generated__/CartQuery.graphql.js +3 -3
  10. package/dist/__generated__/CartStateUpdateMutation.graphql.d.ts +5 -1
  11. package/dist/__generated__/CartStateUpdateMutation.graphql.js +132 -117
  12. package/dist/__generated__/CartState_order.graphql.d.ts +4 -1
  13. package/dist/__generated__/CartState_order.graphql.js +11 -2
  14. package/dist/__generated__/CheckoutUpdateCustomerMutation.graphql.d.ts +1 -1
  15. package/dist/__generated__/CheckoutUpdateCustomerMutation.graphql.js +3 -3
  16. package/dist/__generated__/CheckoutUpdateOrderMutation.graphql.d.ts +1 -1
  17. package/dist/__generated__/CheckoutUpdateOrderMutation.graphql.js +3 -3
  18. package/dist/__generated__/FamilyList_node.graphql.d.ts +3 -1
  19. package/dist/__generated__/FamilyList_node.graphql.js +8 -2
  20. package/dist/__generated__/FontFamiliesQuery.graphql.d.ts +1 -1
  21. package/dist/__generated__/FontFamiliesQuery.graphql.js +5 -5
  22. package/dist/__generated__/PrecartAddToCartMutation.graphql.d.ts +1 -1
  23. package/dist/__generated__/PrecartAddToCartMutation.graphql.js +3 -3
  24. package/dist/__generated__/StoreModalCartQuery.graphql.d.ts +1 -1
  25. package/dist/__generated__/StoreModalCartQuery.graphql.js +69 -69
  26. package/dist/__generated__/StoreModalFamily_collection.graphql.d.ts +2 -1
  27. package/dist/__generated__/StoreModalFamily_collection.graphql.js +16 -15
  28. package/dist/__generated__/StoreModalIndexItem_fontCollection.graphql.d.ts +19 -1
  29. package/dist/__generated__/StoreModalIndexItem_fontCollection.graphql.js +39 -4
  30. package/dist/__generated__/StoreModalIndexPaginationQuery.graphql.d.ts +1 -1
  31. package/dist/__generated__/StoreModalIndexPaginationQuery.graphql.js +40 -5
  32. package/dist/__generated__/StoreModalIndexQuery.graphql.d.ts +1 -1
  33. package/dist/__generated__/StoreModalIndexQuery.graphql.js +40 -5
  34. package/dist/__generated__/StoreModalProductContent_collection.graphql.d.ts +19 -1
  35. package/dist/__generated__/StoreModalProductContent_collection.graphql.js +46 -11
  36. package/dist/__generated__/StoreModalProductQuery.graphql.d.ts +1 -1
  37. package/dist/__generated__/StoreModalProductQuery.graphql.js +198 -167
  38. package/dist/__generated__/StoreModalProductRefetchQuery.graphql.d.ts +1 -1
  39. package/dist/__generated__/StoreModalProductRefetchQuery.graphql.js +171 -140
  40. package/dist/__generated__/StoreModalProductSlugQuery.graphql.d.ts +1 -1
  41. package/dist/__generated__/StoreModalProductSlugQuery.graphql.js +201 -170
  42. package/dist/__generated__/StripeProviderCreateCheckoutSessionMutation.graphql.d.ts +5 -2
  43. package/dist/__generated__/StripeProviderCreateCheckoutSessionMutation.graphql.js +41 -22
  44. package/dist/__generated__/TestFontsFormUpdateCustomerMutation.graphql.d.ts +1 -1
  45. package/dist/__generated__/TestFontsFormUpdateCustomerMutation.graphql.js +3 -3
  46. package/dist/__generated__/TypeTesterStandaloneChangedStylesQuery.graphql.d.ts +1 -1
  47. package/dist/__generated__/TypeTesterStandaloneChangedStylesQuery.graphql.js +9 -3
  48. package/dist/__generated__/TypeTesterStandaloneQuery.graphql.d.ts +1 -1
  49. package/dist/__generated__/TypeTesterStandaloneQuery.graphql.js +9 -3
  50. package/dist/__generated__/TypeTester_fontStyle.graphql.d.ts +3 -1
  51. package/dist/__generated__/TypeTester_fontStyle.graphql.js +8 -2
  52. package/dist/__generated__/TypeTestersChangedStylesQuery.graphql.d.ts +1 -1
  53. package/dist/__generated__/TypeTestersChangedStylesQuery.graphql.js +9 -3
  54. package/dist/__generated__/TypeTestersIDQuery.graphql.d.ts +1 -1
  55. package/dist/__generated__/TypeTestersIDQuery.graphql.js +39 -39
  56. package/dist/__generated__/TypeTestersRefetchQuery.graphql.d.ts +1 -1
  57. package/dist/__generated__/TypeTestersRefetchQuery.graphql.js +37 -37
  58. package/dist/__generated__/TypeTestersSlugQuery.graphql.d.ts +1 -1
  59. package/dist/__generated__/TypeTestersSlugQuery.graphql.js +39 -39
  60. package/dist/__tests__/cartDiscountRemoved.test.js +195 -0
  61. package/dist/__tests__/characterViewerCollapse.test.js +199 -0
  62. package/dist/__tests__/characterViewerStyleSelect.test.js +52 -0
  63. package/dist/__tests__/checkoutReturn.test.js +66 -0
  64. package/dist/__tests__/familyStyleCounts.test.js +71 -0
  65. package/dist/__tests__/i18n.test.js +171 -0
  66. package/dist/__tests__/i18nComponents.test.js +408 -0
  67. package/dist/__tests__/localeEnvironments.test.js +275 -0
  68. package/dist/__tests__/localeMiddleware.test.js +68 -0
  69. package/dist/__tests__/localeTransport.test.js +209 -0
  70. package/dist/__tests__/recaptchaLocale.test.js +62 -0
  71. package/dist/__tests__/serverEntryLocale.test.js +124 -0
  72. package/dist/__tests__/storeModalIndexUnlisted.test.js +69 -11
  73. package/dist/__tests__/useSerializablePreloadedQuery.test.js +4 -3
  74. package/dist/components/AddToCartBanner/index.js +8 -2
  75. package/dist/components/BuyButton/index.d.ts +1 -0
  76. package/dist/components/BuyButton/index.js +8 -2
  77. package/dist/components/BuyButton/index.server.d.ts +1 -0
  78. package/dist/components/BuyButton/index.server.js +8 -0
  79. package/dist/components/BuyingOptions/index.js +3 -1
  80. package/dist/components/Cart/AddressFields.js +5 -3
  81. package/dist/components/Cart/CartCouponNotice.d.ts +7 -0
  82. package/dist/components/Cart/CartCouponNotice.js +21 -0
  83. package/dist/components/Cart/CartItem/CartItemAdditionalLicenses.js +3 -1
  84. package/dist/components/Cart/CartItem/CartItemLicense.js +3 -1
  85. package/dist/components/Cart/CartItem/CartItemProduct.js +6 -3
  86. package/dist/components/Cart/CartItem/VariableTextInput.js +7 -3
  87. package/dist/components/Cart/CartItem/index.js +5 -1
  88. package/dist/components/Cart/CartOrder.js +27 -17
  89. package/dist/components/Cart/CartState.d.ts +1 -0
  90. package/dist/components/Cart/CartState.js +18 -3
  91. package/dist/components/Cart/CartTotals.js +17 -9
  92. package/dist/components/Cart/Checkout.js +18 -15
  93. package/dist/components/Cart/CouponCodeInput.js +5 -3
  94. package/dist/components/Cart/CustomerFields.js +6 -4
  95. package/dist/components/Cart/EmptyCart.js +4 -2
  96. package/dist/components/Cart/IdentityBox.js +6 -2
  97. package/dist/components/Cart/ShareCart.js +8 -6
  98. package/dist/components/Cart/checkoutReturn.d.ts +17 -0
  99. package/dist/components/Cart/checkoutReturn.js +38 -0
  100. package/dist/components/CartButton/index.d.ts +2 -1
  101. package/dist/components/CartButton/index.js +6 -2
  102. package/dist/components/CharacterViewer/StyleSelect.js +6 -2
  103. package/dist/components/CharacterViewer/collapse.d.ts +10 -0
  104. package/dist/components/CharacterViewer/collapse.js +39 -0
  105. package/dist/components/CharacterViewer/index.d.ts +4 -0
  106. package/dist/components/CharacterViewer/index.js +78 -24
  107. package/dist/components/CharacterViewer/index.server.d.ts +1 -0
  108. package/dist/components/CharacterViewer/index.server.js +4 -0
  109. package/dist/components/ConfigContext.d.ts +14 -10
  110. package/dist/components/ConfigContext.js +12 -7
  111. package/dist/components/ConsentBanner/index.js +6 -5
  112. package/dist/components/CookieNotification/index.js +11 -5
  113. package/dist/components/CouponText/index.js +15 -7
  114. package/dist/components/CustomerLoginForm/index.js +14 -9
  115. package/dist/components/CustomerSignIn/CustomerSignInElement.d.ts +1 -1
  116. package/dist/components/CustomerSignIn/CustomerSignInElement.js +8 -2
  117. package/dist/components/CustomerSignIn/index.d.ts +6 -1
  118. package/dist/components/CustomerSignIn/index.js +27 -15
  119. package/dist/components/Family/FamilyList.js +5 -2
  120. package/dist/components/Family/index.js +21 -12
  121. package/dist/components/FeatureTester/FeatureTesters.d.ts +2 -1
  122. package/dist/components/FeatureTester/FeatureTesters.js +4 -1
  123. package/dist/components/FeatureTester/FeatureTesters.server.js +4 -0
  124. package/dist/components/FeatureTester/index.d.ts +1 -0
  125. package/dist/components/FeatureTester/index.js +3 -1
  126. package/dist/components/FeatureTester/index.server.js +2 -0
  127. package/dist/components/FontdueContextProvider/index.d.ts +12 -2
  128. package/dist/components/FontdueContextProvider/index.js +85 -11
  129. package/dist/components/FontdueProvider/index.js +7 -1
  130. package/dist/components/FontdueProvider/index.server.js +21 -3
  131. package/dist/components/LocaleContext.d.ts +9 -0
  132. package/dist/components/LocaleContext.js +20 -0
  133. package/dist/components/NewsletterSignup/index.d.ts +2 -1
  134. package/dist/components/NewsletterSignup/index.js +20 -11
  135. package/dist/components/NewsletterSignup/index.server.d.ts +1 -0
  136. package/dist/components/NewsletterSignup/index.server.js +3 -1
  137. package/dist/components/NodePasswordForm/index.d.ts +6 -1
  138. package/dist/components/NodePasswordForm/index.js +14 -6
  139. package/dist/components/OrderVariableSelection/index.js +7 -5
  140. package/dist/components/Precart/License.js +4 -2
  141. package/dist/components/Precart/index.js +15 -12
  142. package/dist/components/Price/index.d.ts +1 -0
  143. package/dist/components/Price/index.js +15 -5
  144. package/dist/components/Root/index.js +2 -1
  145. package/dist/components/Select/index.d.ts +1 -1
  146. package/dist/components/Select/index.js +4 -1
  147. package/dist/components/SelectButton/index.js +3 -1
  148. package/dist/components/SpecimenLink/index.js +3 -1
  149. package/dist/components/StoreModal/StoreModalCart.js +6 -0
  150. package/dist/components/StoreModal/StoreModalContainer.js +14 -12
  151. package/dist/components/StoreModal/StoreModalDownload.js +3 -1
  152. package/dist/components/StoreModal/StoreModalFamily.js +16 -7
  153. package/dist/components/StoreModal/StoreModalFamilyButton.js +14 -9
  154. package/dist/components/StoreModal/StoreModalIndexItem.js +21 -10
  155. package/dist/components/StoreModal/StoreModalIndexPackItem.js +8 -3
  156. package/dist/components/StoreModal/StoreModalPackButton.js +5 -2
  157. package/dist/components/StoreModal/StoreModalProduct.js +14 -5
  158. package/dist/components/StoreModal/index.d.ts +1 -0
  159. package/dist/components/StoreModal/index.js +4 -2
  160. package/dist/components/StoreModal/offered.d.ts +31 -0
  161. package/dist/components/StoreModal/offered.js +21 -0
  162. package/dist/components/StoreModal/types.d.ts +9 -0
  163. package/dist/components/StoreModalProductLicenseSelection/LicenseVariableElement.js +3 -1
  164. package/dist/components/StoreModalProductLicenseSelection/StoreModalProductLicense.js +5 -1
  165. package/dist/components/StoreModalProductSummary/index.js +37 -23
  166. package/dist/components/StripeProvider/index.js +18 -5
  167. package/dist/components/TestFontsForm/index.d.ts +2 -1
  168. package/dist/components/TestFontsForm/index.js +28 -16
  169. package/dist/components/TestFontsForm/index.server.d.ts +1 -0
  170. package/dist/components/TestFontsForm/index.server.js +3 -1
  171. package/dist/components/TestModeBanner/index.js +3 -1
  172. package/dist/components/TypeTester/TypeTesterEditAll.js +3 -1
  173. package/dist/components/TypeTester/TypeTesterFeaturesButton.js +4 -2
  174. package/dist/components/TypeTester/TypeTesterStandalone.d.ts +1 -0
  175. package/dist/components/TypeTester/TypeTesterStandalone.js +6 -2
  176. package/dist/components/TypeTester/TypeTesterStandalone.server.js +2 -0
  177. package/dist/components/TypeTester/TypeTesterStyleSelect.js +5 -3
  178. package/dist/components/TypeTester/TypeTesterStyleSelectData.js +3 -1
  179. package/dist/components/TypeTester/TypeTesterToolbar.js +12 -6
  180. package/dist/components/TypeTester/index.js +7 -2
  181. package/dist/components/TypeTesters/index.d.ts +2 -1
  182. package/dist/components/TypeTesters/index.js +3 -1
  183. package/dist/components/TypeTesters/index.server.d.ts +1 -0
  184. package/dist/components/TypeTesters/index.server.js +4 -0
  185. package/dist/components/elements/EmptyCart/index.js +4 -2
  186. package/dist/components/elements/StoreModalCartLayout/index.d.ts +1 -0
  187. package/dist/components/elements/StoreModalCartLayout/index.js +2 -1
  188. package/dist/components/elements/StoreModalContainer/index.js +4 -2
  189. package/dist/components/elements/StoreModalDownloadLayout/index.js +3 -1
  190. package/dist/components/elements/StoreModalIndexItem/index.js +3 -1
  191. package/dist/components/elements/StoreModalIndexLoadMore/index.js +3 -1
  192. package/dist/components/elements/StoreModalLicenseeIsBillingIdentityElement.js +4 -2
  193. package/dist/components/elements/StoreModalLoadingScreen/index.js +7 -3
  194. package/dist/components/elements/StoreModalUnifiedCheckout.js +44 -27
  195. package/dist/fontdue.css +58 -0
  196. package/dist/hooks/useRecaptchaTimeout.d.ts +1 -1
  197. package/dist/hooks/useRecaptchaTimeout.js +4 -2
  198. package/dist/i18n/de.d.ts +3 -0
  199. package/dist/i18n/de.js +279 -0
  200. package/dist/i18n/en.d.ts +212 -0
  201. package/dist/i18n/en.js +261 -0
  202. package/dist/i18n/index.d.ts +42 -0
  203. package/dist/i18n/index.js +115 -0
  204. package/dist/i18n/types.d.ts +28 -0
  205. package/dist/i18n/types.js +1 -0
  206. package/dist/i18n/useT.d.ts +15 -0
  207. package/dist/i18n/useT.js +21 -0
  208. package/dist/locale.d.ts +20 -0
  209. package/dist/locale.js +52 -0
  210. package/dist/next/tenant.d.ts +3 -1
  211. package/dist/next/tenant.js +11 -2
  212. package/dist/relay/environment.d.ts +8 -2
  213. package/dist/relay/environment.js +65 -15
  214. package/dist/relay/loadSerializableQuery.d.ts +17 -0
  215. package/dist/relay/loadSerializableQuery.js +14 -2
  216. package/dist/relay/serverConfig.d.ts +9 -0
  217. package/dist/relay/serverConfig.js +16 -0
  218. package/dist/relay/useSerializablePreloadedQuery.js +27 -6
  219. package/dist/server/index.d.ts +11 -0
  220. package/dist/server/index.js +7 -0
  221. package/dist/server/middleware.d.ts +19 -1
  222. package/dist/server/middleware.js +56 -13
  223. package/dist/utils.d.ts +0 -1
  224. package/dist/utils.js +0 -5
  225. package/dist/version.js +1 -1
  226. package/package.json +1 -1
@@ -4,6 +4,7 @@ import { resolveFontdueServerConfig } from './serverConfig.js';
4
4
  import { PREVIEW_HEADER, hasPreviewMarkerCookie } from '../preview/constants.js';
5
5
  import { NODE_ACCESS_HEADER } from '../nodeAccess.js';
6
6
  import { CLIENT_VERSION_HEADER, version } from '../version.js';
7
+ import { LOCALE_HEADER, normalizeLocale } from '../locale.js';
7
8
  import { applyCustomerAuthHeader, customerAuthErrorCode, refreshCustomerSession } from '../auth/session.js';
8
9
  import { clearTokens } from '../auth/tokenStore.js';
9
10
 
@@ -130,6 +131,13 @@ export function createNetworkFetch(options) {
130
131
  [CLIENT_VERSION_HEADER]: version
131
132
  };
132
133
 
134
+ // The site's language, when it configured one: explicit (the provider's
135
+ // `locale`, a preload helper's option), else this render's server config.
136
+ // No locale, no header – the request is byte-identical to one from a
137
+ // fontdue-js that predates localisation.
138
+ const locale = resolveRequestLocale(options, serverConfig);
139
+ if (locale) headers[LOCALE_HEADER] = locale;
140
+
133
141
  // Whether this request is an admin *preview*. On the server that's a
134
142
  // forwarded admin token; in the browser it's the readable preview marker
135
143
  // cookie — NOT a bare admin session cookie that happens to ride the fetch
@@ -159,6 +167,14 @@ export function createNetworkFetch(options) {
159
167
  // on-demand `revalidateTag` don't reliably refresh, and the storefront proxy
160
168
  // strips the browser's `no-cache`, so a stale entry can't be busted — leave
161
169
  // dev fetches uncached so every render is fresh. See CACHE_IN_PRODUCTION.
170
+ //
171
+ // The `fontdue-locale` header needs no tag or URL change to keep languages
172
+ // apart in that cache: Next's fetch cache key (IncrementalCache
173
+ // generateCacheKey, next/dist/server/lib/incremental-cache/index.js)
174
+ // hashes the request headers along with the URL, method and body – in Next
175
+ // 14.2, 15.4 and 16.1 alike – so a `de` and a default-language fetch of
176
+ // one query are separate entries. The tags stay locale-free on purpose: a
177
+ // revalidate purges every language of a site at once.
162
178
  const init = {
163
179
  method: 'POST',
164
180
  credentials: 'include',
@@ -276,12 +292,25 @@ export function createNetworkFetch(options) {
276
292
  }
277
293
  };
278
294
  }
295
+
296
+ // The locale a request asks for: the caller's explicit option wins over the
297
+ // render's server config. Blank counts as unset.
298
+ function resolveRequestLocale(options, serverConfig) {
299
+ return normalizeLocale(options === null || options === void 0 ? void 0 : options.locale) ?? normalizeLocale(serverConfig === null || serverConfig === void 0 ? void 0 : serverConfig.locale);
300
+ }
279
301
  export const networkFetch = createNetworkFetch();
280
- export const responseCache = IS_SERVER ? null : new QueryResponseCache({
281
- size: 250,
282
- ttl: CACHE_TTL
283
- });
284
- function createNetwork(options) {
302
+
303
+ // Each client environment has its own response cache (10 s, to resolve
304
+ // preloaded results – see useSerializablePreloadedQuery): the cache is keyed by
305
+ // query and variables only, so a shared one would hand a `de` environment the
306
+ // default-language response. Server environments have none.
307
+ const responseCaches = new WeakMap();
308
+
309
+ /** The response cache of a client environment, or null on the server. */
310
+ export function responseCacheFor(environment) {
311
+ return responseCaches.get(environment) ?? null;
312
+ }
313
+ function createNetwork(options, responseCache) {
285
314
  async function fetchResponse(params, variables, cacheConfig) {
286
315
  const isQuery = params.operationKind === 'query';
287
316
  // @ts-ignore
@@ -299,23 +328,44 @@ function createNetwork(options) {
299
328
  return network;
300
329
  }
301
330
  export function createEnvironment(options) {
302
- return new Environment({
303
- network: createNetwork(options),
331
+ const responseCache = IS_SERVER ? null : new QueryResponseCache({
332
+ size: 250,
333
+ ttl: CACHE_TTL
334
+ });
335
+ const environment = new Environment({
336
+ network: createNetwork(options, responseCache),
304
337
  store: new Store(RecordSource.create()),
305
338
  isServer: IS_SERVER
306
339
  });
340
+ if (responseCache) responseCaches.set(environment, responseCache);
341
+ return environment;
307
342
  }
308
343
 
309
- // Client-side Relay environment is a module-level singleton so multiple
310
- // FontdueProviders on a page (e.g. one per Astro island) share cache and
311
- // data. Server-side stays per-render to avoid cross-request pollution.
312
- let clientEnvironment = null;
344
+ // Client-side Relay environments are module-level singletons, one per locale,
345
+ // so multiple FontdueProviders on a page (e.g. one per Astro island) share
346
+ // cache and data – but a page in `de` and one in the default language never
347
+ // share a store. Relay normalises records by id, so a German and an English
348
+ // `FontCollection:1` in one store would overwrite each other. The key is the
349
+ // normalised locale, '' for none. Each environment keeps the options of the
350
+ // first call that created it, as the single environment always did.
351
+ // Server-side stays per-render to avoid cross-request pollution.
352
+ const clientEnvironments = new Map();
313
353
  export function useCurrentEnvironment(options) {
354
+ const locale = normalizeLocale(options.locale);
314
355
  if (IS_SERVER) {
315
- return createEnvironment(options);
356
+ return createEnvironment({
357
+ ...options,
358
+ locale
359
+ });
316
360
  }
317
- if (clientEnvironment == null) {
318
- clientEnvironment = createEnvironment(options);
361
+ const key = locale ?? '';
362
+ let environment = clientEnvironments.get(key);
363
+ if (environment == null) {
364
+ environment = createEnvironment({
365
+ ...options,
366
+ locale
367
+ });
368
+ clientEnvironments.set(key, environment);
319
369
  }
320
- return clientEnvironment;
370
+ return environment;
321
371
  }
@@ -3,6 +3,13 @@ export interface SerializablePreloadedQuery<TQuery extends OperationType> {
3
3
  params: RequestParameters;
4
4
  variables: VariablesOf<TQuery>;
5
5
  response: GraphQLResponse;
6
+ /**
7
+ * The language the response was fetched in, when one was asked for. The
8
+ * browser commits the payload into that language's Relay environment (see
9
+ * EnsureFontdueContext), never another's. Absent when no locale was set, so
10
+ * a default-language preload serializes exactly as it always did.
11
+ */
12
+ locale?: string;
6
13
  }
7
14
  /** Per-call options shared by every preload helper. */
8
15
  export interface LoadQueryOptions {
@@ -15,6 +22,16 @@ export interface LoadQueryOptions {
15
22
  * token (Astro frontmatter, RR7 loaders — anything that isn't an RSC render).
16
23
  */
17
24
  headers?: Record<string, string>;
25
+ /**
26
+ * The language to fetch in, e.g. `de` (sent as `fontdue-locale`). Falls
27
+ * back to the render's server config (`<FontdueProvider locale>` in RSC,
28
+ * `runWithFontdue(request, next, { locale })` in Astro/RR7). Pass the same
29
+ * locale to the component that renders the preload. In RSC the fallback only
30
+ * covers a full render: a soft navigation re-renders the page segment without
31
+ * the layout's provider, so the react-server entrypoints take this from their
32
+ * own `locale` prop, and a page should pass it.
33
+ */
34
+ locale?: string;
18
35
  }
19
36
  /**
20
37
  * RELAY CACHE KEY NORMALIZATION
@@ -1,4 +1,6 @@
1
1
  import { createNetworkFetch } from './environment.js';
2
+ import { resolveFontdueServerConfig } from './serverConfig.js';
3
+ import { normalizeLocale } from '../locale.js';
2
4
 
3
5
  /** Per-call options shared by every preload helper. */
4
6
 
@@ -43,8 +45,15 @@ import { createNetworkFetch } from './environment.js';
43
45
  // This response will be sent to the client to "warm" the QueryResponseCache
44
46
  // to avoid the client fetches.
45
47
  export default async function loadSerializableQuery(query, variables, options) {
48
+ var _await$resolveFontdue;
46
49
  if (!('params' in query)) throw new Error('Params not found in query, is it a fragment instead of a query?');
47
- const fetcher = createNetworkFetch(options);
50
+ // Resolve the locale here rather than inside the fetch so the payload can
51
+ // record the language it was fetched in.
52
+ const locale = normalizeLocale(options === null || options === void 0 ? void 0 : options.locale) ?? (typeof window === 'undefined' ? normalizeLocale((_await$resolveFontdue = await resolveFontdueServerConfig()) === null || _await$resolveFontdue === void 0 ? void 0 : _await$resolveFontdue.locale) : undefined);
53
+ const fetcher = createNetworkFetch({
54
+ ...options,
55
+ locale
56
+ });
48
57
  const response = await fetcher(query.params, variables);
49
58
 
50
59
  // The result is handed to the browser and, on a prerendered page, frozen
@@ -61,6 +70,9 @@ export default async function loadSerializableQuery(query, variables, options) {
61
70
  return {
62
71
  params: query.params,
63
72
  variables,
64
- response
73
+ response,
74
+ ...(locale ? {
75
+ locale
76
+ } : {})
65
77
  };
66
78
  }
@@ -5,11 +5,20 @@ export interface FontdueServerConfig {
5
5
  headers?: Record<string, string>;
6
6
  /** Extra Next.js fetch cache tags applied to every server-side GraphQL fetch. */
7
7
  cacheTags?: string[];
8
+ /**
9
+ * The language to ask Fontdue for, e.g. `de`. Sent as the `fontdue-locale`
10
+ * header on every server-side GraphQL fetch in the render (the embedded
11
+ * components' preloads and createFontdueFetch alike) and stamped on each
12
+ * preloaded query, so the browser commits it into that language's Relay
13
+ * environment. Unset sends no header: the foundry's default language.
14
+ */
15
+ locale?: string;
8
16
  }
9
17
  type MaybePromise<T> = T | Promise<T>;
10
18
  type AmbientConfigResolver = () => MaybePromise<FontdueServerConfig | undefined>;
11
19
  type NodeAccessRecovery = () => MaybePromise<Record<string, string>>;
12
20
  export declare function setFontdueServerConfig(config: FontdueServerConfig): void;
21
+ export declare function patchFontdueServerConfig(patch: FontdueServerConfig): void;
13
22
  export declare function registerAmbientConfigResolver(resolver: AmbientConfigResolver): void;
14
23
  export declare function registerNodeAccessRecovery(recovery: NodeAccessRecovery): void;
15
24
  export declare function resolveNodeAccessRecovery(): NodeAccessRecovery | undefined;
@@ -72,6 +72,22 @@ export function setFontdueServerConfig(config) {
72
72
  store().getSlot().current = config;
73
73
  }
74
74
 
75
+ // Merge `patch` onto whatever this render's slot already holds, ignoring
76
+ // undefined fields – so the FontdueProvider server entrypoint can add a
77
+ // `locale` to a config a data helper (or __prepareFontdueRender) already set
78
+ // without dropping its url, headers or cache tags. Internal.
79
+ export function patchFontdueServerConfig(patch) {
80
+ const slot = store().getSlot();
81
+ const defined = Object.fromEntries(Object.entries(patch).filter(_ref => {
82
+ let [, value] = _ref;
83
+ return value !== undefined;
84
+ }));
85
+ slot.current = {
86
+ ...slot.current,
87
+ ...defined
88
+ };
89
+ }
90
+
75
91
  // Ambient config resolver — the seam that lets a request-scoped store outside
76
92
  // the RSC slot feed config into server fetches without this browser-safe module
77
93
  // importing a Node builtin. Two registrants today:
@@ -1,10 +1,12 @@
1
1
  // Convert preloaded query object (with raw GraphQL Response) into
2
2
  // Relay's PreloadedQuery.
3
3
 
4
- import { useMemo } from "react";
4
+ import { useContext, useMemo } from "react";
5
5
  import { useRelayEnvironment } from "react-relay";
6
6
  import { createOperationDescriptor, getRequest } from "relay-runtime";
7
- import { responseCache } from "./environment.js";
7
+ import { responseCacheFor } from "./environment.js";
8
+ import LocaleContext from "../components/LocaleContext.js";
9
+
8
10
  // This hook converts a serializable preloaded query into Relay's PreloadedQuery
9
11
  // object. It primes Relay so that `usePreloadedQuery` resolves synchronously on
10
12
  // both server and client:
@@ -13,11 +15,20 @@ import { responseCache } from "./environment.js";
13
15
  // payload directly into the environment's Store via `commitPayload`. This is
14
16
  // required for SSR frameworks (Astro, Vite SSR, etc.) whose non-streaming
15
17
  // `renderToString` needs the data available synchronously during render.
18
+ //
19
+ // Both land in the environment from context, which is the one for the
20
+ // surrounding locale. Self-wrapping components pass `preloadedQuery.locale` to
21
+ // EnsureFontdueContext, which switches to that locale's environment when it
22
+ // differs from the outer one, so a payload fetched in `de` is never committed
23
+ // into the default-language store (Relay normalises by id, so the two would
24
+ // overwrite each other).
16
25
  export default function useSerializablePreloadedQuery(preloadQuery) {
17
26
  let fetchPolicy = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "store-or-network";
18
27
  let query = arguments.length > 2 ? arguments[2] : undefined;
19
28
  const environment = useRelayEnvironment();
29
+ const contextLocale = useContext(LocaleContext);
20
30
  useMemo(() => {
31
+ warnOnLocaleMismatch(preloadQuery, contextLocale);
21
32
  // A preload with no data is not a result to hand Relay: on a page that was
22
33
  // prerendered while the origin was down (older fontdue-js serialized
23
34
  // those) priming the cache with it would make hydration throw "No data
@@ -26,12 +37,12 @@ export default function useSerializablePreloadedQuery(preloadQuery) {
26
37
  const response = preloadQuery.response;
27
38
  const singular = Array.isArray(response) ? response[0] : response;
28
39
  if ((singular === null || singular === void 0 ? void 0 : singular.data) == null) return;
29
- writePreloadedQueryToCache(preloadQuery);
40
+ writePreloadedQueryToCache(environment, preloadQuery);
30
41
  if (query != null) {
31
42
  const operation = createOperationDescriptor(getRequest(query), preloadQuery.variables);
32
43
  environment.commitPayload(operation, singular.data);
33
44
  }
34
- }, [preloadQuery, environment, query]);
45
+ }, [preloadQuery, environment, query, contextLocale]);
35
46
  return {
36
47
  environment,
37
48
  fetchKey: preloadQuery.params.id ?? preloadQuery.params.cacheID,
@@ -45,8 +56,18 @@ export default function useSerializablePreloadedQuery(preloadQuery) {
45
56
  }
46
57
  };
47
58
  }
48
- function writePreloadedQueryToCache(preloadedQueryObject) {
59
+
60
+ // Only reachable through FontdueProvider's own preload (self-wrapping
61
+ // components switch environments instead): a provider given one `locale` and a
62
+ // preload fetched in another. The data still renders, in the preload's
63
+ // language, so say why the page looks mixed.
64
+ function warnOnLocaleMismatch(preloadQuery, contextLocale) {
65
+ if (preloadQuery.locale == null || preloadQuery.locale === contextLocale) return;
66
+ console.warn(`fontdue-js: ${preloadQuery.params.name} was preloaded in "${preloadQuery.locale}" ` + `but rendered under locale ${contextLocale ? `"${contextLocale}"` : "(none)"}. ` + "Pass the same locale to FontdueProvider and the preload.");
67
+ }
68
+ function writePreloadedQueryToCache(environment, preloadedQueryObject) {
69
+ var _responseCacheFor;
49
70
  // @ts-ignore
50
71
  const cacheKey = preloadedQueryObject.params.id ?? preloadedQueryObject.params.cacheID;
51
- responseCache === null || responseCache === void 0 ? void 0 : responseCache.set(cacheKey, preloadedQueryObject.variables, preloadedQueryObject.response);
72
+ (_responseCacheFor = responseCacheFor(environment)) === null || _responseCacheFor === void 0 ? void 0 : _responseCacheFor.set(cacheKey, preloadedQueryObject.variables, preloadedQueryObject.response);
52
73
  }
@@ -1,3 +1,5 @@
1
+ import { LOCALE_HEADER } from '../locale.js';
2
+ export { LOCALE_HEADER };
1
3
  export declare class FontdueNotFoundError extends Error {
2
4
  readonly queryName: string;
3
5
  constructor(queryName: string);
@@ -35,6 +37,15 @@ export interface CreateFontdueFetchOptions {
35
37
  * renders want. Inert outside Next.
36
38
  */
37
39
  cacheTags?: string[];
40
+ /**
41
+ * The language to ask for, e.g. `de`, sent as the `fontdue-locale` header.
42
+ * Falls back to the per-render config's `locale` (set by
43
+ * `<FontdueProvider locale>` in RSC, or `runWithFontdue(request, next,
44
+ * { locale })`). Unset sends no header: the foundry's default language.
45
+ * Next's data cache keys fetches by their headers, so each language is
46
+ * cached separately under the same tags.
47
+ */
48
+ locale?: string;
38
49
  }
39
50
  export type FontdueFetch = <Q, V = void>(queryName: string, query: string, variables?: V) => Promise<Q>;
40
51
  /**
@@ -48,6 +48,8 @@
48
48
  import { resolveFontdueServerConfig, setFontdueServerConfig, resolveNodeAccessRecovery } from '../relay/serverConfig.js';
49
49
  import { PREVIEW_HEADER } from '../preview/constants.js';
50
50
  import { CLIENT_VERSION_HEADER, version } from '../version.js';
51
+ import { LOCALE_HEADER, normalizeLocale } from '../locale.js';
52
+ export { LOCALE_HEADER };
51
53
  function readEnv(name) {
52
54
  if (typeof process !== 'undefined' && process.env) {
53
55
  const v = process.env[name];
@@ -154,6 +156,11 @@ export function createFontdueFetch() {
154
156
  [CLIENT_VERSION_HEADER]: version
155
157
  };
156
158
 
159
+ // The site's language: explicit option, else the per-render config's. No
160
+ // locale, no header – the request is exactly what it was before.
161
+ const locale = normalizeLocale(options.locale) ?? normalizeLocale(config === null || config === void 0 ? void 0 : config.locale);
162
+ if (locale) headers[LOCALE_HEADER] = locale;
163
+
157
164
  // Declare preview intent explicitly: a forwarded admin token means this is a
158
165
  // preview render → reveal hidden fonts; otherwise "false" so a public (or
159
166
  // merely session-authenticated) request never gets the hidden-fonts view.
@@ -1,3 +1,5 @@
1
+ /** The locale set by runWithLocale / runWithFontdue for this request, if any. */
2
+ export declare function ambientLocale(): string | undefined;
1
3
  /** Whether the current async context is an admin preview. */
2
4
  export declare function isPreviewing(): boolean;
3
5
  /**
@@ -7,6 +9,16 @@ export declare function isPreviewing(): boolean;
7
9
  * fetch/preload helpers pick these up automatically and don't need it.
8
10
  */
9
11
  export declare function ambientHeaders(): Record<string, string>;
12
+ /**
13
+ * Run `next` with `locale` (e.g. `de`, from your `/de/…` route) in ambient
14
+ * scope: every fontdue-js server fetch and preload in the render asks Fontdue
15
+ * for that language (the `fontdue-locale` header) and each preload records it,
16
+ * so the browser renders it in the same language. Pass the same locale to
17
+ * `<FontdueProvider locale>` and the islands. Unlike preview, a locale doesn't
18
+ * make the response per-visitor – the URL already differs per language – so
19
+ * the response stays cacheable. An unset or blank locale is a pass-through.
20
+ */
21
+ export declare function runWithLocale(locale: string | undefined, next: () => Response | Promise<Response>): Promise<Response>;
10
22
  /**
11
23
  * Run `next` (your framework's render/loader chain) with the request's admin
12
24
  * preview token in ambient scope, then return its Response. While active, every
@@ -33,5 +45,11 @@ export declare function runWithNodeAccess(request: Request, next: () => Response
33
45
  * single-responsibility middleware. Equivalent to nesting
34
46
  * `runWithPreview(request, () => runWithNodeAccess(request, next))` — mount this
35
47
  * in place of runWithPreview to get the password-unlock forwarding too.
48
+ *
49
+ * `options.locale` adds runWithLocale for a localised route:
50
+ *
51
+ * runWithFontdue(ctx.request, next, { locale: ctx.params.locale });
36
52
  */
37
- export declare function runWithFontdue(request: Request, next: () => Response | Promise<Response>): Promise<Response>;
53
+ export declare function runWithFontdue(request: Request, next: () => Response | Promise<Response>, options?: {
54
+ locale?: string;
55
+ }): Promise<Response>;
@@ -16,7 +16,11 @@
16
16
  //
17
17
  // - runWithPreview — forwards only the admin preview token.
18
18
  // - runWithNodeAccess — forwards only the visitor's node-access token.
19
- // - runWithFontdue — both, composed (runWithPreview ∘ runWithNodeAccess).
19
+ // - runWithFontdue — both, composed (runWithPreview ∘ runWithNodeAccess),
20
+ // plus runWithLocale when given `{ locale }`.
21
+ //
22
+ // runWithLocale is the odd one out: it reads no cookie and keeps the response
23
+ // cacheable – it only carries the route's language to every fetch.
20
24
  //
21
25
  // Mount one of these in your framework's request middleware. A storefront that
22
26
  // wants both features mounts the precomposed runWithFontdue:
@@ -43,21 +47,33 @@ import { AsyncLocalStorage } from 'node:async_hooks';
43
47
  import { readPreviewToken, previewAuthHeaders } from '../preview/index.js';
44
48
  import { nodeAccessHeadersFromCookie } from '../nodeAccess.js';
45
49
  import { registerAmbientConfigResolver } from '../relay/serverConfig.js';
50
+ import { normalizeLocale } from '../locale.js';
46
51
  const ambientStore = new AsyncLocalStorage();
47
52
 
48
- // Feed the ambient headers into fontdue-js's server config resolution. Returns
49
- // undefined outside the context, so plain public requests are untouched.
53
+ // Feed the ambient headers (and locale) into fontdue-js's server config
54
+ // resolution. Returns undefined outside the context, so plain public requests
55
+ // are untouched.
50
56
  registerAmbientConfigResolver(() => {
51
57
  const store = ambientStore.getStore();
52
- return store ? {
58
+ if (!store) return undefined;
59
+ return store.locale ? {
60
+ headers: store.headers,
61
+ locale: store.locale
62
+ } : {
53
63
  headers: store.headers
54
- } : undefined;
64
+ };
55
65
  });
56
66
 
67
+ /** The locale set by runWithLocale / runWithFontdue for this request, if any. */
68
+ export function ambientLocale() {
69
+ var _ambientStore$getStor;
70
+ return (_ambientStore$getStor = ambientStore.getStore()) === null || _ambientStore$getStor === void 0 ? void 0 : _ambientStore$getStor.locale;
71
+ }
72
+
57
73
  /** Whether the current async context is an admin preview. */
58
74
  export function isPreviewing() {
59
- var _ambientStore$getStor;
60
- return ((_ambientStore$getStor = ambientStore.getStore()) === null || _ambientStore$getStor === void 0 ? void 0 : _ambientStore$getStor.previewing) === true;
75
+ var _ambientStore$getStor2;
76
+ return ((_ambientStore$getStor2 = ambientStore.getStore()) === null || _ambientStore$getStor2 === void 0 ? void 0 : _ambientStore$getStor2.previewing) === true;
61
77
  }
62
78
 
63
79
  /**
@@ -67,8 +83,8 @@ export function isPreviewing() {
67
83
  * fetch/preload helpers pick these up automatically and don't need it.
68
84
  */
69
85
  export function ambientHeaders() {
70
- var _ambientStore$getStor2;
71
- return ((_ambientStore$getStor2 = ambientStore.getStore()) === null || _ambientStore$getStor2 === void 0 ? void 0 : _ambientStore$getStor2.headers) ?? {};
86
+ var _ambientStore$getStor3;
87
+ return ((_ambientStore$getStor3 = ambientStore.getStore()) === null || _ambientStore$getStor3 === void 0 ? void 0 : _ambientStore$getStor3.headers) ?? {};
72
88
  }
73
89
  const NO_STORE = 'private, no-store';
74
90
 
@@ -82,19 +98,41 @@ function forceNoStore(response) {
82
98
  }
83
99
 
84
100
  // Run `next` with `patch` merged onto any ambient context an outer middleware
85
- // already set, so runWithPreview ∘ runWithNodeAccess accumulate (headers union,
86
- // previewing OR'd) instead of the inner call clobbering the outer.
101
+ // already set, so runWithPreview ∘ runWithNodeAccess ∘ runWithLocale accumulate
102
+ // (headers union, previewing OR'd, the innermost locale) instead of the inner
103
+ // call clobbering the outer.
87
104
  function withAmbient(patch, next) {
88
105
  const existing = ambientStore.getStore();
106
+ const locale = patch.locale ?? (existing === null || existing === void 0 ? void 0 : existing.locale);
89
107
  return Promise.resolve(ambientStore.run({
90
108
  headers: {
91
109
  ...(existing === null || existing === void 0 ? void 0 : existing.headers),
92
110
  ...patch.headers
93
111
  },
94
- previewing: (existing === null || existing === void 0 ? void 0 : existing.previewing) === true || patch.previewing === true
112
+ previewing: (existing === null || existing === void 0 ? void 0 : existing.previewing) === true || patch.previewing === true,
113
+ ...(locale ? {
114
+ locale
115
+ } : {})
95
116
  }, next));
96
117
  }
97
118
 
119
+ /**
120
+ * Run `next` with `locale` (e.g. `de`, from your `/de/…` route) in ambient
121
+ * scope: every fontdue-js server fetch and preload in the render asks Fontdue
122
+ * for that language (the `fontdue-locale` header) and each preload records it,
123
+ * so the browser renders it in the same language. Pass the same locale to
124
+ * `<FontdueProvider locale>` and the islands. Unlike preview, a locale doesn't
125
+ * make the response per-visitor – the URL already differs per language – so
126
+ * the response stays cacheable. An unset or blank locale is a pass-through.
127
+ */
128
+ export async function runWithLocale(locale, next) {
129
+ const normalized = normalizeLocale(locale);
130
+ if (!normalized) return next();
131
+ return withAmbient({
132
+ locale: normalized
133
+ }, next);
134
+ }
135
+
98
136
  /**
99
137
  * Run `next` (your framework's render/loader chain) with the request's admin
100
138
  * preview token in ambient scope, then return its Response. While active, every
@@ -138,7 +176,12 @@ export async function runWithNodeAccess(request, next) {
138
176
  * single-responsibility middleware. Equivalent to nesting
139
177
  * `runWithPreview(request, () => runWithNodeAccess(request, next))` — mount this
140
178
  * in place of runWithPreview to get the password-unlock forwarding too.
179
+ *
180
+ * `options.locale` adds runWithLocale for a localised route:
181
+ *
182
+ * runWithFontdue(ctx.request, next, { locale: ctx.params.locale });
141
183
  */
142
184
  export async function runWithFontdue(request, next) {
143
- return runWithPreview(request, () => runWithNodeAccess(request, next));
185
+ let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
186
+ return runWithPreview(request, () => runWithNodeAccess(request, () => runWithLocale(options.locale, next)));
144
187
  }
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export { isSelected } from './selection.js';
2
- export declare const pluralize: (count: number, singular: string, plural?: string) => string;
3
2
  export declare function notEmpty<TValue>(value: TValue | null | undefined): value is TValue;
4
3
  export declare function kebabToCamel(str: string): string;
5
4
  export type VariableSettings = {
package/dist/utils.js CHANGED
@@ -1,9 +1,4 @@
1
1
  export { isSelected } from './selection.js';
2
- export const pluralize = (count, singular, plural) => {
3
- if (count === 1) return `1 ${singular}`;
4
- if (!plural) return `${count} ${singular}s`;
5
- return `${count}\xa0${plural}`;
6
- };
7
2
  export function notEmpty(value) {
8
3
  return value !== null && value !== undefined;
9
4
  }
package/dist/version.js CHANGED
@@ -8,4 +8,4 @@
8
8
  // (Fontage.ClientVersions) to spot sites that need to upgrade before a
9
9
  // feature that depends on a newer client can work for them.
10
10
  export const CLIENT_VERSION_HEADER = 'fontdue-client-version';
11
- export const version = "3.14.0";
11
+ export const version = "3.15.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fontdue-js",
3
- "version": "3.14.0",
3
+ "version": "3.15.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "npm run relay && run-p build-js build-css build-ts",