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

Server Side Rendering doesn't work with latest NextJS #1156

Closed
schabluk opened this issue Oct 11, 2020 · 29 comments
Closed

Server Side Rendering doesn't work with latest NextJS #1156

schabluk opened this issue Oct 11, 2020 · 29 comments
Labels

Comments

@schabluk
Copy link

schabluk commented Oct 11, 2020

🐛 Bug Report

The guide for setting up SSR configuration in NextJS doesn't work with its latest version.

🤔 Expected Behavior

No errors.

😯 Current Behavior

After following the guide: https://react-spectrum.adobe.com/react-spectrum/ssr.html#nextjs I'm getting an error:

ready - started server on http://localhost:3000
info  - automatically enabled Fast Refresh for 1 custom loader
Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
See here for more info: https://err.sh/next.js/built-in-css-disabled

info  - Using external babel configuration from /Users/my/project/.babelrc.js
error - ./src/styles/globals.css
TypeError: Invalid PostCSS Plugin found at: plugins[0]

(@/Users/my/project/postcss.config.js)
    at Array.forEach (<anonymous>)

Content of postcss.config.js:

module.exports = {
  plugins: ['tailwindcss', 'autoprefixer', 'postcss-import'],
}

The @zeit/next-css plugin is considered to be obsolete according to: https://err.sh/next.js/built-in-css-disabled

After removing @zeit/next-css plugin, I'm getting:

error - ./node_modules/@react-spectrum/actiongroup/dist/main.css
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to src/pages/_app.tsx. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://err.sh/next.js/css-global
Location: node_modules/@react-spectrum/actiongroup/dist/module.js

💁 Possible Solution

The suggestion is that the maintainer should publish a compiled version of the dependency: https://github.com/vercel/next.js/blob/master/errors/css-npm.md

🌍 Your Environment

Software Version(s)
react-spectrum 3.4.0
next 9.5.5
@devongovett
Copy link
Member

The error seems to be complaining about your postcss config. Are you sure it's correct and you have all of those modules installed properly?

@schabluk
Copy link
Author

schabluk commented Oct 11, 2020

@devongovett: Yes, I'm pretty sure my setup is correct. Everything was working before I installed @adobe/react-spectrum and configured SSRProvider and theme Provider.

The first error is about the PostCSS, but the second complains about this line:

// node_modules/@react-spectrum/actiongroup/dist/module.js
import "./main.css";

@devongovett
Copy link
Member

Did you check out the example project? https://github.com/devongovett/rsp-next

@schabluk
Copy link
Author

So, I could finally make the build work without errors after changing postcss.config.js like this:

module.exports = {
  plugins: [
    require('postcss-import'),
    require('tailwindcss'),
    require('autoprefixer')],
}

However, it turns out that @zeit/next-css doesn't allow me to use CSS Modules together with regular CSS files from react-spectrum - it works only with built-in css module support. At this point I have to bail out on it, because most of my codebase uses CSS Modules. Though, I'm still going to use react-aria and react-stately :)

Any chance that the built-in CSS in Next will be supported by Spectrum in the near future?

@devongovett
Copy link
Member

devongovett commented Oct 13, 2020

TBH I'm not sure what we could do about it. Next doesn't load CSS files from node_modules so I'm not sure how we're expected to distribute the styles or how that would play with other tools. The current recommendation in the docs is the best we could come up with after talking to the Next team without completely changing how we distribute the styles. If anyone has any better ideas we'd love to hear them though. 😄

@schabluk
Copy link
Author

schabluk commented Oct 14, 2020

@devongovett: please check related issue in tailwindcss: tailwindlabs/tailwindcss#2547

@schabluk
Copy link
Author

Importing css from 3rd party components is supported in Next v10: https://nextjs.org/blog/next-10#importing-css-from-third-party-react-components

@devongovett
Copy link
Member

Hmm still can't import css from within node_modules though. I don't think we want to force people to manually import the css file for each component they use...

@schabluk
Copy link
Author

I don't think we want to force people to manually import the css file for each component they use...

No, that would be overcomplicated. Weird that it doesn't work from within node_modules...

@twavv
Copy link
Contributor

twavv commented Dec 19, 2020

It's worth noting that asking users to import CSS for every component *is the approach that Reach UI takes, and it's not overly complicated (though there are comparatively fewer components).

@DarkPurple141
Copy link

Using a Babel plugin may help here. Alternatively an approach like https://github.com/atlassian-labs/compiled might make sense?

@ZLevine
Copy link

ZLevine commented Jan 6, 2021

It's worth noting that asking users to import CSS for every component *is the approach that Reach UI takes, and it's not overly complicated (though there are comparatively fewer components).

Agreed. Also you avoid importing CSS for components you never use.

@nhducit
Copy link

nhducit commented Jan 12, 2021

both nextjs and react spectrum are awesome but it sad they are not easy to use them together

@kennetpostigo
Copy link

kennetpostigo commented Feb 5, 2021

Hi, I'm running into this with a bare nextjs application, and I opened a issue in the example repo, so just wanted to reference here.

Does anyone know if theres a work around?

@ryankauk
Copy link

ryankauk commented Feb 27, 2021

I was thinking maybe there's a way to ignore the css imports from webpack config side and then just import the styles manually. I don't know enough about webpack, does anyone with more web pack knowledge know a way for this to be possible?

I tried https://webpack.js.org/plugins/ignore-plugin/ out but it didn't seem to work

@tanishqkancharla
Copy link

Hey I'm new to this, so forgive me for not completely understanding the problem but couldn't Spectrum just publish using CSS modules instead of global css? Or do the distributed version necessarily convert the modules into global css?

@Amir-Abushanab
Copy link

Starting a new Typescript + Next.js (10.2.0) project and I've hit a brick wall with this too

@abhinav-anshul
Copy link

Hi, anyone came up with a solution?

@edazpotato
Copy link

I've had this same issue. If anyone has a workaround, that would be great!

@cmawhorter
Copy link

cmawhorter commented Jun 15, 2021

FWIW -- i think i was able to get this running. according to this issue in next-transpile-modules, a fix shipped in 7.2.

to get it to run without error, i had to do the following:

  • use the explicit list of modules instead of wildcard (it complained about not being able to find module)
  • install webpack@4 (with webpack5 it was giving an error about "tap" being undefined). got the idea from here
  • not sure if this did anything, but i also did: node v14, rm -fr ./node_modules; rm-fr ./.next

after that npm run dev loads the site. no idea if it's actually working though. i'm new to both nextjs and spectrum.

Edit: yeah.... that got rid of the errors but it still doesn't work. There is a lengthy issue open at nextjs without any solutions too so... 🤷

In the meantime maybe docs could be updated to mention it only works with nextjs v9 and an explanation? AFAICT it's a nextjs issue not spectrum

@DaveStein
Copy link

@cmawhorter did you do this in NextJS 10 or 11?

@cmawhorter
Copy link

@DaveStein pretty sure it was v10

@DaveStein
Copy link

Ah I'm on 11 so I'm scared to even try vs await what the maintainer says over at martpie/next-transpile-modules#222

@reidbarber
Copy link
Member

reidbarber commented Jul 19, 2021

Posting here for visibility, but it seems to be working with the config posted over at martpie/next-transpile-modules#222 (comment). Big thanks to @fabianishere and @DaveStein.

next.config.js:

const withTM = require("next-transpile-modules")([
  "@adobe/react-spectrum",
  "@react-spectrum/actiongroup",
  "@react-spectrum/breadcrumbs",
  "@react-spectrum/button",
  "@react-spectrum/buttongroup",
  "@react-spectrum/checkbox",
  "@react-spectrum/combobox",
  "@react-spectrum/dialog",
  "@react-spectrum/divider",
  "@react-spectrum/form",
  "@react-spectrum/icon",
  "@react-spectrum/illustratedmessage",
  "@react-spectrum/image",
  "@react-spectrum/label",
  "@react-spectrum/layout",
  "@react-spectrum/link",
  "@react-spectrum/listbox",
  "@react-spectrum/menu",
  "@react-spectrum/meter",
  "@react-spectrum/numberfield",
  "@react-spectrum/overlays",
  "@react-spectrum/picker",
  "@react-spectrum/progress",
  "@react-spectrum/provider",
  "@react-spectrum/radio",
  "@react-spectrum/slider",
  "@react-spectrum/searchfield",
  "@react-spectrum/statuslight",
  "@react-spectrum/switch",
  "@react-spectrum/tabs",
  "@react-spectrum/text",
  "@react-spectrum/textfield",
  "@react-spectrum/theme-dark",
  "@react-spectrum/theme-default",
  "@react-spectrum/theme-light",
  "@react-spectrum/tooltip",
  "@react-spectrum/view",
  "@react-spectrum/well",
  "@spectrum-icons/ui",
  "@spectrum-icons/workflow"
]);

module.exports = withTM({});

You may need to remove some of these if you're using an older version, or add some if new components become available.

Hopefully we can update the docs and add an example pretty soon.

@DaveStein
Copy link

A key thing for debugging in 222 was that using the error messages as normal,I was able to nail down all the @react-spectrum values. Fabian then described in ticket how doing an inspect mode showed him additional things like @spectrum-icons

@sarnason
Copy link

It's not working for me. Spent two days already trying to get a large project to build.
Made a sample with details in the readme. I am sure @devongovett will find it interesting as it is based on his original sample.
https://github.com/sarnason/rsp-next-ts

@reidbarber
Copy link
Member

@sarnason I've got working examples at rsp-next-ts and rsp-next for reference. Both fresh from create-next-app.

@sarnason
Copy link

Thanks @reidbarber! I was able to find my problem by comparing the two. Turns out that using the nightly wasn't such a good idea. Shall I file a bug for it being broken?

@reidbarber
Copy link
Member

@sarnason Feel free to create one if you still have good details about the error you were getting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests