[英]“The php script was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type” error
[英]The script from <url> was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type
我有一個 Flask 后端,我的 webpack 配置看起來像這樣(去掉了 sass/css/babel)。
const webpack = require('webpack');
const resolve = require('path').resolve;
const HtmlWebpackPlugin = require('html-webpack-plugin');
const isDevelopment = process.env.NODE_ENV === 'development';
const config = {
entry: {
main: __dirname + '/js/index.jsx'
},
devtool: 'eval-source-map',
output:{
path: resolve('../public'),
filename: '[name].[chunkhash].js'
},
resolve: {
extensions: ['.js','.jsx','.scss']
},
...etc ...etc ...etc
plugins: [
new HtmlWebpackPlugin({
hash: true,
inject: false,
hash: true,
template: './index.html',
filename: 'index.html'
})
]
};
module.exports = config;
當我執行 webpack-cli 命令webpack --progress -d --config webpack.config.js --watch
, index.html
在../public
正確創建, [name].[chunkhash].js
。 例子:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="app" />
<script src="main.6e9f93766744ff757148.js?58e0a4a8c99dee0bfe93"></script>
</body>
</html>
在 Flask 中,我指定:
app = Flask(__name__,
template_folder="./templates/public")
因此,當您點擊localhost:<port>
,它應該呈現application/templates/public/index.html
(我在application/templates/src
進行開發,然后執行 webpack 命令,該輸出被放入application/templates/public
)。
現在,當我 fun flask run
,網絡中會出現以下輸出:
127.0.0.1 - - [07/Mar/2020 19:40:53] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Mar/2020 19:40:53] "GET /main.6e9f93766744ff757148.js?58e0a4a8c99dee0bfe93 HTTP/1.1" 404 -
在開發人員工具中,我可以在響應中看到正確的 html 和對 javascript 的引用。
但控制台指出:
The script from <url>/<file>.js was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
所以 Flask 正在渲染正確的html
文件,但找不到 javascript。 我怎樣才能讓 Flask 找到這個 javascript?
謝謝!
事實證明這是 Flask 的問題,而不是 webpack 或其他任何東西。 我已經將我的 static_folder 指定為 static/dist,而它應該剛剛設置為 static。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.