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

[RFC] Improve Next.js integration with a dedicated npm package #34575

Closed
garronej opened this issue Oct 2, 2022 · 17 comments · Fixed by #39947
Closed

[RFC] Improve Next.js integration with a dedicated npm package #34575

garronej opened this issue Oct 2, 2022 · 17 comments · Fixed by #39947
Assignees
Labels
new feature New feature or request nextjs RFC Request For Comments

Comments

@garronej
Copy link
Contributor

garronej commented Oct 2, 2022

Hello everyone 👋🏻

What's the problem? 🤔

It's currently not as straight forward as it could be to setup Material UI in a Next.js project.
Users are encouraged to copy past a bunch of complicated, not type safe code from an example setup.

Proposed solution 🟢

tss-react exports a utility that abstract away the implementation of emotion's advanced approach for setting up SSR in a Next.js project:

Users are expected to set it up this way:

//pages/_app.tsx
import Head from "next/head";
import App from "next/app";
import type { AppProps } from 'next/app'
import { createEmotionSsrAdvancedApproach } from "tss-react/next";

const {
    augmentDocumentWithEmotionCache,
    withAppEmotionCache
} = createEmotionSsrAdvancedApproach({ "key": "css" });

export { augmentDocumentWithEmotionCache };

export default withAppEmotionCache(App);
//pages/_document.tsx
import Document from "next/document";
import { augmentDocumentWithEmotionCache } from "./_app";

augmentDocumentWithEmotionCache(Document);

export default Document;

This utility also works well when there's need for multiple emotion caches.

There is nothing specific to tss-react in this utility so it could be moved over to the MUI codebase:

-import { createEmotionSsrAdvancedApproach } from "tss-react/next";
+import { createEmotionSsrAdvancedApproach } from "@mui/next"; // Or whatever other path

Resources and benchmarks 🔗

@garronej
Copy link
Contributor Author

garronej commented Oct 24, 2022

Hello @mnajdova @oliviertassinari,
I think this is worth reviewing, Material UI is getting a bad press for not being easy enough to use in SSR setups:

image

relevent timestamp

Best regards,

@oliviertassinari oliviertassinari added the enhancement This is not a bug, nor a new feature label Oct 24, 2022
@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 24, 2022

@garronej It used to be tricky to configure SSR correctly with Material UI v4. The issue was the JSS counter based approach to generate class names, once you have a mismatch it breaks the styles and is really hard to find. I would imagine it's what Theo refers to.

Now, I agree that it could be great to abstract this logic. If we do, I think that we should look into how we can make it support different Next.js's versions if they make breaking changes. For example, they have a new experimental layout system vercel/next.js#37136.

This could also open the door to more Next.js specific modules, we could abstract the Link and progress bar integration.

@garronej
Copy link
Contributor Author

It used to be tricky to configure SSR correctly with Material UI v4. The issue was the JSS counter based approach to
generate class names, once you have a mismatch it breaks the styles and is really hard to find. I would imagine it's what
Theo refers to.

Fair enought, I wasn't aware of the legacy.

This could also open the door to more Next.js specific modules, we could abstract the Link and progress bar integration.

👍

@garronej
Copy link
Contributor Author

garronej commented Dec 22, 2022

For example, they have a new experimental layout system

tss-react now provides a helper for Next.js 13 appDir.

@oliviertassinari oliviertassinari changed the title [RFC] Improve Next.js integration [RFC] Improve Next.js integration with a dedicated npm package Jul 24, 2023
@siriwatknp
Copy link
Member

siriwatknp commented Jul 26, 2023

Check out #39712

@oliviertassinari

This comment was marked as outdated.

@oliviertassinari
Copy link
Member

Related to Next.js Link integration: https://twitter.com/benmvp/status/1458609672731070468.

@sgoodrow-zymergen

This comment was marked as off-topic.

@garronej
Copy link
Contributor Author

Hi @sgoodrow-zymergen,
Over on the tss-react repo, the comunity is helping me incrementally maintain a generic provider.
You can give it a shot, see if it solves your issue, no need to install tss-react, you can just copy paste the code:

https://github.com/garronej/tss-react/blob/main/src/next/appDir.tsx
https://docs.tss-react.dev/ssr/next.js

We have dark App Router + theme switchin working without issues as demonstrated here: https://next-demo.react-dsfr.fr/mui

@sgoodrow-zymergen
Copy link

Thanks @garronej, those updates do resolve the issue. Hoping this all gets bundled into a nice package soon!

@danilo-leal danilo-leal moved this to In progress in Material UI Dec 1, 2023
@danilo-leal danilo-leal added this to the Material UI v6 milestone Dec 1, 2023
@DiegoAndai DiegoAndai removed this from the Material UI: v6 milestone Dec 7, 2023
@github-project-automation github-project-automation bot moved this from In progress to Done in Material UI Dec 11, 2023
@mwskwong
Copy link
Contributor

mwskwong commented Dec 13, 2023

@siriwatknp I noticed @mui/material-nextjs seems to be intended for Material UI specifically. It is named as such. Only the Mateiral UI doc mentions it.

Yet, this can also be used in other libraries like Joy UI by simply setting the key to joy. Why not just make this package generic, e.g '@mui/nextjs-integration and in the doc, just instruct the user to specify the correct key correspondingly?

@siriwatknp
Copy link
Member

@siriwatknp I noticed @mui/material-nextjs seems to be intended for Material UI specifically. It is named as such. Only the Mateiral UI doc mentions it.

Yet, this can also be used in other libraries like Joy UI by simply setting the key to joy. Why not just make this package generic, e.g '@mui/nextjs-integration and in the doc, just instruct the user to specify the correct key correspondingly?

Thanks for the suggestion but there will be APIs that are specific to Material UI like Link, Image, and NProgressBar.

@oliviertassinari
Copy link
Member

oliviertassinari commented Dec 26, 2023

I'm reopening, I think that until the examples use the new npm package, most people won't use it (until Next.js makes a breaking change and it's less work to just use our package). It will also allow us to guarantee that it works end-to-end.

@oliviertassinari oliviertassinari added new feature New feature or request nextjs and removed enhancement This is not a bug, nor a new feature labels Dec 26, 2023
@siriwatknp
Copy link
Member

@oliviertassinari #40199 is merged.

@oliviertassinari
Copy link
Member

@siriwatknp I think we should update the examples before considering the problem solved: #34575 (comment).

@siriwatknp
Copy link
Member

siriwatknp commented Jan 16, 2024

@siriwatknp I think we should update the examples before considering the problem solved: #34575 (comment).

As I mentioned, #40199 is the PR that update examples to use @mui/material-nextjs package and it is merged. It's in the latest master.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 18, 2024

@siriwatknp Oh, got it, awesome 👌.

In the future, I think we need a @mui/system-nextjs too. Most of Material UI integration with Next.js should be concerned about components. All the style and theming logic should be into @mui/system-nextjs, providing a similar separation of concerns as we have with Shadcn.

  • Radix Primitives -> Base UI
  • Tailwind CSS -> MUI System
  • Shadcn UI -> Material UI

This isolation of problem scopes seems to work well with what's common for developers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request nextjs RFC Request For Comments
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

7 participants