Skip to content

How do I setup postCSS autoprefixer for Next.js project? #983

Answered by mattp0123
mattp0123 asked this question in Help
Discussion options

You must be logged in to vote

Problem solved!

import Document from 'next/document';
import postCss, { AcceptedPlugin } from 'postcss';
import postCssPresetEnv from 'postcss-preset-env';
import { getCssText } from 'styles';

export default class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext) {
    const cssText = getCssText();
    const { css } = await postCss([
      postCssPresetEnv() as AcceptedPlugin,
    ]).process(cssText);
    const initialProps = await Document.getInitialProps(ctx);
    return {
      ...initialProps,
      styles: (
        <>
          {initialProps.styles}
          <style id="stitches" dangerouslySetInnerHTML={{ __html: css }} />
        </>
      ),
    };

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mattp0123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
1 participant