-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve performance when calling hooks like
useTranslations
in…
… Server Components by making sure we only suspend when i18n config is initially loaded and never for subsequent calls (#741) Fixes #734 by making sure we suspend only once when loading configuration. See also https://github.com/acdlite/rfcs/blob/first-class-promises/text/0000-first-class-support-for-promises.md#resuming-a-suspended-component-by-replaying-its-execution
- Loading branch information
Showing
24 changed files
with
459 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @ts-expect-error -- React uses CJS | ||
export * from 'react'; | ||
|
||
export {default} from 'react'; | ||
|
||
export function use(promise: Promise<unknown> & {value?: unknown}) { | ||
if (!(promise instanceof Promise)) { | ||
throw new Error('Expected a promise, got ' + typeof promise); | ||
} | ||
|
||
if (promise.value) { | ||
return promise.value; | ||
} else { | ||
throw promise.then((value) => { | ||
promise.value = value; | ||
return promise; | ||
}); | ||
} | ||
} | ||
|
||
const cached = {} as Record<string, unknown>; | ||
|
||
export function cache(fn: (...args: Array<unknown>) => unknown) { | ||
if (!fn.name) { | ||
throw new Error('Expected a named function for easier debugging'); | ||
} | ||
|
||
function cachedFn(...args: Array<unknown>) { | ||
const key = `${fn.name}(${args | ||
.map((arg) => JSON.stringify(arg)) | ||
.join(', ')})`; | ||
|
||
if (cached[key]) { | ||
return cached[key]; | ||
} else { | ||
const result = fn(...args); | ||
cached[key] = result; | ||
return result; | ||
} | ||
} | ||
|
||
return cachedFn; | ||
} | ||
|
||
cache.reset = () => { | ||
Object.keys(cached).forEach((key) => { | ||
delete cached[key]; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import type {useFormatter as useFormatterType} from 'use-intl'; | ||
import getFormatter from '../server/react-server/getFormatter'; | ||
import useHook from './useHook'; | ||
import useLocale from './useLocale'; | ||
import {cache} from 'react'; | ||
import {type useFormatter as useFormatterType} from 'use-intl'; | ||
import {createFormatter} from 'use-intl/core'; | ||
import useConfig from './useConfig'; | ||
|
||
const createFormatterCached = cache(createFormatter); | ||
|
||
export default function useFormatter( | ||
// eslint-disable-next-line no-empty-pattern | ||
...[]: Parameters<typeof useFormatterType> | ||
): ReturnType<typeof useFormatterType> { | ||
const locale = useLocale(); | ||
return useHook('useFormatter', getFormatter({locale})); | ||
const config = useConfig('useFormatter'); | ||
return createFormatterCached(config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
import type {useMessages as useMessagesType} from 'use-intl'; | ||
import getMessages from '../server/react-server/getMessages'; | ||
import useHook from './useHook'; | ||
import useLocale from './useLocale'; | ||
import {getMessagesFromConfig} from '../server/react-server/getMessages'; | ||
import useConfig from './useConfig'; | ||
|
||
export default function useMessages( | ||
// eslint-disable-next-line no-empty-pattern | ||
...[]: Parameters<typeof useMessagesType> | ||
): ReturnType<typeof useMessagesType> { | ||
const locale = useLocale(); | ||
return useHook('useMessages', getMessages({locale})); | ||
const config = useConfig('useMessages'); | ||
return getMessagesFromConfig(config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import type {useTimeZone as useTimeZoneType} from 'use-intl'; | ||
import getTimeZone from '../server/react-server/getTimeZone'; | ||
import useHook from './useHook'; | ||
import useLocale from './useLocale'; | ||
import useConfig from './useConfig'; | ||
|
||
export default function useTimeZone( | ||
// eslint-disable-next-line no-empty-pattern | ||
...[]: Parameters<typeof useTimeZoneType> | ||
): ReturnType<typeof useTimeZoneType> { | ||
const locale = useLocale(); | ||
return useHook('useTimeZone', getTimeZone({locale})); | ||
const config = useConfig('useTimeZone'); | ||
return config.timeZone; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,10 @@ | ||
import type {useTranslations as useTranslationsType} from 'use-intl'; | ||
import getBaseTranslator from './getBaseTranslator'; | ||
import useHook from './useHook'; | ||
import useLocale from './useLocale'; | ||
import getBaseTranslator from './getTranslator'; | ||
import useConfig from './useConfig'; | ||
|
||
export default function useTranslations( | ||
...[namespace]: Parameters<typeof useTranslationsType> | ||
): ReturnType<typeof useTranslationsType> { | ||
const locale = useLocale(); | ||
|
||
const result = useHook( | ||
'useTranslations', | ||
getBaseTranslator(locale, namespace) | ||
); | ||
|
||
return result; | ||
const config = useConfig('useTranslations'); | ||
return getBaseTranslator(config, namespace); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
4185a72
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-intl-example-app-router – ./examples/example-app-router
next-intl-example-app-router-git-main-next-intl.vercel.app
next-intl-example-next-13.vercel.app
next-intl-example-app-router-next-intl.vercel.app
next-intl-example-app-router.vercel.app
4185a72
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
next-intl-docs – ./docs
next-intl-docs.vercel.app
next-intl-docs-next-intl.vercel.app
next-intl-docs-git-main-next-intl.vercel.app