Dom*_*iro 5 typescript session-replay yandex-metrika styled-components next.js
我正在使用 Next.js 和样式组件的项目中工作。在我的文件 [slug].tsx 中:
export default function ProductDetails({ product }: IProductDetailsProps) {
const router = useRouter();
if (router.isFallback) {
return (
<LoadingContainer>
<ReactLoading color="#000" type="bubbles" />
</LoadingContainer>
);
}
return (
<>
{product._id ? (
<>
<Header />
<Container>
<ProductPath>
<Link href="/">Home</Link>
<Link href="/shop">Shop</Link>
{product.categoryId && (
<Link href={`/shop/${product.categoryId.slug}`}>
{product.categoryId.name}
</Link>
)}
<span>{product.name}</span>
</ProductPath>
<ProductInfo product={product} />
</Container>
</>
) : (
<Error>An unexpected error has occurred.</Error>
)}
</>
);
}
Run Code Online (Sandbox Code Playgroud)
大多数标签来自 styled-components,例如:
export const LoadingContainer = styled.div`
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
`;
export const Error = styled.div`
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
`;
export const Container = styled.div``;
export const ProductPath = styled.p`
display: block;
padding: 22px 32px;
font-size: 14px;
background-color: #f1f1f1;
a {
text-decoration: none;
color: #09c;
transition: color 0.2s;
&:hover {
color: #fcb800;
}
}
a + a::before,
a + span::before {
content: '/';
color: #000;
cursor: auto;
margin: 0 10px;
}
`;
Run Code Online (Sandbox Code Playgroud)
我遵循了 Next.js 的 styled-components 文档(https://styled-components.com/docs/advanced#nextjs xtjs),我的 .babelrc:
{
"presets": ["next/babel"],
"plugins": [["styled-components", { "ssr": true }]]
}
Run Code Online (Sandbox Code Playgroud)
_document.tsx:
import Document, { DocumentContext } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}
}
Run Code Online (Sandbox Code Playgroud)
该项目需要 Yandex Session Replay 工作,但是当我的应用程序在生产环境中加载时,控制台中没有错误,并且 Yandex Session Replay 不会渲染 CSS:
有什么建议么?
谢谢。
尝试在组件内添加渲染函数,_document如下所示:
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext
} from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />)
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1669 次 |
| 最近记录: |