-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Global CSS not applied to AMP pages #10549
Comments
I have the same problem with TailwindCSS setup. I use the Hybrid AMP. Adding inline style My NextJS version: 9.2.2 |
Wow, I've been trying to figure this one out for hours, thanks. I'll see if I can find the error and work on it and eventually do a PR EDIT: I'm assuming this isn't solved yet, even in unstable versions, right? |
It has to be somewhere in this file https://github.com/zeit/next.js/blob/canary/packages/next/next-server/server/render.tsx right? |
Btw @achrinza as a temp fix, you can probably import your .css like this:
Assuming, ofc, you have the .css in the public dir. This, for some reason however, throws an Amp Validation error
(This error happens even if the is the only child of ) This works fine afaik. Also, this should point in the right direction. Why does importing with |
@filipesmedeiros Thanks for the the workaround. Unfortunately (based on the error), this won't generate a valid AMP page which would prevent the AMP Cache from caching the web page. It'll also breaking some CI/CD pipelines that run the AMP validator. So unfortunately it’s a no-no for my projects |
FWIW #8710 seems to be the PR that adds Global CSS |
@achrinza True, this doesn't generate valid AMP. I think for now the easiest way then, to have AMP and a "global" style would be to just inline it in the of the _app.js using styled jsx. |
So wait, this should be the cause right: webpack is not putting the imported stylesheets inside <style amp-custom>, maybe it has nothing to do with Next. EDIT: nevermind, this should be something like the way Next is grabbing those global styles and passing them to AMP for the HTML generation. |
@timneutkens Sorry to tag you on this, but I have the feeling you can point me in the right direction (or tag someone who can?) so I can try to have a fix for this. I've been on this for a couple of hours, and I think this has to do with the Webpack config because:
So I think to solve this, we need to find a way to tell Webpack to grab the global stylesheets and put them inside So question is: can you guys point me in the direction of where we are telling Webpack what to do with global stylesheets and how to access the flag that tells us if a page is AMP or not? If this is asking too much, let me know. Thanks! |
Webpack only compiles the stylesheet. It outputs it into a directory under |
Thanks, gonna check it out. EDIT: Ah btw:
I assume you're talking about prod right? Because in dev, each .css file (even if In any case, I'll look at what you said of inlining all the compiled stylesheets in one tag! |
Not sure if this is the best option, but one approach could be to implement a custom transformer for AMP Optimizer which collects all inline styles and merges it into |
@sebastianbenz you mean after reading the files and getting then inside the markup? So:
Something like this? I was trying to read directly from the file into the |
If reading from the file into amp-custom works, then it's probably the easiest way. |
I'm having a hard time connecting the two parts, mainly because it seems that when AMP mode is on, the dev server doesn't execute any code in this client Webpack loader, which seems to control where the global CSS ends up, at least in dev. Tim said (for prod I assumed) we only need to append the fully compiled So currently it seems that for prod and dev, the methods of appending CSS into Still exploring, though |
Why nobody is talking about SASS support for amp? I looked through all open and closed issues. I mean there are no open issues even entertaining idea of SASS with amp. I understand it will take a while to get implemented. I would just like to have glimmer of hope that one day I will get built in SASS/SCSS support for amp. |
The problem is not SASS I think, it's importing CSS in general with AMP. I think AMP itself is very restrictive on how CSS in handled, so importing is weird with AMP. Best way to do it is with styled-components, emotion, etc, I think |
I did not have the opportunity to try it, but I saw something about it at this address. |
@filipesmedeiros I disagree. Main objective of AMP is not to make it easy for development but its customer experience. Also there is no custom javascript allowed in general. And they have really good reason for it. You can use it for edge cases. @bulentala you can do all that with postCSS. Because either way you need to support for all flavour of browser e.g. mozella, safari, chrome. They all have their propriatary prefixes to css. Also most of these libraries will make your css worse than it already is. I think Nextjs people have done fantastic job for making it easy to add AMP pages. I have been building amp websites with Hugo SSG. And nextjs does save time for adding AMP pages. It will just take longer without SASS and other stuff. Edit: |
Sass/CSS handling is the same so this issue covers both already. It's marked good first issue so feel free to send a PR |
This comment has been minimized.
This comment has been minimized.
I got it working for production builds in #17914 but I'm not sure what I need to tweak to get styles working in development when viewing AMP pages. Did you make any progress on this @filipesmedeiros ? |
@jpbow Not really sorry, I actually kinda gave up on AMP, because performance was good enough without it. Tbh I'm starting to think that having another standard (AMP) is not as productive as trying to optimize "regular" website made, for example, with Next.js or React. With this said, I know very little about AMP so I might be wrong. |
It's a really big issue because I use tailwind and I need style AMP pages with tailwindcss classes. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
For people wanting this feature because of tailwind, tailwind-in-js: |
I think it's also possible to achvie this using |
it's also possible to achvie this using styled jsx loader module.exports = {
webpack: (config, options) => {
config.module.rules.push({
test: /\.css$/,
use: [
options.defaultLoaders.babel,
{
loader: require('styled-jsx/webpack').loader,
options: { type: 'global' },
},
],
});
return config;
},
}; and import styles from '../styles.css';
<style jsx global>
{styles}
</style> |
@tonny008 Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected. |
so no luck resolving this issue after 16 months? |
Given that Google is no longer giving priority to AMP pages, I think it makes the most sense to abandon AMP as a strategy. Our dev hours are likely better spent making our sites work well on mobile. |
Hmm, didn't know about that. Is there any good article about the reason and what is the future of AMP according to Google? So is it worth making AMP websites now or better to wait for some alternatives? |
This comment has been minimized.
This comment has been minimized.
Thank you and my luck of checking this issue. AMP is too... Weird, I was about to use Linaria for normal styling and Styled Components for the sole purpose of supporting AMP (aside from the 15 pages I had open about AMP-specific things) |
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
AMP is not about getting priority in SERP, and it was never about that except for people who are opportunists. AMP is about inventing a new evergreen HTML standard which continuously removes outdated practices and optimizing for performance automatically, and it started for mobile but it is now for every device form factor. Many publishing companies are making AMP only responsive complete websites and they are using Next.js to ease some tasks like SSG. Most of the people who stopped being attracted to AMP are talking about the difficulty of maintaining two websites, but I think they are very outdated about how powerful AMP library got in recent years. Actually we were able easily to do almost all the features we need for a modern publishing website using AMP only, including login and custom CAPCHA. Now, when we need to use some advanced CSR features we will use either hybrid AMP for that page/app (and possibly use AMP-to-PWA pattern), or skip the AMP completely, but the majority (if not all) are done in AMP only with This is an alternative route compared to hydration, but it is better than hydration in our use case because the page is fully interactive once it is loaded and it is smaller as there will not be hydration data payload. After all, supporting Web Vitals is not an easy task for the average developer, and AMP is making it almost ready out of the box once the validation is complete. |
I wasn't having much luck with the solution from @tonny008 so I did another workaround. My use case is having a single global CSS file imported in the app.js, which has my tailwind imports etc. First compiled the imported CSS into its own file to make it available later: const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
webpack: (config, options) => {
config.plugins.push(
new MiniCssExtractPlugin({
filename: '../public/global.css',
})
);
return config;
},
}; Then fetch and embed for amp pages: import '../styles/global.css';
type MyAppProps = AppProps & {
css: string;
};
const MyApp = ({ Component, pageProps, css }: MyAppProps) => {
return (
<>
{isAmp && <style jsx global>{css}</style>}
<Component {...pageProps} />
</>
)
};
MyApp.getInitialProps = async () => {
const res = await fetch('http://localhost:3000/global.css');
const css = await res.text();
return { css }
} Loses the advantages of chunking and does not work for CSS modules but fits my use case. |
Thank you for taking the time to open this. We are not planning to add additional features / changes to the current AMP implementation. |
Bug report
Describe the bug
Global CSS (via
_app.tsx
/_app.js
) are not being applied to AMP pages.Non-AMP pages do not have this issue.
To Reproduce
Reproduction sandbox: https://github.com/achrinza/issue-sandbox-nexjs-global-css-amp
Step-by-step:
npm init next-app
styles.css
pages/_app.js
pages/NonAmp.js
page:pages/Amp.js
pagenpm run dev
localhost:3000/Amp
andlocalhost:3000/NonAmp
- observe the lack ofcolor: red
on the AMP page.Expected behavior
The AMP page should have a global CSS applied.
Screenshots
If applicable, add screenshots to help explain your problem.
System information
Additional context
N/A
The text was updated successfully, but these errors were encountered: