Skip to content
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

State of issuerLayer=="rsc" for the library authors #64244

Closed
timofei-iatsenko opened this issue Apr 9, 2024 · 7 comments
Closed

State of issuerLayer=="rsc" for the library authors #64244

timofei-iatsenko opened this issue Apr 9, 2024 · 7 comments
Labels
Documentation Related to Next.js' official documentation. locked

Comments

@timofei-iatsenko
Copy link

timofei-iatsenko commented Apr 9, 2024

What is the improvement or update you wish to see?

Hello NextJs team,

I'm a maintainer of the Lingui https://lingui.dev/ which is i18n library with a focus on DX.

Recently we started a discussion how to support RSC without lacking a DX for our users and found that we need a possibility to resolve our Trans component to a different implementation depending on the issuer layer.

I ended up with my custom resolver for webpack which looks like this:

class LinguiTransRscResolver {
  apply(resolver) {
    const target = resolver.ensureHook('resolve');
    resolver
      .getHook('resolve')
      .tapAsync('LinguiTransRscResolver', (request, resolveContext, callback) => {

        if (request.request === TRANS_VIRTUAL_MODULE_NAME) {
          const req = {
            ...request,
            request: request.context.issuerLayer === 'rsc'
              // RSC Version without Context and will use React.cache to get current 
              // locale and message catalog
              ? path.resolve('./src/i18n/rsc-trans.tsx')
              // Regular version, using context to get Message Catalog
              : '@lingui/react',
          };

          return resolver.doResolve(target, req, null, resolveContext, callback);
        }

        callback();
      });
  }
}

However, request.context.issuerLayer === 'rsc' is an internal Next.js implementation detail, and I feel that using it in our library may break at any time.

  • How safe is to use this property in a 3rd party code?
  • Could you suggest a better alternative to solve the same issue?
  • Could you consider making this property part of public api for library authors?

Related issues:

lingui/js-lingui#1698
lingui/js-lingui#1903

Lingui with RSC PoC: https://github.com/thekip/nextjs-lingui-rsc-poc/blob/main/next.config.js

@timofei-iatsenko timofei-iatsenko added the Documentation Related to Next.js' official documentation. label Apr 9, 2024
@pawelblaszczyk5
Copy link

Maybe using package.json conditions would solve this and be more universal across RSC frameworks? There’s a “react-server” import/export condition for RSC 😃 Would that help there?

@timofei-iatsenko
Copy link
Author

Good point. That makes sense.

@timneutkens
Copy link
Member

Yeah definitely don't add a webpack plugin for this as it'll break in the future 👍 The react-server condition in package.json exports allows you to provide a separate implementation for React Server Components that is supported across all frameworks that support Server Components 💯

@timofei-iatsenko
Copy link
Author

Is this react-server condition already supported by next?

@pawelblaszczyk5
Copy link

pawelblaszczyk5 commented Apr 11, 2024

Is this react-server condition already supported by next?

Yes, it was supported from the first release, that’s part of React spec 😃
Example package utilizing it - https://github.com/vercel/react-tweet/blob/main/packages/react-tweet/package.json

@timofei-iatsenko
Copy link
Author

Thanks for your answers, we will go this way.

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Related to Next.js' official documentation. locked
Projects
None yet
Development

No branches or pull requests

3 participants