Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
// 劫持原本的renderPage函数并重写
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () => originalRenderPage({
// 根App组件
enhanceApp: (App) => (props) => sheet.collectStyles(),
})
// 如果重写了getInitialProps 就要把这段逻辑重新实现
const props = await Document.getInitialProps(ctx)
return {
...props,
styles: (
<>
{props.styles}
{sheet.getStyleElement()}
),
}
} finally {
sheet.seal()
}
}
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: (
{initialProps.styles}
{sheets.getStyleElement()}
),
};
};
// Step 2: Retrieve styles from components in the page
// Render app and page and get the context of the page with collected side effects
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props =>
sheet.collectStyles(
<>
)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Step 3: Extract the styles as
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheets.collect()
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
{initialProps.styles}
{sheets.getStyleElement()}
]
};
};
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles()
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
)
};
} finally {
sheet.seal();
}
}
static async getInitialProps(ctx) {
const renderer = getFelaRenderer()
const originalRenderPage = ctx.renderPage
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => ,
})
const initialProps = await Document.getInitialProps(ctx)
const sheetList = renderToSheetList(renderer)
return {
...initialProps,
sheetList,
}
}
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx);
return { ...initialProps };
}
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
static async getInitialProps (ctx) {
const initialProps = await Document.getInitialProps(ctx)
return client.fetch('*[_id == "global-config"] {lang}.lang[0]').then(lang => {
return {...initialProps, lang}
})
}
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles()
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: [
...(Array.isArray(initialProps.styles) ? initialProps.styles : []),
...sheet.getStyleElement()
]
};
}
}