【问题标题】:Unable to load language.json and css files in Firebase hosting无法在 Firebase 托管中加载 language.json 和 css 文件
【发布时间】:2020-06-01 07:16:27
【问题描述】:

我在 StackBlitz 中开发了我的应用程序,并包含 ngx-translate 包。 在部署到火力基地之前,它运行良好......基本上所有东西都坏了(抽泣......) 我尽力详细说明我的配置和错误日志,因为我自己无法解决。 我也通过互联网挖掘,但没有运气解决其中任何一个。 很抱歉这篇冗长的帖子。

index.html

<link rel="stylesheet" type="text/css" href="/node_modules/primeicons/primeicons.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/themes/nova-light/theme.css" />
<link rel="stylesheet" type="text/css" href="/node_modules/primeng/resources/primeng.min.css" />

上述代码在 Firebase 中出错,但在 StackBlitz 中没有(完美运行)

Resource interpreted as Stylesheet but transferred with MIME type text/html: "https://fireforgez.firebaseapp.com/node_modules/primeng/resources/themes/nova-light/theme.css".
Resource interpreted as Stylesheet but transferred with MIME type text/html: "https://fireforgez.firebaseapp.com/node_modules/primeng/resources/primeng.min.css".
Resource interpreted as Stylesheet but transferred with MIME type text/html: "https://fireforgez.firebaseapp.com/node_modules/primeicons/primeicons.css".

为了摆脱它,我做了一个愚蠢的补丁,用实际的网址替换它

<link rel="stylesheet" type="text/css" href="https://unpkg.com/primeicons@4.0.0-rc.2/primeicons.css" />
<link rel="stylesheet" type="text/css" href="https://unpkg.com/primeng@9.1.0/resources/themes/nova-light/theme.css" />
<link rel="stylesheet" type="text/css" href="https://unpkg.com/primeng@9.1.0/resources/primeng.min.css" />

PrimeFlex 包也遇到了类似的错误

    DevTools failed to load SourceMap: Could not parse content for
https://fireforgez.firebaseapp.com/primeflex.css.map: Unexpected token < in JSON at position 0

我比较了 StackBlitz 和 Firebase,样式表文件都是可加载的。但是当我用 firebase 托管 url 替换时,所有 .css 都会恢复到 firebase 主 url 例如 https://fireforgez.firebaseapp.com/node_modules/primeng/resources/themes/nova-light/theme.css 将重定向到 https://fireforgez.firebaseapp.com/ 并产生了一系列错误

Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'node_modules/primeng/resources/themes/nova-light/theme.css'
Error: Cannot match any routes. URL Segment: 'node_modules/primeng/resources/themes/nova-light/theme.css'
    at ApplyRedirects.noMatchError (router.umd.js:2284) [angular]
    at CatchSubscriber.eval [as selector] (router.umd.js:2265) [angular]
    at CatchSubscriber.error (catchError.js:48) [angular]
    at MapSubscriber.Subscriber._error (Subscriber.js:92) [angular]
    at MapSubscriber.Subscriber.error (Subscriber.js:72) [angular]
    at MapSubscriber.Subscriber._error (Subscriber.js:92) [angular]
    at MapSubscriber.Subscriber.error (Subscriber.js:72) [angular]
    at MapSubscriber.Subscriber._error (Subscriber.js:92) [angular]
    at MapSubscriber.Subscriber.error (Subscriber.js:72) [angular]
    at ThrowIfEmptySubscriber.Subscriber._error (Subscriber.js:92) [angular]
    at ThrowIfEmptySubscriber.Subscriber.error (Subscriber.js:72) [angular]
    at TakeLastSubscriber.Subscriber._error (Subscriber.js:92) [angular]
    at TakeLastSubscriber.Subscriber.error (Subscriber.js:72) [angular]
    at MergeMapSubscriber.OuterSubscriber.notifyError (OuterSubscriber.js:26) [angular]
    at resolvePromise (zone.js:816) [angular]
    at resolvePromise (zone.js:768) [angular]
    at eval (zone.js:878) [angular]
    at Object.onInvokeTask (core.umd.js:19397) [angular]
    at drainMicroTaskQueue (zone.js:591) [<root>]

对于 ngx-translate,它在 StackBlitz 中也可以正常工作,但无法加载语言文件。

SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.onLoad (https://fireforgez.firebaseapp.com/turbo_modules/@angular/common@9.1.9/bundles/common-http.umd.js:1390:55)
at ZoneDelegate.invokeTask (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:411:35)
at Object.onInvokeTask (https://fireforgez.firebaseapp.com/turbo_modules/@angular/core@9.1.9/bundles/core.umd.js:19397:37)
at ZoneDelegate.invokeTask (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:410:40)
at Zone.runTask (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:178:51)
at ZoneTask.invokeTask [as invoke] (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:493:38)
at invokeTask (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:1633:18)
at XMLHttpRequest.globalZoneAwareCallback (https://fireforgez.firebaseapp.com/turbo_modules/zone.js@0.10.3/dist/zone.js:1670:25)
...
message: "Http failure during parsing for https://fireforgez.firebaseapp.com/assets/i18n/en.json"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "https://fireforgez.firebaseapp.com/assets/i18n/en.json"

这是我完整的 angular.json 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/demo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "node_modules/primeicons/primeicons.css",
              "node_modules/primeng/resources/themes/nova-light/theme.css",
              "node_modules/primeng/resources/primeng.min.css",
              "../node_modules/quill/dist/quill.core.css",
              "../node_modules/quill/dist/quill.snow.css",
              "node_modules/primeflex/primeflex.css"
            ],
            "scripts": [
              "../node_modules/quill/dist/quill.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "demo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "demo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "styles.css"
            ],
            "scripts": [
              "../node_modules/quill/dist/quill.js"
            ],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "demo"
}

【问题讨论】:

    标签: firebase primeng ngx-translate stackblitz


    【解决方案1】:

    我非常感谢 StackBlitz 的所有者 Eric,他非常感激和耐心地解释说 Firebase 部署仍处于测试阶段(在那之前我不知道!...) https://github.com/stackblitz/core/issues/1254#issuecomment-636860107

    我一直在尝试所有可能的方法来在 SB 中解决它以进行部署,但没有运气。在他的建议下,我绞尽脑汁,将项目降到本地,并在本地手动构建损坏的依赖项。之后我部署到 firebase,现在一切正常!

    所以目前,如果你们有严肃的项目并且需要从 SB 部署到 Firebase,我建议你等到部署得到稳定版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多