Angular 7应用PCF部署后主页面加载缓慢及main.js优化咨询
Hey there, let's dig into fixing that massive 30-second load time your Angular 7 app is experiencing on PCF (compared to 2 seconds locally). We'll break this down into two main areas: shrinking your main.js bundle and optimizing deployment-specific performance gaps.
First: Reduce main.js Bundle Size
Your main.js is sitting at 1.27MB, which isn't huge on its own—but combined with other assets and possible network/configuration issues on PCF, it's contributing to the slowdown. Here's how to trim it:
1. Analyze Your Bundle to Find Bloat
First, identify exactly what's taking up space in main.js:
- Run
ng build --prod --stats-jsonto generate astats.jsonfile in yourdistfolder. - Install the webpack bundle analyzer temporarily:
npm install -D webpack-bundle-analyzer - Run
npx webpack-bundle-analyzer dist/stats.jsonto open a visual breakdown of your bundle. This will show you which libraries or components are the biggest culprits.
2. Clean Up Redundant Dependencies & Code
Looking at your package.json and AppModule.ts, there are quick wins here:
- Remove outdated dependencies: You have
@angular/animation@4.0.0-beta.8alongside@angular/animations@7.2.1—this old package is redundant. Delete it frompackage.jsonand runnpm install. - Eliminate duplicate imports: Your
AppModuleimportsBrowserModuletwice—remove one instance (keep the first one paired withBrowserAnimationsModule). - Deduplicate UI libraries: You're using
bootstrap,mdbootstrap, andangular-bootstrap-mdall at once. These overlap heavily—pick one library that covers your needs and remove the others to cut down on redundant CSS/JS.
3. Optimize Third-Party Library Imports
Many of your dependencies are likely being bundled into main.js unnecessarily:
- Lazy-load heavy libraries per module: Libraries like
@agm/core(Google Maps),ng2-pdf-viewer, andngx-lightboxshouldn't be imported inAppModule. Instead, import them only in the lazy-loaded modules where they're used—this keeps them out of the initial main.js bundle. - Use partial imports for UI libraries: Instead of importing the full
MDBBootstrapModule.forRoot(), import only the specific modules you need (e.g.,ButtonsModule,CardsModule,CarouselModule). This drastically reduces the amount of code added to your bundle. - RxJS Tree Shaking: Ensure you're using RxJS 6+ syntax correctly—import operators from
rxjs/operators(e.g.,import { map } from 'rxjs/operators') instead of the oldrxjs/add/operator/mapstyle, which prevents Tree Shaking from working.
4. Enable Gzip Compression on PCF
Even with a smaller main.js, transferring uncompressed files over the network is slow. PCF can handle gzip compression for your static assets:
- Check if your PCF route has compression enabled. You can add configuration to your
manifest.ymlto enforce this, or verify with your PCF admin that the platform's edge router is set up to compress assets like JS, CSS, and JSON.
Second: Fix PCF-Specific Performance Gaps
The 2-second local load vs 30-second PCF load suggests deployment-specific issues. Here's how to address them:
1. Optimize Asset Handling
- Complete image optimization: You're already resizing images—go further by converting them to modern formats like WebP (with fallbacks for older browsers) and using tools like
imageminto compress them without losing quality. Your 3MB assets folder should shrink significantly. - Leverage browser caching: Your
angular.jsonhasoutputHashing: allenabled (great!), which means filenames include unique hashes. Ensure PCF sets longCache-Controlheaders (e.g.,max-age=31536000) for these hashed assets so browsers cache them indefinitely.
2. Check PCF Deployment Configuration
- Verify resource allocation: If your PCF app instance has insufficient CPU or memory, it can slow down asset serving and initial app boot. Use PCF's app monitoring tools to check for resource bottlenecks (e.g., high CPU usage during load).
- Reduce network latency: If your PCF region is far from your user base, consider deploying to a closer region. You can also use Chrome DevTools' Network tab to see exactly where time is being spent (e.g., slow main.js download, blocked API requests, or slow asset loading).
3. Refine Lazy Loading & Preloading
- Double-check lazy route configuration: Ensure your lazy-loaded modules are correctly set up in
AppRoutesModule(no accidental eager loading). For example, routes should useloadChildren: () => import('./path/to/module').then(m => m.ModuleName)syntax. - Implement smart preloading: Use Angular's
PreloadAllModulesstrategy (or a custom preloader) to load non-critical lazy modules in the background after the main app loads. This improves subsequent page loads without blocking the initial render.
4. Trim Unnecessary Polyfills
- Review your
polyfills.tsfile: Remove polyfills for features your target browsers natively support (e.g.,classlist.jsif you don't need to support IE10 and below). - Conditionally load IE polyfills: Your
postbuildscript copies an IE-specific polyfill—serve this only to IE users (via user-agent detection) instead of including it in the initial load for all users.
Final Quick Checks
- Run
ng build --prod --aot --build-optimizer(your script already does this, but confirm no flags are missing). - Clear PCF's app cache after deploying updates to ensure users get the latest optimized assets.
Content of the question comes from Stack Exchange, asked by ZeroTPS

