You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改firebase deploy默认构建步骤,部署禁用Service Worker的Flutter Web

修改Firebase Deploy默认构建命令的两种方法

方法1:直接部署预构建产物

把Firebase Hosting的部署源改成Flutter构建后的目录,这样它就不会自动触发构建了:

修改firebase.json中hosting的source字段为"build/web":

{
  "flutter": {
    "platforms": {
      "dart": {
        "lib/firebase_options.dart": {
          "projectId": "XXXXXXXXXXXX",
          "configurations": {
            "web": "XXXXXXXXXXXXXXXXXXXXXXX"
          }
        }
      }
    }
  },
  "hosting": {
    "source": "build/web",
    "headers": [
      {
        "source": "/**",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "no-cache, no-store, must-revalidate"
          }
        ]
      }
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "frameworksBackend": {
      "region": "us-central1"
    }
  }
}

操作步骤:

  • 先手动执行自定义构建命令:flutter build web --release --pwa-strategy none
  • 再运行firebase deploy,此时会直接部署build/web里的内容,不会重新构建。

方法2:让Deploy自动执行自定义构建

如果不想每次手动跑构建,可以给hosting添加predeploy脚本,让firebase deploy先执行你的构建命令再部署:

修改firebase.json,在hosting节点下加入predeploy数组:

{
  "flutter": {
    "platforms": {
      "dart": {
        "lib/firebase_options.dart": {
          "projectId": "XXXXXXXXXXXX",
          "configurations": {
            "web": "XXXXXXXXXXXXXXXXXXXXXXX"
          }
        }
      }
    },
    "hosting": {
      "source": ".",
      "predeploy": [
        "flutter build web --release --pwa-strategy none"
      ],
      "headers": [
        {
          "source": "/**",
          "headers": [
            {
              "key": "Cache-Control",
              "value": "no-cache, no-store, must-revalidate"
            }
          ]
        }
      ],
      "ignore": [
        "firebase.json",
        "**/.*",
        "**/node_modules/**"
      ],
      "rewrites": [
        {
          "source": "**",
          "destination": "/index.html"
        }
      ],
      "frameworksBackend": {
        "region": "us-central1"
      }
    }
  }
}

之后直接运行firebase deploy,CLI会自动先执行你指定的构建命令,再部署生成的产物,不会使用默认的构建逻辑。

内容的提问来源于stack exchange,提问作者bf8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:09:53