@inlang/paraglide-js 2.3.0 → 2.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.
@@ -6,16 +6,19 @@
6
6
  * reloading is disabled, you need to ensure that the UI is updated
7
7
  * to reflect the new locale.
8
8
  *
9
+ * If any custom strategy's \`setLocale\` function is async, then this
10
+ * function will become async as well.
11
+ *
9
12
  * @example
10
13
  * setLocale('en');
11
14
  *
12
15
  * @example
13
16
  * setLocale('en', { reload: false });
14
17
  *
15
- * @type {(newLocale: Locale, options?: { reload?: boolean }) => void}
18
+ * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<void> | void}
16
19
  */
17
20
  export let setLocale: (newLocale: Locale, options?: {
18
21
  reload?: boolean;
19
- }) => void;
22
+ }) => Promise<void> | void;
20
23
  export function overwriteSetLocale(fn: (newLocale: Locale) => void): void;
21
24
  //# sourceMappingURL=set-locale.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;GAeG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,IAAI,CA8FnE;AAgBK,uCAFI,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,QAIrC"}
1
+ {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAgCA;;;;;;;;;;;;;;;;;;GAkBG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAmGnF;AAgBK,uCAFI,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,QAIrC"}
@@ -2,6 +2,22 @@ import { getLocale } from "./get-locale.js";
2
2
  import { localizeUrl } from "./localize-url.js";
3
3
  import { customClientStrategies, isCustomStrategy } from "./strategy.js";
4
4
  import { cookieDomain, cookieMaxAge, cookieName, isServer, localStorageKey, strategy, TREE_SHAKE_COOKIE_STRATEGY_USED, TREE_SHAKE_GLOBAL_VARIABLE_STRATEGY_USED, TREE_SHAKE_LOCAL_STORAGE_STRATEGY_USED, TREE_SHAKE_URL_STRATEGY_USED, } from "./variables.js";
5
+ /**
6
+ * Navigates to the localized URL, or reloads the current page
7
+ *
8
+ * @param {string} [newLocation] The new location
9
+ * @return {undefined}
10
+ */
11
+ const navigateOrReload = (newLocation) => {
12
+ if (newLocation) {
13
+ // reload the page by navigating to the new url
14
+ window.location.href = newLocation;
15
+ }
16
+ else {
17
+ // reload the page to reflect the new locale
18
+ window.location.reload();
19
+ }
20
+ };
5
21
  /**
6
22
  * Set the locale.
7
23
  *
@@ -10,13 +26,16 @@ import { cookieDomain, cookieMaxAge, cookieName, isServer, localStorageKey, stra
10
26
  * reloading is disabled, you need to ensure that the UI is updated
11
27
  * to reflect the new locale.
12
28
  *
29
+ * If any custom strategy's \`setLocale\` function is async, then this
30
+ * function will become async as well.
31
+ *
13
32
  * @example
14
33
  * setLocale('en');
15
34
  *
16
35
  * @example
17
36
  * setLocale('en', { reload: false });
18
37
  *
19
- * @type {(newLocale: Locale, options?: { reload?: boolean }) => void}
38
+ * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<void> | void}
20
39
  */
21
40
  export let setLocale = (newLocale, options) => {
22
41
  const optionsWithDefaults = {
@@ -32,6 +51,8 @@ export let setLocale = (newLocale, options) => {
32
51
  catch {
33
52
  // do nothing, no locale has been set yet.
34
53
  }
54
+ /** @type {Array<Promise<any>>} */
55
+ const customSetLocalePromises = [];
35
56
  /** @type {string | undefined} */
36
57
  let newLocation = undefined;
37
58
  for (const strat of strategy) {
@@ -85,8 +106,9 @@ export let setLocale = (newLocale, options) => {
85
106
  // Handle async setLocale - fire and forget
86
107
  if (result instanceof Promise) {
87
108
  result.catch((error) => {
88
- console.warn(`Custom strategy "${strat}" setLocale failed:`, error);
109
+ error.message = `Custom strategy "${strat}" setLocale failed: ${error.message}`;
89
110
  });
111
+ customSetLocalePromises.push(result);
90
112
  }
91
113
  }
92
114
  }
@@ -95,13 +117,14 @@ export let setLocale = (newLocale, options) => {
95
117
  optionsWithDefaults.reload &&
96
118
  window.location &&
97
119
  newLocale !== currentLocale) {
98
- if (newLocation) {
99
- // reload the page by navigating to the new url
100
- window.location.href = newLocation;
120
+ if (customSetLocalePromises.length) {
121
+ // Wait for any async custom setLocale functions
122
+ return Promise.all(customSetLocalePromises).then(() => {
123
+ navigateOrReload(newLocation);
124
+ });
101
125
  }
102
126
  else {
103
- // reload the page to reflect the new locale
104
- window.location.reload();
127
+ navigateOrReload(newLocation);
105
128
  }
106
129
  }
107
130
  return;
@@ -317,6 +317,102 @@ test("calls setLocale on multiple custom strategies", async () => {
317
317
  expect(customLocale1).toBe("de");
318
318
  expect(customLocale2).toBe("de");
319
319
  });
320
+ test("awaits async setLocale functions to resolve in custom strategy", async () => {
321
+ let customLocale1 = "en";
322
+ globalThis.window = {
323
+ location: {
324
+ reload: vi.fn(),
325
+ },
326
+ };
327
+ const runtime = await createParaglide({
328
+ blob: await newProject({
329
+ settings: {
330
+ baseLocale: "en",
331
+ locales: ["en", "fr", "de"],
332
+ },
333
+ }),
334
+ strategy: ["custom-async", "baseLocale"],
335
+ isServer: "false",
336
+ });
337
+ runtime.defineCustomClientStrategy("custom-async", {
338
+ getLocale: () => customLocale1,
339
+ setLocale: async (locale) => {
340
+ customLocale1 = locale;
341
+ },
342
+ });
343
+ const setLocalePromise = runtime.setLocale("de");
344
+ expect(window.location.reload).not.toHaveBeenCalled();
345
+ await setLocalePromise;
346
+ // Verify that setLocale resolved before reload was called
347
+ expect(window.location.reload).toHaveBeenCalledTimes(1);
348
+ expect(customLocale1).toBe("de");
349
+ });
350
+ test("awaits async setLocale functions to resolve in multiple custom strategies", async () => {
351
+ let customLocale1 = "en";
352
+ let customLocale2 = "en";
353
+ globalThis.window = {
354
+ location: {
355
+ reload: vi.fn(),
356
+ },
357
+ };
358
+ const runtime = await createParaglide({
359
+ blob: await newProject({
360
+ settings: {
361
+ baseLocale: "en",
362
+ locales: ["en", "fr", "de"],
363
+ },
364
+ }),
365
+ strategy: ["custom-async1", "custom-async2", "baseLocale"],
366
+ isServer: "false",
367
+ });
368
+ runtime.defineCustomClientStrategy("custom-async1", {
369
+ getLocale: () => customLocale1,
370
+ setLocale: async (locale) => {
371
+ customLocale1 = locale;
372
+ },
373
+ });
374
+ runtime.defineCustomClientStrategy("custom-async2", {
375
+ getLocale: () => customLocale2,
376
+ setLocale: async (locale) => {
377
+ customLocale2 = locale;
378
+ },
379
+ });
380
+ const setLocalePromise = runtime.setLocale("de");
381
+ expect(window.location.reload).not.toHaveBeenCalled();
382
+ await setLocalePromise;
383
+ // Verify that setLocale resolved before reload was called
384
+ expect(window.location.reload).toHaveBeenCalledTimes(1);
385
+ expect(customLocale1).toBe("de");
386
+ expect(customLocale2).toBe("de");
387
+ });
388
+ test("reload should not run if async setLocale function rejects in custom strategy", async () => {
389
+ const customLocale1 = "en";
390
+ globalThis.window = {
391
+ location: {
392
+ reload: vi.fn(),
393
+ },
394
+ };
395
+ const runtime = await createParaglide({
396
+ blob: await newProject({
397
+ settings: {
398
+ baseLocale: "en",
399
+ locales: ["en", "fr", "de"],
400
+ },
401
+ }),
402
+ strategy: ["custom-async", "baseLocale"],
403
+ isServer: "false",
404
+ });
405
+ runtime.defineCustomClientStrategy("custom-async", {
406
+ getLocale: () => customLocale1,
407
+ setLocale: async () => {
408
+ throw new Error("fetch error");
409
+ },
410
+ });
411
+ await expect(() => runtime.setLocale("de")).rejects.toThrowError(`Custom strategy "custom-async" setLocale failed: fetch error`);
412
+ // Verify that reload was never called
413
+ expect(window.location.reload).toHaveBeenCalledTimes(0);
414
+ expect(customLocale1).toBe("en");
415
+ });
320
416
  test("custom strategy setLocale works with cookie and localStorage", async () => {
321
417
  let customData = "en";
322
418
  globalThis.document = { cookie: "" };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inlang/paraglide-js",
3
3
  "type": "module",
4
- "version": "2.3.0",
4
+ "version": "2.3.1",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public",