我正在尝试使用 App Engine 在 Google Cloud Platform 上托管 Angular 应用程序。问题是在我成功部署它之后,我可以访问它大约 2-3 分钟,点击刷新并且没有收到 HTTP 错误 404,但是在这 2-3 分钟之后我在 Chrome 中收到以下错误(同事在他们的机器上也收到同样的错误):
我尝试使用以下方法构建我的 Angular 应用程序:
ng build --prod
ng build --prod --base-href sep6-app
Angular 应用文件夹结构:
- dist/
- sep6-app/
- app.yaml
我的 app.yaml:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
static_files: sep6-app/index.html
upload: sep6-app/index.html
- url: /.*
static_dir: sep6-app
在 GCP 中,我的存储桶包含:
- app.yaml
- sep6-app/
有一件事,如果我像这样在 app.yaml 中设置 URL,然后重新部署:
- url: /
static_files: sep6-app/index.html
upload: sep6-app/index.html
- url: /
static_dir: sep6-app
我可以毫无问题地访问我的应用程序,但是如果我刷新任何页面,我会收到 404 错误,据我所知,由于 Angular 处理路由的方式,这是预料之中的。但是我不明白为什么“- url:/.*”会在短时间内工作然后停止?
我正在使用(以防万一):
Angular CLI: 9.1.7
Angular: 9.1.9
关于为什么会发生这种情况以及如何解决这个问题,您有什么想法或想法吗?谢谢!
最佳答案
通过使用以下 app.yaml 设法让它工作:
runtime: python38
service: default
handlers:
- url: /(.*\.(gif|png|jpg|less|json|woff|woff2|ttf|eot|scss|css|js|ico|svg)(|\.map))$
static_files: sep6-app/\1
upload: sep6-app/(.*)(|\.map)
- url: /(.*)
static_files: sep6-app/index.html
upload: sep6-app/index.html
关于angular - GCP App Engine/Angular - 无法加载模块脚本 : The server responded with a non-JavaScript MIME type of "text/html",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61970036/