Skip to content

Commit

Permalink
fix: Allow to call getRequestConfig in outer module closure in a Cl…
Browse files Browse the repository at this point in the history
…ient Component module graph (#687)

Fixes #685 

This is fixed for compatibility, since we've silently supported this
pattern previously. However, it's still not recommended to call
`getRequestConfig` in a Client Component module graph as the function
can't do anything useful there. Instead, you can extract shared config
like `locales` to a file like
[`src/navigation.ts`](https://next-intl-docs.vercel.app/docs/routing/navigation)
or any other module that is then imported into Client and Server
Components.
  • Loading branch information
amannn committed Dec 4, 2023
1 parent 64b24c2 commit 0f16f10
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/next-intl/src/server/react-client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import type {
getRequestConfig as getRequestConfig_type,
getFormatter as getFormatter_type,
getNow as getNow_type,
getTimeZone as getTimeZone_type,
getTranslations as getTranslations_type,
getMessages as getMessages_type,
getLocale as getLocale_type,
unstable_setRequestLocale as unstable_setRequestLocale_type
} from '../react-server';

/**
* Allows to import `next-intl/server` in non-RSC environments.
*
Expand All @@ -12,14 +23,27 @@ function notSupported(message: string) {
};
}

export const getRequestConfig = notSupported('getRequestConfig');
export const getFormatter = notSupported('getFormatter');
export const getNow = notSupported('getNow');
export const getTimeZone = notSupported('getTimeZone');
export const getTranslations = notSupported('getTranslations');
export const getMessages = notSupported('getMessages');
export const getLocale = notSupported('getLocale');
export function getRequestConfig(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...args: Parameters<typeof getRequestConfig_type>
): ReturnType<typeof getRequestConfig_type> {
return notSupported('getRequestConfig');
}
export const getFormatter = notSupported(
'getFormatter'
) as typeof getFormatter_type;
export const getNow = notSupported('getNow') as typeof getNow_type;
export const getTimeZone = notSupported(
'getTimeZone'
) as typeof getTimeZone_type;
export const getTranslations = notSupported(
'getTranslations'
) as typeof getTranslations_type;
export const getMessages = notSupported(
'getMessages'
) as typeof getMessages_type;
export const getLocale = notSupported('getLocale') as typeof getLocale_type;

export const unstable_setRequestLocale = notSupported(
'unstable_setRequestLocale'
);
) as typeof unstable_setRequestLocale_type;
21 changes: 21 additions & 0 deletions packages/next-intl/test/server/react-client/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {describe, expect, it} from 'vitest';
import {getRequestConfig} from '../../../src/server.react-client';

describe('getRequestConfig', () => {
it('can be called in the outer module closure', () => {
expect(
getRequestConfig(({locale}) => ({
messages: {hello: 'Hello ' + locale}
}))
);
});

it('can not call the returned function', () => {
const getConfig = getRequestConfig(({locale}) => ({
messages: {hello: 'Hello ' + locale}
}));
expect(() => getConfig({locale: 'en'})).toThrow(
'`getRequestConfig` is not supported in Client Components.'
);
});
});

2 comments on commit 0f16f10

@vercel
Copy link

@vercel vercel bot commented on 0f16f10 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 0f16f10 Dec 4, 2023

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-next-intl.vercel.app
next-intl-docs-git-main-next-intl.vercel.app
next-intl-docs.vercel.app

Please sign in to comment.