我的静态文件是从 Amazon S3 提供的。我正在为 CI/CD 使用 circleci 来部署我的文件。这些文件已按预期添加到 s3 存储桶中,但是当我转到静态网站链接时,我看到一个空白页面,其中包含控制台错误“Uncaught SyntaxError: Unexpected token ILLEGAL”。
通过一些调查(以这个链接为例),我发现问题出在元数据中。js 文件添加内容类型application/javascript而不是text/js. 当我手动进入我的存储桶并单击各个文件时,转到属性,然后是元数据,然后删除现有的内容类型并为所有 js文件添加Content-Type值为 的行,网站开始正常显示。唯一不是要添加的 js 文件的文件是 ,并且该文件获得了正确的元数据 ( )。text/jsgzindex.htmltext/html
当我切换内容类型时,我感到非常兴奋,认为我解决了这个问题。但是,当我重新部署应用程序时,再次添加了相同的错误元数据。我找到了一个解决方案,即在部署期间添加content-type值为 的标签。text/js这确实可以获取与js.gz文件关联的正确内容类型,但 index.html 文件也正在部署中。由于 index.html 文件也获取了该text/js文件,因此我们收到错误“无法访问此站点”。
有人对解决方案有任何想法吗?我正在寻找一种动态选择一种或另一种内容类型的方法,但我想知道是否有更好的解决方案。该应用程序正在通过 CloudFront 分发。
感谢您的任何帮助!
这是我的 circleci config.yml 的结尾,部署发生在:
- run: sudo apt-get update && sudo apt-get install -y python-dev
- run: sudo curl -O https://bootstrap.pypa.io/get-pip.py
- run: sudo python get-pip.py
- run: sudo pip install awscli --upgrade
- run: aws --version
- run: aws s3 ls
- run: yarn run deploy
该行yarn run deploy运行:
yarn build && aws s3 sync dist/ s3://[bucket] --delete --content-encoding gzip
添加的更新行content-type:
yarn build && aws s3 sync dist/ s3://[bucket] --delete --content-encoding gzip --content-type text/js
更新:
我想我通过添加标签来通过扩展名包含或排除文件找到了解决方案,如下所示。
"deploy-html": "yarn build && aws s3 sync dist/ s3://[bucket name] --exclude *.html --include *.gz --delete --content-encoding gzip --content-type text/js",
"deploy-js": "yarn build && aws s3 sync dist/ s3://[bucket name] --exclude *.js --include *.html --content-encoding gzip --content-type text/html",
"deploy": "yarn build && yarn run deploy-html && yarn run deploy-js"
但是,这使它们通常不可用,因此会导致错误:
Unknown options: server.js,webpack.common.js,webpack.dev.js,webpack.prod.js