R
R
Roman Rakzin2022-01-27 20:05:42
Angular
Roman Rakzin, 2022-01-27 20:05:42

How to solve the error when reloading angular/webpack chunks from the cache when the server has been updated?

I get an error when I update the prod server. After clearing the cache, this goes away, but how to solve this problem?
error photo -> prnt.sc/26k942n
ChuncLoadError

Package.json

"scripts": {
    "ng": "ng",
    "start": "cross-env NODE_ENV=dev webpack-dev-server --port=4200",
    "build": "npm run clean && webpack",
    "build:dev": "cross-env NODE_ENV=dev npm run build --sourcemap --aot --vendor-chunk --common-chunk --delete-output-path --buildOptimizer",
    "build:test": "cross-env NODE_ENV=test npm run build --sourcemap --aot --vendor-chunk --common-chunk --delete-output-path --buildOptimizer",
    "build:prod": "cross-env NODE_ENV=prod npm run build --aot",
    "clean": "npm cache clean --force && npm run rimraf -- dist",
    "rimraf": "rimraf",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "protractor ./protractor.conf.js",
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
  },

in webpack every time new chunks, but for some reason the links are taken from the cache. If I clean it, then the actual data is pulled up.

webpack.config.js

output: {
      path: path.join(process.cwd(), "dist"),
      chunkFilename: `[name].[hash].chunk.js`,
      filename: `[name].[hash].bundle.js`,
      crossOriginLoading: false
},

I set hash and contenthash for server prod.

So far I have found 2 solutions - to intercept the error, or to make a PWA application. Before PWA, you still need to study, and error interception is sort of like treating the effect, not the cause.
And what to do then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Rakzin, 2022-01-31
@TwoRS

Helped in webpack to put in output.
Replaced hash with contenthash (you can also use chunchash).
To optimize on Dev, I do not make this option, since it takes more time to build the contenthash

function generateOutputParams() {
  if (ENV === 'dev') {
    return {
      path: path.join(process.cwd(), "dist"),
      chunkFilename: `[name].[hash].chunk.js`,
      filename: `[name].[hash].bundle.js`,
      crossOriginLoading: false
    }
  }

  return {
    path: path.join(process.cwd(), "dist"),
    chunkFilename: `[name].[contenthash].chunk.js`,
    filename: `[name].[contenthash].bundle.js`,
    crossOriginLoading: false
  }
}

const outputValue = generateOutputParams();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question