Tailwindcss with SSR #12868
-
I used the official example from here https://github.com/zeit/next.js/tree/canary/examples/with-tailwindcss You can see it deployed here - https://next-js-tailwind-ex.now.sh/ My Question is that I am unable to figure out how to do it correctly with SSR so that the initial response from server contains the styles within head (inline in the file). @taylorbryant you have any tips for this may be ? Thank you for putting up the example btw and also for updating it to the latest updates from tailwindcss. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 13 replies
-
Do you have a link to your code that isn't working? The linked example seems to work fine, so isn't much help. Did you import your https://github.com/cdock1029/with-tailwind/blob/master/pages/_app.js |
Beta Was this translation helpful? Give feedback.
-
@sambhav-gore Thanks for the kind words! I've actually never tried inlining CSS in the head in Next.js. A quick search turned up these. Maybe they'll be helpful? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Just an FYI for anyone that sees this, 9.5.2 on up |
Beta Was this translation helpful? Give feedback.
-
if someone has not yet succeeded, try this: _document.tsx // @ts-ignore
import bundleCss from "!raw-loader!../styles/tailwindSSR.css";
import Document from "next/document";
export default class MyDocument extends Document {
static async getInitialProps(ctx: any) {
const page = ctx.renderPage((App) => (props) => <App {...props} />);
const initialProps: any = await Document.getInitialProps(ctx);
return {
...page,
styles: [
...initialProps.styles,
process.env.NODE_ENV === 'production' ? <style
key="custom"
dangerouslySetInnerHTML={{
__html: bundleCss,
}}
/>: <></>,
],
};
}
} package.json {
"scripts": {
"prebuild": "NODE_ENV=production tailwindcss-cli build -o styles/tailwindSSR.css",
"build": "next build",
},
}
I dont like it but It works for me |
Beta Was this translation helpful? Give feedback.
-
Sharing another way: // next.config.js
const { webpack } = require('next/dist/compiled/webpack/webpack');
// config.webpack
const criticalCss = execSync(
['npx', 'tailwind', ['--input', 'src/pages/global.scss'], '--minify'].flat().join(' '),
{ cwd: PROJECT_ROOT }
);
config.plugins.push(
new webpack.DefinePlugin({
CRITICAL_CSS: JSON.stringify(criticalCss),
})
); // _document.tsx
<style dangerouslySetInnerHTML={{ __html: CRITICAL_CSS }} /> |
Beta Was this translation helpful? Give feedback.
-
Adding this to my next.config.js did the job
reference: https://twitter.com/hdjirdeh/status/1369709676271726599 |
Beta Was this translation helpful? Give feedback.
-
I was able to solve this myself by utilizing the link elements rel attribute value "preload" which did the trick for me. I wanted to avoid adding the style sheet directly in the head. |
Beta Was this translation helpful? Give feedback.
@sambhav-gore Thanks for the kind words!
I've actually never tried inlining CSS in the head in Next.js.
A quick search turned up these. Maybe they'll be helpful?