Skip to content

Commit

Permalink
Add options for defaultGetInitialProps
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 17, 2021
1 parent 3e0d408 commit 7484ada
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
37 changes: 14 additions & 23 deletions examples/styled-jsx-with-csp/pages/_document.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { nanoid } from 'nanoid'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
class CustomDocument extends Document {
static async getInitialProps(ctx) {
const nonce = nanoid()
const jsxStyleRegistry = createStyleRegistry()

// https://github.com/vercel/next.js/blob/canary/packages/next/pages/_document.tsx#L89
const { html, head } = await ctx.renderPage()

// Adds `nonce` to style tags on Server Side Rendering
const styles = jsxStyleRegistry.styles({ nonce })
const docProps = await ctx.defaultGetInitialProps(ctx, { nonce: !!nonce })

let contentSecurityPolicy = ''
if (process.env.NODE_ENV === 'production') {
Expand All @@ -24,25 +17,23 @@ class CustomDocument extends Document {

ctx.res.setHeader('Content-Security-Policy', contentSecurityPolicy)

return { html, head, styles, nonce }
return { ...docProps, nonce }
}

render() {
return (
<StyleRegistry registry={this.props.registry}>
<Html>
<Head>
{/* Styled-JSX will add this `nonce` to style tags on Client Side Rendering */}
{/* https://github.com/vercel/styled-jsx/blob/master/src/lib/stylesheet.js#L31 */}
{/* https://github.com/vercel/styled-jsx/blob/master/src/lib/stylesheet.js#L240 */}
<meta property="csp-nonce" content={this.props.nonce} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
</StyleRegistry>
<Html>
<Head>
{/* Styled-JSX will add this `nonce` to style tags on Client Side Rendering */}
{/* https://github.com/vercel/styled-jsx/blob/master/src/lib/stylesheet.js#L31 */}
{/* https://github.com/vercel/styled-jsx/blob/master/src/lib/stylesheet.js#L240 */}
<meta property="csp-nonce" content={this.props.nonce} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/next/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,15 @@ export async function renderToHTML(
)
},
defaultGetInitialProps: async (
docCtx: DocumentContext
docCtx: DocumentContext,
options: { nonce?: boolean } = {}
): Promise<DocumentInitialProps> => {
const enhanceApp = (AppComp: any) => {
return (props: any) => <AppComp {...props} />
}

const { html, head } = await docCtx.renderPage({ enhanceApp })
const styles = jsxStyleRegistry.styles()
const styles = jsxStyleRegistry.styles({ nonce: options.nonce })
return { html, head, styles }
},
}
Expand Down

0 comments on commit 7484ada

Please sign in to comment.