[英]How to load manifest.json in React JS app
我有一個帶有 Webpack(而不是 CRA)的 React 應用程序設置。 我試圖通過添加一個與 index.html 並行的 manifest.json 文件來使其漸進式。 在運行應用程序時,我遇到以下錯誤。
GET http://localhost:8090/manifest.json 404 (Not Found)
此外,如果我在運行build
命令后查看dist
文件夾,則沒有manifest.json
文件復制到dist
。 我曾嘗試使用 webpack 的file-loader
插件,但它也不起作用。 下面是webpack.config.js
片段:
module: {
rules: [
{ test: /\.(js)$/, exclude: /node_modules/, use: 'babel-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
//{ test: /\.html$/, use: ['html-loader'] },
{ test: /\.(svg|png|jpg|jpeg|gif)$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: 'assets/img' } } },
{ test: /\.(json)$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: './' } } }
]
},
下面是index.html
片段:
<head>
<link rel="manifest" href="./manifest.json" />
</head>
我究竟做錯了什么?
您可以執行以下步驟來加載manifest.json
:
manifest.json
放在項目根目錄的static
文件夾中。index.html
: <link rel="manifest" href="/manifest.json">
要知道您的文件是否已加載,請檢查 Chrome DevTools,在 Application => Manifest
如果上面的答案對您不起作用(就像我的情況一樣),請嘗試app-manifest-webpack-plugin 。
當您運行npm run build
或yarn run build
時,它會為您構建manifest.json文件。
它還具有多種配置,使其非常靈活。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.