Why Reactjs application considers css files type as 'text/html'

Viewed 88

I am trying to apply css files into the application but as the error I receive states that the sheets were denied due to their MIME type - somehow it's read them as 'text/html'.

What I have tried so far:

  • Defined the sheets inside index.html as <link type="text/css" rel="stylesheet" href="zoom_main.css" /> <link type="text/css" rel="stylesheet" href="zoom_form.css" />

  • Moved the css files into src folder and imported them from index.js (entry point)

  • Used PUBLIC_URL <link type="text/css" rel="stylesheet" href="%PUBLIC_URL%/zoom_form.css" />

enter image description here

enter image description here

enter image description here

UseStyles.js

const UseStyles = stylePath => {
  useEffect(() => {

    const head = document.head;
    const link = document.createElement("link");

    link.type = "text/css";
    link.rel = "stylesheet";
    link.href = stylePath;

    head.appendChild(link);

    return () => {
      head.removeChild(link);
    };
  }, [stylePath]);

  return stylePath;
};

export default UseStyles;

ZoomContainer.js

const PATH = {
  formCssUrl: "zoom_form.css",
  mainCssUrl: "zoom_main.css"
};

const zoomConfig = {
  apiKey: "",
  apiSecret: "",
  userName: "",
  userEmail: "",
  leaveUrl: "https://zoom.us/",

  meetingNumber: "",
  passWord: "",
  role: 0
};

const ZoomContainer = () => {
  const [isSubmitted, setIsSubmitted] = useState({
    stylePath: PATH.formCssUrl,
    status: false
  });
  console.log(PATH);
  const { stylePath } = isSubmitted;

  const handleSubmit = e => {
    e.preventDefault();

    setIsSubmitted({
      stylePath: PATH.mainCssUrl,
      status: true
    });
  };

  UseStyles(stylePath);

  return (
    <div className="App">
      <button onClick={handleSubmit}>Start Meeting</button>
      <Zoom isSubmitted={isSubmitted} meetConfig={zoomConfig} />
    </div>
  );
};

export default ZoomContainer;

index.js

const { persistor, store } = configureStore();
render(
  <ReduxProvider store={store}>
    <PersistGate loading={null} persistor={persistor}>
      <App />
    </PersistGate>
  </ReduxProvider>,
  document.getElementById("root")
);

serviceWorker.unregister();

index.html (is found in public folder)

<!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" />

    <link
      type="text/css"
      rel="stylesheet"
      href="node_modules/@zoomus/websdk/dist/css/react-select.css"
    />
    <link
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
      crossorigin="anonymous"
    />
    <link
      type="text/css"
      rel="stylesheet"
      href="https://source.zoom.us/1.7.9/css/react-select.css"
    />
    <script src="https://source.zoom.us/1.7.9/lib/vendor/jquery.min.js"></script>
    <script src="https://source.zoom.us/1.7.9/lib/vendor/lodash.min.js"></script>

    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <title>Tena</title>
  </head>

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

webpack.common.js

const path = require("path");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: ["babel-loader", "eslint-loader"]
      },
      {
        test: /(\.(css|scss))$/,
        use: ["style-loader", "css-loader", "sass-loader"]
      },
      {
        test: /\.svg$/,
        use: [
          {
            loader: "babel-loader"
          },
          {
            loader: "react-svg-loader",
            options: {
              jsx: true // true outputs JSX tags
            }
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        loader: "file-loader",
        options: {
          name: "[name].[ext]"
        }
      }
    ]
  },
  resolve: {
    extensions: [".webpack.js", ".web.js", ".ts", "*", ".js", ".json"]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: "Tena",
      template: "./public/index.html"
    })
  ],
  output: {
    path: path.resolve(__dirname, "../", "build"),
    publicPath: "/",
    filename: "bundle.js"
  },

  devServer: {
    stats: "minimal",
    overlay: true,
    historyApiFallback: true,
    disableHostCheck: true,
    headers: { "Access-Control-Allow-Origin": "*" },
    https: false
  }
};

package.json

{
 ...
  "scripts": {
    "start": "webpack-dev-server --config build-utils/webpack.config.js --env.env=dev --port 3000",
    "build": "webpack --config build-utils/webpack.config.js --env.env=prod",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
 ...
}
1 Answers

I needed to use copy-webpack-plugin to copy the style sheets to the build directory from the public folder.

webpack.common.js

const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
  ...
  plugins: [
    ...
    new CopyPlugin({
      patterns: [
        { from: "./public/zoom_form.css", to: "." },
        { from: "./public/zoom_main.css", to: "." }
      ]
    })
  ],
 ...
};
Related