当我在生产中加载 ReactJS 项目时,我在浏览器检查器(Chrome、Brave、Safari)中收到以下错误:
Refused to apply inline style because it violates the following Content Security Policy directive: "style-src-elem 'self' https://fonts.googleapis.com". Either the 'unsafe-inline' keyword, a hash ('sha256-TMIFk5ZjGNpczjRE6YVaBrPXVNzANj9JRK+w2bh9TX8='), or a nonce ('nonce-...') is required to enable inline execution.
Run Code Online (Sandbox Code Playgroud)
我使用以下命令构建ReactJS项目(它解决了内联脚本问题,但没有解决内联样式):
INLINE_RUNTIME_CHUNK=false npm run build
Run Code Online (Sandbox Code Playgroud)
这是 CSP 策略部分,参考样式:
Header set Content-Security-Policy "default-src 'self'; style-src 'self'; style-src-elem 'self' https://fonts.googleapis.com; ..."
Run Code Online (Sandbox Code Playgroud)
我相信问题发生在构建阶段,当 React 生成块文件(名称如“ 2.3c013fe3.chunk.js ”)并在代码中嵌入内联样式时,尽管我在源代码中的任何地方都没有使用内联样式。所有样式都包含在 fileX.module.css 中并导入到 tsx 文件中,如下例所示:
示例.tsx
import styles from './Example.module.css';
const Example = () => (
<div className={styles.container}>
Hello world …Run Code Online (Sandbox Code Playgroud) 我有以下代码在 TypeScript 中使用 Redux 定义类型化选择器:
import { useSelector, TypedUseSelectorHook } from 'react-redux';
export interface RootState {
user: {
account: string;
}
};
export const useTypedSelector: TypedUseSelectorHook<RootState> = useSelector;
Run Code Online (Sandbox Code Playgroud)
现在我看到它TypedUseSelectorHook已被弃用,应替换为createSelectorHook<State, Action>().
不知道如何更改它,是否也Action必须在那里。有什么建议吗?