-
-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Next.js 13 RSC integration #139
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
.DS_Store | ||
node_modules | ||
dist | ||
.vscode |
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,10 +1,14 @@ | ||
require('eslint-config-molindo/setupPlugins'); | ||
|
||
module.exports = { | ||
extends: ['molindo/typescript', 'molindo/react', 'plugin:@next/next/recommended'], | ||
extends: [ | ||
'molindo/typescript', | ||
'molindo/react', | ||
'plugin:@next/next/recommended' | ||
], | ||
rules: { | ||
'react/react-in-jsx-scope': 'off', | ||
'jsx-a11y/anchor-is-valid': 'off', | ||
'react/display-name': 'off' | ||
} | ||
} | ||
}; |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
/.next/ | ||
.DS_Store | ||
tsconfig.tsbuildinfo | ||
.vscode |
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,6 +1,3 @@ | ||
module.exports = { | ||
i18n: { | ||
locales: ['en', 'de'], | ||
defaultLocale: 'en' | ||
} | ||
} | ||
experimental: {appDir: true} | ||
}; |
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,15 @@ | ||
'use client'; | ||
import {NextIntlProvider} from 'next-intl'; | ||
import {ReactNode} from 'react'; | ||
|
||
type Props = { | ||
children: ReactNode; | ||
locale: string; | ||
}; | ||
|
||
export default function Provider({children, locale}: Props) { | ||
console.log('Provider'); | ||
|
||
// return children; | ||
return <NextIntlProvider locale={locale}>{children}</NextIntlProvider>; | ||
} |
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,17 @@ | ||
import {ReactNode} from 'react'; | ||
import NextIntlProvider from 'next-intl/NextIntlProvider'; | ||
import Storage from '../../next-intl/IntlStorage'; | ||
|
||
type Props = { | ||
children: ReactNode; | ||
params: { | ||
locale: string; | ||
}; | ||
}; | ||
|
||
export default function LocaleLayout({children, params: {locale}}: Props) { | ||
const now = new Date().toISOString(); | ||
Storage.set({now, locale}); | ||
|
||
return <NextIntlProvider locale={locale}>{children}</NextIntlProvider>; | ||
} |
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,42 @@ | ||
import Storage from '../../next-intl/IntlStorage'; | ||
|
||
export function generateStaticParams() { | ||
return ['de', 'en'].map((locale) => ({locale})); | ||
} | ||
|
||
export default function Index() { | ||
const value = Storage.get(); | ||
|
||
return ( | ||
<p> | ||
Hello {value.locale} ({value.now}) | ||
</p> | ||
); | ||
} | ||
|
||
// import {useTranslations} from 'next-intl'; | ||
// import LocaleSwitcher from 'components/LocaleSwitcher'; | ||
// import PageLayout from 'components/PageLayout'; | ||
|
||
// import {useContext} from 'react'; | ||
// import ServerOnlyContext from './ServerOnlyContext'; | ||
|
||
// export default function Index() { | ||
// // TODO: Use middleware to redirect to a specific locale | ||
|
||
// const serverOnly = useContext(ServerOnlyContext); | ||
// // const t = useTranslations('Index'); | ||
|
||
// console.log('Index'); | ||
|
||
// // return <p>{t('title')}</p>; | ||
|
||
// return <p>Hello {serverOnly.only.for.server + 10}</p>; | ||
|
||
// // return ( | ||
// // <PageLayout title={t('title')}> | ||
// // <p>{t('description')}</p> | ||
// // <LocaleSwitcher /> | ||
// // </PageLayout> | ||
// // ); | ||
// } |
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,23 @@ | ||
import {headers} from 'next/headers'; | ||
import {ReactNode} from 'react'; | ||
|
||
type Props = { | ||
children: ReactNode; | ||
}; | ||
|
||
export default function RootLayout({children, ...rest}: Props) { | ||
// console.log(headers()); | ||
|
||
// How to get this from the URL? | ||
// TODO: Validate locale or redirect to default locale | ||
const locale = 'en'; | ||
|
||
return ( | ||
<html lang={locale}> | ||
<head> | ||
<title>next-intl example</title> | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
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,4 @@ | ||
export default { | ||
locales: ['en', 'de'], | ||
defaultLocale: 'en' | ||
}; |
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,12 @@ | ||
import {NextRequest, NextResponse} from 'next/server'; | ||
import resolveLocale from 'next-intl/resolveLocale'; | ||
import i18n from './i18n'; | ||
|
||
export function middleware(request: NextRequest) { | ||
const locale = resolveLocale(request.headers, i18n); | ||
return NextResponse.redirect(new URL('/' + locale, request.url)); | ||
} | ||
|
||
export const config = { | ||
matcher: '/' | ||
}; |
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,30 @@ | ||
import {requestAsyncStorage} from 'next/dist/client/components/request-async-storage'; | ||
|
||
const key = '__next-intl'; | ||
|
||
/** | ||
* Returns the request-level storage of Next.js where typically headers | ||
* and cookies are stored. This is recreated for every request. | ||
* | ||
* This uses internal APIs of Next.js and may break in | ||
* the future, so we should really move away from this. | ||
*/ | ||
function getStorage() { | ||
const requestStore = | ||
requestAsyncStorage && 'getStore' in requestAsyncStorage | ||
? requestAsyncStorage.getStore()! | ||
: requestAsyncStorage; | ||
|
||
return requestStore; | ||
} | ||
|
||
export default { | ||
set(value: any) { | ||
const storage = getStorage(); | ||
(storage as any)[key] = value; | ||
}, | ||
get() { | ||
const storage = getStorage(); | ||
return (storage as any)[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'server-only'; | ||
import {createServerContext, ReactNode} from 'react'; | ||
|
||
// This is always passed to the client, regardless of if it's read from a client component! | ||
// Interestingly the module code is not there, but the context value. | ||
const ServerOnlyContext = createServerContext('serverOnly', 'initialValue'); | ||
|
||
export default function NextIntlProvider({ | ||
children, | ||
locale | ||
}: { | ||
children: ReactNode; | ||
locale: string; | ||
}) { | ||
return ( | ||
<ServerOnlyContext.Provider value={{locale}}> | ||
{children} | ||
</ServerOnlyContext.Provider> | ||
); | ||
} |
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,2 @@ | ||
export {default as IntlStorage} from './IntlStorage'; | ||
export {default as resolveLocale} from './resolveLocale'; |
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,19 @@ | ||
import acceptLanguageParser from 'accept-language-parser'; | ||
|
||
type I18nConfig = { | ||
locales: Array<string>; | ||
defaultLocale: string; | ||
}; | ||
|
||
export default function resolveLocale( | ||
requestHeaders: Headers, | ||
i18n: I18nConfig | ||
) { | ||
const locale = | ||
acceptLanguageParser.pick( | ||
i18n.locales, | ||
requestHeaders.get('accept-language') || i18n.defaultLocale | ||
) || i18n.defaultLocale; | ||
|
||
return locale; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe wrap all pages under optional
[[locale]]
then get usingparams
https://beta.nextjs.org/docs/routing/defining-routes#example
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.
Hey and thank you for your interest! Yep, that seems to work, I've experimented with it here:
next-intl/packages/example/src/app/[locale]/page.tsx
Lines 11 to 15 in ccb9c8a
I think that part is doable, the part where I currently don't see an ergonomic solution is how global configuration like messages can be passed to the library (see the topic about context in the PR description).
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.
I've tried some workarounds,
1, Using singleton — Failed because for RSC, initializing on root layout requires 2 render to assign the
messages
object.2. Using third-party state manager like zustand — Failed because of the same reason as number 1.
Probably the solution is gonna be around client component. To use in a server component we could provide utility component like
<T key='dashboard.hi' />
. Performance-wise, next will also server-render client components so the downside is on developer experience. But won't be much I think.