next.js: AMP: styles missing when importing standard css files in a project using styled components
Bug report
Describe the bug
When importing standard css files from node modules, e.g.:
import 'reset-css/reset.css';
import 'semantic-ui-css/components/form.min.css';
import 'semantic-ui-css/components/message.min.css';
and when you’re using styled components, only the styled components remain. The styles of the normal css files imported from node modules disappear / are missing (in the AMP version).
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Use styled components in your project
- Import css from a lib that doesn’t use styled components, e.g. css reset (
import 'reset-css/reset.css';) - Use withAmp on your page
- View the amp version and notice standard imported css is missing
Expected behavior
Import both styled components and other imported css from files.
System information
- OS: macOS 10.14.4
- Browser: Chrome 73.0.3683.103 (Official Build) (64-bit)
- Version of Next.js: 8.1.1-canary.3
Additional context
Styled components seem to be working as of this version for AMP, but other css next to styled components is being thrown away in the AMP version.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 7
- Comments: 26 (9 by maintainers)
Styled JSX throws amp-validation errors.
The mandatory attribute 'amp-custom' is missing in tag 'style amp-custom (transformed)'.When using
<style amp-custom>{'...'}</style>in<Head>(as it is supposed to be) it compiles in HTML as:<style amp-custom content=""></style><style>(styled JSX here)</style>That’s why this is a problem.
Another workaround is to leverage the styled-css global feature + Next.js useAmp hook. See blog here: https://nextjs.org/blog/styling-next-with-styled-jsx#writing-styles-in-external-files
Example usage:
Downside is copying versioned files to your project.
@wouterds I was mistaken, we don’t support that in AMP mode right now (sorry about that), currently to do this you would need to load the styles in a custom
_document. Note: this is not ideal and is just a workaround you can use while official handling of this is implemented.Example with custom
pages/_document:hello folks, I found a solution:
_document.js.I used tailwindcss with AMP as an example and exposed completed code in my post https://dev.to/geekplux/how-to-use-tailwindcss-with-amp-in-a-next-js-project-1f97
@selahattintoprak feel free to send a PR to add it
any update on this?
is compiled as (in prod):
@timneutkens any clue on an ETA to actually support AMP within this project?
☝️ @geekplux’s workaround worked well for me. I am not using tailwindcss, but am using styled-components, and wanted to use a global css file. To expand a bit on what it entails:
*.cssinto a minified and autoprefixed*.min.cssfile. This requires extending the default postcss.config.js (and a slightly different syntax because of next.js’ lack ofrequire()s)_document.jsstylesof_document.jsDifferences in my implementation:
process.env.NODE_ENV(instead of a custom env) to differentiate prod vs dev.cssnanoto minify the css (and also named it *.min.css)Downside of this implementation:
next start, that’d be great.@ijjk Could you illustrate with an example how that’s supposed to work? Because this doesn’t.
Edit: to clarify;
background: red;works, but the external stylesheets are not loaded.@hi guys, let me give you my wisdom in this issue 😃
i’ve working for some days now on a project where I was forced to do an amp version, using @tailwindcss. This is what i’ve done and it works, really elegant by the way.
// .babelrc
// package.json
// any component, this is just an real example from my current code
As you will suppose now, the styled-jsx-postcss-plugin delegate to postcss the css transformation, using the same postcss config that you should use inside postcss.config.js .
Honestly, this setup took me 15 minutes to make it work, and it works like a charm. Only non-positive point to signal is that in some cases the styled-jsx-plugin-postcss shutdown, because of some memory leaks I guess, but that’s only a small issue.
Hope this helps some of you to achieve your work.
Whew glad I found this issue I have been trying to dig into the
global.cssnot importing for an hour. Looks like Tim added the good first issue 🏷️ so I will dive into it some more. Anyone already working on it??#8626 didn’t resolve this issue for me. And the workaround failed on zeit now. Is there a way to make this work? Thank you