-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Note that this is a compatibility release that allows to use `next-intl` in Next.js 13 apps. It's still advised to use `next-intl` via the `pages` folder, usage in the `app` folder with Server Components is not possible currently (this will be part of a separate release based on #139).
export default function RootLayout({children, ...rest}: Props) { | ||
console.log(rest); | ||
|
||
// How to get this from the URL? |
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 using params
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
export default function Index({params: {locale}}: Props) { | |
// TODO: Validate locale or redirect to default locale | |
return <p>Hello {locale}</p>; | |
} |
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.
ps: I can't find any documentation for
createServerContext
, but I just try to use it regardless the type-error and it seems like it works without sacrificing DX. However, we need to refactor functions out of the context.
packages/example/src/middleware.ts
Outdated
import type {NextRequest} from 'next/server'; | ||
import i18n from './i18n'; | ||
|
||
// Somehow not invoked? |
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.
From a quick glance at the example WIP, the middleware doesn't seem to fire unless there is a "pages" folder as well. After that, it fires on both "pages" and "app" folders.
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.
Oh cool, thanks for figuring this out! Seems like a bug on the Next.js side.
Hi, thanks for your work. We had decided to use next-intl for a project a while ago, and we are in the process of slowly migrating to next 13. I'm not sure if this will be of any use, but I've added a working (toy) example of a header-based solution: https://github.com/daniel-koudouna/next-intl/tree/feat/next-13 Some things to note:
I'm not well versed with react internals by any means, this is just another experiment. Let me know what you think, I can open a PR to this branch if it helps. |
Hey @daniel-koudouna and thanks a lot for chiming in! This branch is a really raw draft at this point, mostly for experimenting. I'm currently using Next.js internal APIs in Using That is a bit the dealbreaker currently in my prototype and I'd like to find a better solution before moving forward. I think the other parts should be manageable. But since Next.js is moving i18n functionality out of the core, probably this library will also participate in locale negotiation in the future. Would be really cool to get input from you when there's a first working prototype! Also great that you mention |
No worries, happy to help if I can, although I'm not at all familiar with React internals. I found the headers-based solution pretty easy to add domains to. |
Closed in favour of #149 |
Things to figure out
createServerContext
seems to be sent to the client so we could use it for auto-serializationheaders()
andcookies()
.react-server
exports?Afterwards