简体   繁体   English

如何在 Next.js 中使用 CSS Houdini

[英]How do I use CSS Houdini in Next.js

I would like to use CSS Houdini in a Next.js project I am building.我想在我正在构建的 Next.js 项目中使用 CSS Houdini。 I have installed, via yarn , both the CSS Houdini package I want to use along with css-paint-polyfill .我已经通过yarn安装了 CSS Houdini package 我想与css-paint-polyfill polyfill 一起使用。 I am following the webpack instructions here https://houdini.how/usage我在这里遵循 webpack 说明https://houdini.how/usage

Here is my component:这是我的组件:

import 'css-paint-polyfill';
import workletURL from 'url-loader!css-houdini-lines';
import styles from './Separator.module.css';

CSS.paintWorklet.addModule(workletURL);

export default function Separator() {
  return <div className={styles.separator} />;
}

I am getting the dreaded我越来越害怕了

error - ReferenceError: window is not defined
at /home/tithos/code/web/new-tim/node_modules/css-paint-polyfill/dist/css-paint-polyfill.js:1:239

I tried putting the import for css-paint-polyfill in a useEffect, but the throw another error.我尝试将css-paint-polyfill polyfill 的导入放在 useEffect 中,但抛出另一个错误。 I even tried const DynamicComponent = dynamic(() => import('../components/hello')) from https://nextjs.org/docs/advanced-features/dynamic-import , but I did not know how to reference the library.我什至尝试了const DynamicComponent = dynamic(() => import('../components/hello')) from https://nextjs.org/docs/advanced-features/dynamic-import ,但我不知道如何参考图书馆。

Has any one solved this?有没有人解决这个问题?

Seems like CSS Paint Polyfill eagerly accesses window , which means it will throw an error when in an environment which has no window , such as server phase of Next.好像 CSS Paint Polyfill 急切地访问window ,这意味着它在没有window的环境中会抛出错误,例如 Next 的服务器阶段。 There are multiple things you can do.你可以做很多事情。

  1. Modify your next.config.js to stub the aforementioned module when for Webpack when isServer is true .isServertrue时,修改next.config.js以对 Webpack 存根上述模块。 You can follow this page of the Next docs for this.您可以为此关注Next 文档的此页面
  2. Create a dynamic component that's only imported on the client (I see you've tried this, but it should have worked, so maybe if you shared what you did in this regard, I could work out what's wrong with your approach).创建一个仅在客户端上导入的动态组件(我看到你已经尝试过了,但它应该可以工作,所以如果你分享了你在这方面所做的事情,我可以找出你的方法有什么问题)。
  3. Create an issue and send in a PR to the repository where the Polyfill is hosted to lazily access window depending on whether it's available or not.创建一个问题并将 PR 发送到托管 Polyfill 的存储库,以根据window是否可用而延迟访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM