MP4 file not loading on React / Rails app

老子叫甜甜 提交于 2021-01-29 15:13:53

问题


I'm getting this error. From what I can google, it needs to configure the webpack file but in this rails app it's a webpacker.yml file. CRA looks like it works fine when using the usual tag. Any help would be appreciated.

./app/javascript/components/User/cover.mp4 1:0 Module parse failed: Unexpected character '' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

  "dependencies": {
"@babel/preset-react": "^7.10.4",
"@rails/actioncable": "^6.0.0-alpha",
"@rails/activestorage": "^6.0.0-alpha",
"@rails/ujs": "^6.0.0-alpha",
"@rails/webpacker": "4.2.2",
"animate.css": "^4.1.0",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"bootstrap": "^4.5.0",
"jquery": "^3.5.1",
"popper.js": "^1.16.1",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-animate-on-scroll": "^2.1.5",
"react-animations": "^1.0.0",
"react-bootstrap-sweetalert": "^5.1.15",
"react-dom": "^16.13.1",
"react-ga": "^3.1.2",
"react-router-dom": "^5.2.0",
"react-scroll": "^1.8.0",
"styled-components": "^5.1.1",
"turbolinks": "^5.2.0"

}, "version": "0.1.0", "devDependencies": { "webpack-dev-server": "^3.11.0" }


回答1:


in config/webpacker.yml under static_assets_extensions add .mp4

static_assets_extensions
  - .mp4
  ...

create a new folder app/javascripts/static

then

const file = require('static/file.mp4')

render() {
  return (
    <div>
      <video controls>
        <source src={file} />
      </video>
  ...

Note: You should use CDN to deliver videos



来源:https://stackoverflow.com/questions/64598871/mp4-file-not-loading-on-react-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!