gpt4 book ai didi

javascript - `[WDS] Disconnected` ReactJS,网络包

转载 作者:行者123 更新时间:2023-11-30 14:42:01 32 4
gpt4 key购买 nike

运行 yarn start 后出现此错误。我正在尝试使用 webpackBabel

开发一个 hello-word ReactJS 应用

[WDS] 已断开连接!
内容安全策略:该页面的设置阻止了在 http://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.12/semantic.min.css(“style-src”)加载资源。
内容安全策略:该页面的设置阻止了 blob 资源的加载:http://localhost:3000/edcc77c5-a0d7-4b24-9f6d-5e740ffd77df(“style-src”)。

这是我的 webpack.config.js 文件

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const port = process.env.PORT || 3000;
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
filename: 'bundle.[hash].js'
},
devtool: 'inline-source-map',
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: ['babel-loader']
},
{
test: /\.css$/,
use: [
{
loader: 'style-loader'
},
{
loader: 'css-loader',
options: {
modules: true,
camelCase: true,
sourceMap: true
}
}
]
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: 'public/index.html',
favicon: 'public/favicon.ico'
})
],
devServer: {
host: 'localhost',
port: port,
historyApiFallback: true,
open: true
}
};

这是我的 package.jon 文件

{
"scripts": {
"start": "webpack-dev-server"
},
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-prop-types": "^0.4.0",
"react-router-dom": "^4.2.2",
"semantic-ui-react": "^0.79.0"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.1.0",
"style-loader": "^0.20.3",
"webpack": "^4.2.0",
"webpack-cli": "^2.0.13",
"webpack-dev-server": "^3.1.1"
}
}

公共(public)/index.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.12/semantic.min.css"></link>
<title>webpack-for-react</title>
</head>

<body>
<div id="root"></div>
</body>

</html>

我试图解决这个错误。但我做不到。如果有人有线索,请帮助我。我认为这个错误来自 webpack.config.js 文件。

最佳答案

问题是您使用的Content-Security-Policy

现在您只允许本地 css 文件和内联样式。

当您尝试导入 semantic.min.css 时,cloudflare 不是允许的主机。

所以你可能需要添加

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline' http://cdnjs.cloudflare.com; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

我建议你阅读CSP documentation了解更多信息。

并尝试在开发过程中使用 Content-Security-Policy-Report-Only,这样您就只会收到违规报告。

关于javascript - `[WDS] Disconnected` ReactJS,网络包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49498437/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com