Skip to content

Commit

Permalink
feat: nextjs css ssr production
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 24, 2020
1 parent 1ba6eed commit 01cd343
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
6 changes: 1 addition & 5 deletions core/webpack-compile/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import path from 'path';
import fs from 'fs';
import { log, error } from '@component-controls/logger';
import { mergeBuildConfiguration } from '@component-controls/config';
import {
BuildProps,
defBundleName,
getCSSBundleName,
} from '@component-controls/core';
import { BuildProps, defBundleName } from '@component-controls/core';
import { LoadingStore } from '@component-controls/store';
import LoaderPlugin from '@component-controls/loader/plugin';
import {
Expand Down
1 change: 0 additions & 1 deletion core/webpack-configs/src/react/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ export const react: PresetType = (options: BuildProps) => {
// Translates CSS into CommonJS
loader: 'css-loader',
options: {
importLoaders: 1,
modules: {
localIdentName: '[name].[local].[hash]',
},
Expand Down
1 change: 1 addition & 0 deletions examples/simple/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '*.css';
10 changes: 10 additions & 0 deletions examples/stories/src/tutorial/getting-started/ssg/nextjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,21 @@ Create a new or edit `_document.js` file in the pages folder:

```js:title=pages/_document.js
import React from 'react';
import fs from 'fs';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { extractCritical } from 'emotion-server';

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
const styles = extractCritical(initialProps.html);
// following only if you are usng css in your componentes
let cssStyles;
const cssBundle = process.env.NEXT_CC_CSS_FILENAME;
if (cssBundle && fs.existsSync(cssBundle)) {
cssStyles = fs.readFileSync(cssBundle, 'utf8');
}
// end css styles
return {
...initialProps,
styles: (
Expand All @@ -140,6 +148,8 @@ export default class MyDocument extends Document {
data-emotion-css={styles.ids.join(' ')}
dangerouslySetInnerHTML={{ __html: styles.css }}
/>

{cssStyles && <style>{cssStyles}</style>}
</>
),
};
Expand Down

0 comments on commit 01cd343

Please sign in to comment.