Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

javascript - How to load lazy loaded angular script chunks via Node.js

Switched angular 9 app to lazy loading to reduce main.js bundle size. Works fine locally and loads the module upon hitting the route.

e.g. downloads the script http://localhost:port/name-module.js

After running npm run build the chunks are produced for all routes.

Since I'm loading my Angular app via Node.js Express app I have a html file where I included the main scripts.

e.g.

<script src="./angular/runtime-es2015.js" type="module"></script>
<script src="./angular/polyfills-es2015.js" type="module"></script>
<script src="./angular/main-es2015.js" type="module"></script>

Obviously, I need to include my chunks as well but thought the code would be in main.js to fetch the required chunk when the corresponding route is hit? In other words I don't have to explicitly define the script for those chunks in the html file shown above.

The error which I'm getting:

Uncaught (in promise): ChunkLoadError: Loading chunk 2 failed.
(missing: http://localhost:5000/2-es2015.js)
ChunkLoadError: Loading chunk 2 failed.
(missing: http://localhost:5000/2-es2015.js)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...