I am trying to load a Unity Webgl build in my react component using react-unity-webgl. However, the MIME type of the loader.js file is text/html when I open the networks panel. Therefore, the loader.js file won't run as it's considered an HTML file. I am running on a Windows development server using create react app.
The error shows:
expected expression, got '<'
The script from “http://localhost:3000/game/samplegame/unity/samplegame/Build/samplegame.loader.js” was loaded even though its MIME type (“text/html”) is not a valid JavaScript MIME type.
React Component:
import React from 'react';
import { Unity, useUnityContext } from 'react-unity-webgl';
const Samplegame = () => {
const { unityProvider } = useUnityContext({
loaderUrl: "unity/samplegame/Build/samplegame.loader.js",
dataUrl: "unity/samplegame/Build/samplegame.data.gz",
frameworkUrl: "unity/samplegame/Build/samplegame.js.gz",
codeUrl: "unity/samplegame/Build/samplegame.wasm.gz",
});
return (
<div>
<h2>Sample Game</h2>
<Unity unityProvider={unityProvider} style={{ width: "960px", height: "600px" }} />
</div>
);
};
export default Samplegame;
I tried using react-app-rewired to adjust the Webpack configuration, but it doesn't make a difference.
config-overrides.js
const { override, addWebpackPlugin } = require('customize-cra');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const path = require('path');
module.exports = override(
addWebpackPlugin(
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, 'public/unity'),
to: path.resolve(__dirname, 'build/unity'),
transform(content, filePath) {
if (filePath.endsWith('.js')) {
return Buffer.from(content.toString().replace(/text\/plain/g, 'application/javascript'));
}
return content;
},
},
],
})
)
);