-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handled gatsby ssr css production
- Loading branch information
1 parent
1530ec0
commit 08f6e5c
Showing
18 changed files
with
264 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
stories: ['./src/*.@(mdx|tsx)'], | ||
siteUrl: `https://component-controls-gatsby.netlify.app`, | ||
cssFileName: 'globals.css', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const withStories = require('@component-controls/nextjs-plugin/build'); | ||
|
||
module.exports = withStories({ | ||
configPath: 'docs', | ||
distDir: 'dist', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { FC } from 'react'; | ||
import { GetStaticProps, GetStaticPaths } from 'next'; | ||
import { DocType, defDocType } from '@component-controls/core'; | ||
import { DocumentHomePage } from '@component-controls/app'; | ||
import { | ||
Layout, | ||
store, | ||
getHomePagesPaths, | ||
getDocHomePage, | ||
} from '@component-controls/nextjs-plugin'; | ||
|
||
interface PageListProps { | ||
type: DocType; | ||
docId?: string; | ||
storyId?: string; | ||
} | ||
|
||
const DocHomeTemplate: FC<PageListProps> = ({ | ||
type = defDocType, | ||
docId, | ||
storyId, | ||
}) => { | ||
return ( | ||
<Layout docId={docId} storyId={storyId}> | ||
<DocumentHomePage type={type} /> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export const getStaticPaths: GetStaticPaths = async () => { | ||
return { paths: getHomePagesPaths(store), fallback: false }; | ||
}; | ||
|
||
export const getStaticProps: GetStaticProps = async ({ params }) => { | ||
const { doctype: basepath } = params as { doctype: string }; | ||
const page = getDocHomePage(store, basepath); | ||
const { type = null, docId = null, storyId = null } = page || {}; | ||
return { props: { docId, storyId, type } }; | ||
}; | ||
|
||
export default DocHomeTemplate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { FC } from 'react'; | ||
import { GetStaticProps, GetStaticPaths } from 'next'; | ||
import { DocPage } from '@component-controls/app'; | ||
import { DocType } from '@component-controls/core'; | ||
import { | ||
Layout, | ||
store, | ||
getDocPagesPaths, | ||
getDocPage, | ||
} from '@component-controls/nextjs-plugin'; | ||
|
||
interface DocPageProps { | ||
docId?: string; | ||
storyId?: string; | ||
type: DocType; | ||
activeTab?: string; | ||
category?: string; | ||
} | ||
|
||
const DocPageTemplate: FC<DocPageProps> = ({ | ||
docId, | ||
storyId, | ||
type, | ||
category, | ||
activeTab, | ||
}) => { | ||
return ( | ||
<Layout docId={docId} storyId={storyId} activeTab={activeTab}> | ||
<DocPage type={type} category={category} /> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export const getStaticPaths: GetStaticPaths = async () => { | ||
return { paths: getDocPagesPaths(store), fallback: false }; | ||
}; | ||
|
||
export const getStaticProps: GetStaticProps = async ({ params }) => { | ||
const { doctype, docid } = params as { doctype: string; docid: string[] }; | ||
const page = getDocPage(store, doctype, docid); | ||
const { | ||
type = null, | ||
docId = null, | ||
storyId = null, | ||
category = null, | ||
activeTab = null, | ||
} = page || {}; | ||
return { props: { docId, type, storyId, category, activeTab } }; | ||
}; | ||
|
||
export default DocPageTemplate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import NextApp from 'next/app'; | ||
import { CacheProvider } from '@emotion/core'; | ||
|
||
// Use only { cache } from 'emotion'. Don't use { css }. | ||
import { cache } from 'emotion'; | ||
|
||
export default class App extends NextApp { | ||
render() { | ||
const { Component, pageProps } = this.props; | ||
return ( | ||
<CacheProvider value={cache}> | ||
<Component {...pageProps} /> | ||
</CacheProvider> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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); | ||
let cssStyles; | ||
const cssBundle = process.env.NEXT_CC_CSS_FILENAME; | ||
if (cssBundle && fs.existsSync(cssBundle)) { | ||
cssStyles = fs.readFileSync(cssBundle, 'utf8'); | ||
} | ||
return { | ||
...initialProps, | ||
styles: ( | ||
<> | ||
{initialProps.styles} | ||
<style | ||
data-emotion-css={styles.ids.join(' ')} | ||
dangerouslySetInnerHTML={{ __html: styles.css }} | ||
/> | ||
|
||
{cssStyles && <style>{cssStyles}</style>} | ||
</> | ||
), | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<Html> | ||
<Head /> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { FC } from 'react'; | ||
import { GetStaticProps } from 'next'; | ||
import { DocType, defDocType } from '@component-controls/core'; | ||
import { DocPage } from '@component-controls/app'; | ||
import { Layout, store, getIndexPage } from '@component-controls/nextjs-plugin'; | ||
|
||
interface PageListProps { | ||
type: DocType; | ||
docId?: string; | ||
storyId?: string; | ||
} | ||
|
||
const HomePage: FC<PageListProps> = ({ type = defDocType, docId, storyId }) => { | ||
return ( | ||
<Layout docId={docId} storyId={storyId}> | ||
<DocPage type={type} /> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export const getStaticProps: GetStaticProps = async () => { | ||
const homePage = getIndexPage(store); | ||
const { docId = null, type = null, storyId = null } = homePage; | ||
return { props: { docId, type, storyId } }; | ||
}; | ||
|
||
export default HomePage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const { onPreRenderHTML } = require('./dist/gatsby-ssr'); | ||
exports.onPreRenderHTML = onPreRenderHTML; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { config } from '../../rollup-config'; | ||
|
||
export default config({ | ||
input: ['./src/index.ts', './src/gatsby-node.ts'], | ||
input: ['./src/index.ts', './src/gatsby-node.ts', './src/gatsby-ssr.tsx'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import fs from 'fs'; | ||
import React from 'react'; | ||
import { PreRenderHTMLArgs } from 'gatsby'; | ||
|
||
export const onPreRenderHTML = ({ | ||
getHeadComponents, | ||
replaceHeadComponents, | ||
}: PreRenderHTMLArgs) => { | ||
const headComponents = getHeadComponents(); | ||
const cssBundle = process.env.GATSBY_CC_CSS_FILENAME; | ||
if (cssBundle && fs.existsSync(cssBundle)) { | ||
const cssStyles = fs.readFileSync(cssBundle, 'utf8'); | ||
headComponents.push(<style key="controls-styles">{cssStyles}</style>); | ||
} | ||
replaceHeadComponents(headComponents); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters