【问题标题】:How to Cache Busting in Angular Universal如何在 Angular Universal 中缓存破坏
【发布时间】:2020-04-14 10:41:42
【问题描述】:

如何在 Angular Universal 中运行缓存清除命令? 我尝试运行 npm run build:ssr --output-hashing=all 但它没有改变/添加任何东西。

PACKAGE.json

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "compile:server": "webpack --config webpack.server.config.js --progress --colors",
    "serve:ssr": "node dist/server",
    "build:ssr": "npm run build:client-and-server-bundles && npm run compile:server",
    "build:client-and-server-bundles": "ng build --prod && ng run sample-project:server:production --bundleDependencies all"
  },

【问题讨论】:

  • build:ssr 中的package.json 是什么?
  • 您是否遇到任何错误?
  • 我没有错误@Plochie
  • 删除dist如果可用,然后在成功运行命令后,检查dist文件夹是否正在创建。
  • @Plochie 是的 dist 正在创建。我的意思是,我想放置缓存破坏,以便它会破坏旧缓存并替换为新缓存。问题是当我添加--output-hashing=all 时。它仍然没有改变任何东西。 dist 还是一样的

标签: angular caching browser-cache angular8 angular-universal


【解决方案1】:

当你运行npm run build:ssr --output-hashing=all时,npm只会执行npm run build:ssr,不会考虑提供的选项。

为此,在package.json 中添加以下脚本并运行npm run build:ssr:outhashall

...
"scripts" {
    "build:ssr:outhashall": "npm run build:client-and-server-bundles:outhashall && npm run compile:server",
    "build:client-and-server-bundles:outhashall": "ng build --prod --output-hashing=all && ng run sample-project:server:production --bundleDependencies all"
}
...

来自 cmets 的积分:

请注意--output-hashing=all 将生成带有散列名称的构建文件,因此如果您在项目中进行了一些更改,那么您将在每次构建后看到不同的文件名。

因此,在部署应用程序时,您需要删除现有文件并将新文件放在部署文件夹中。

【讨论】:

  • 嗨!我只是运行你的命令。我 git status 它,它只改变了 package.json :(
  • 它应该有几个js文件的变化,因为它会有散列?
  • 嗨!我已经检查了这个stackoverflow.com/questions/52061108/…。它并没有真正改变文件名。那么如何在每次构建时更改文件名,以便在部署时删除旧缓存?
  • 是的。那么,如果您进行了更改,缓存清除的目的是什么?
  • 这用于您使用ng build -prod --output-hashing none的情况,这将每次生成相同名称的文件。在这种情况下,您可能需要缓存清除。我希望我说得通。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2011-01-29
  • 2018-11-24
  • 2012-03-09
  • 1970-01-01
  • 2018-10-29
  • 2020-05-19
相关资源
最近更新 更多