Skip to content

Commit 1cac9a6

Browse files
authored
fix: Add workaround for OpenTelemetry/Zone.js (#1719)
Fixes #1711 Available as `[email protected]`
1 parent d3271f9 commit 1cac9a6

File tree

7 files changed

+24
-15
lines changed

7 files changed

+24
-15
lines changed

packages/next-intl/.size-limit.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const config: SizeLimitConfig = [
99
{
1010
name: "import * from 'next-intl' (react-server)",
1111
path: 'dist/production/index.react-server.js',
12-
limit: '14.735 KB'
12+
limit: '15.355 KB'
1313
},
1414
{
1515
name: "import {createSharedPathnamesNavigation} from 'next-intl/navigation' (react-client)",
@@ -21,13 +21,13 @@ const config: SizeLimitConfig = [
2121
name: "import {createLocalizedPathnamesNavigation} from 'next-intl/navigation' (react-client)",
2222
path: 'dist/production/navigation.react-client.js',
2323
import: '{createLocalizedPathnamesNavigation}',
24-
limit: '4.115 KB'
24+
limit: '4.125 KB'
2525
},
2626
{
2727
name: "import {createNavigation} from 'next-intl/navigation' (react-client)",
2828
path: 'dist/production/navigation.react-client.js',
2929
import: '{createNavigation}',
30-
limit: '4.115 KB'
30+
limit: '4.125 KB'
3131
},
3232
{
3333
name: "import {createSharedPathnamesNavigation} from 'next-intl/navigation' (react-server)",
@@ -55,7 +55,7 @@ const config: SizeLimitConfig = [
5555
{
5656
name: "import * from 'next-intl/server' (react-server)",
5757
path: 'dist/production/server.react-server.js',
58-
limit: '14.035 KB'
58+
limit: '14.635 KB'
5959
},
6060
{
6161
name: "import createMiddleware from 'next-intl/middleware'",

packages/next-intl/__mocks__/react.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import {isPromise} from '../src/shared/utils';
2+
13
// @ts-expect-error -- React uses CJS
24
export * from 'react';
35

46
export {default} from 'react';
57

68
export function use(promise: Promise<unknown> & {value?: unknown}) {
7-
if (!(promise instanceof Promise)) {
9+
if (!isPromise(promise)) {
810
throw new Error('Expected a promise, got ' + typeof promise);
911
}
1012

packages/next-intl/src/navigation/shared/createSharedNavigationFns.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
Pathnames
1717
} from '../../routing/types';
1818
import {ParametersExceptFirst, Prettify} from '../../shared/types';
19-
import {isLocalizableHref} from '../../shared/utils';
19+
import {isLocalizableHref, isPromise} from '../../shared/utils';
2020
import BaseLink from './BaseLink';
2121
import {
2222
HrefOrHrefWithParams,
@@ -111,10 +111,9 @@ export default function createSharedNavigationFns<
111111
const isLocalizable = isLocalizableHref(href);
112112

113113
const localePromiseOrValue = getLocale();
114-
const curLocale =
115-
localePromiseOrValue instanceof Promise
116-
? use(localePromiseOrValue)
117-
: localePromiseOrValue;
114+
const curLocale = isPromise(localePromiseOrValue)
115+
? use(localePromiseOrValue)
116+
: localePromiseOrValue;
118117

119118
const finalPathname = isLocalizable
120119
? getPathname(

packages/next-intl/src/react-server/index.test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import {describe, expect, it, vi} from 'vitest';
33
import {getTranslations} from '../server.react-server';
4+
import {isPromise} from '../shared/utils';
45
import {renderToStream} from './testUtils';
56
import {
67
_createCache,
@@ -73,7 +74,7 @@ describe('performance', () => {
7374
try {
7475
useTranslations('Component');
7576
} catch (promiseOrError) {
76-
if (promiseOrError instanceof Promise) {
77+
if (isPromise(promiseOrError)) {
7778
await promiseOrError;
7879
useTranslations('Component');
7980
} else {

packages/next-intl/src/server/react-server/RequestLocale.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import {headers} from 'next/headers';
22
import {cache} from 'react';
33
import {HEADER_LOCALE_NAME} from '../../shared/constants';
4+
import {isPromise} from '../../shared/utils';
45
import {getCachedRequestLocale} from './RequestLocaleCache';
56

67
async function getHeadersImpl(): Promise<Headers> {
78
const promiseOrValue = headers();
89

910
// Compatibility with Next.js <15
10-
return promiseOrValue instanceof Promise
11-
? await promiseOrValue
12-
: promiseOrValue;
11+
return isPromise(promiseOrValue) ? await promiseOrValue : promiseOrValue;
1312
}
1413
const getHeaders = cache(getHeadersImpl);
1514

packages/next-intl/src/server/react-server/getConfig.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
_createIntlFormatters,
77
initializeConfig
88
} from 'use-intl/core';
9+
import {isPromise} from '../../shared/utils';
910
import {getRequestLocale} from './RequestLocale';
1011
import {getRequestLocale as getRequestLocaleLegacy} from './RequestLocaleLegacy';
1112
import createRequestConfig from './createRequestConfig';
@@ -72,7 +73,7 @@ See also: https://next-intl.dev/docs/usage/configuration#i18n-request
7273
};
7374

7475
let result = getConfig(params);
75-
if (result instanceof Promise) {
76+
if (isPromise(result)) {
7677
result = await result;
7778
}
7879

packages/next-intl/src/shared/utils.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,10 @@ function comparePathnamePairs(a: string, b: string): number {
233233
export function getSortedPathnames(pathnames: Array<string>) {
234234
return pathnames.sort(comparePathnamePairs);
235235
}
236+
237+
export function isPromise<Value>(
238+
value: Value | Promise<Value>
239+
): value is Promise<Value> {
240+
// https://github.com/amannn/next-intl/issues/1711
241+
return typeof (value as any).then === 'function';
242+
}

0 commit comments

Comments
 (0)