CSS and JS Bundling in 1C-Bitrix: Speed Up Loading

CSS and JS Bundling in 1C-Bitrix: Speed Up Loading We often take over projects that have evolved over years and discover chaos in CSS and JS management. A typical scenario: 60–120 requests for styles and scripts on a catalog page. Even with HTTP/2, each additional request consumes time for DNS, T

Our competencies:

Frequently Asked Questions

CSS and JS Bundling in 1C-Bitrix: Speed Up Loading

We often take over projects that have evolved over years and discover chaos in CSS and JS management. A typical scenario: 60–120 requests for styles and scripts on a catalog page. Even with HTTP/2, each additional request consumes time for DNS, TCP, and TLS. On mobile networks, this kills performance. Our experience shows that combining files can reduce the number of requests by 10–20 times and speed up loading by 40–70%.

Why Bundling CSS and JS Speeds Up the Site?

Browsers limit the number of concurrent connections to a single domain (typically 6). When there are many files, they load sequentially. Bundling into one file reduces the number of requests, which is particularly noticeable on mobile networks. It also lowers the overhead for DNS, TCP, and TLS per request. For more details, see HTTP/2.

Bitrix's Built-In Bundling Mechanism

Bitrix offers two approaches: using the asset management API and the bitrix:main.include component. When minification is enabled (Settings → Performance → Compression), all registered CSS files are collected into /bitrix/cache/css/<hash>.css and JS into /bitrix/cache/js/<hash>.js. However, only files registered before ShowHead() are bundled. Direct <link> tags in templates are ignored.

What Problems Do We Solve?

Order of inclusion. When bundling, the CSS cascade often breaks. For example, reset styles must come first, component styles later. The solution is to use \Bitrix\Main\Page\Asset::addCss() with explicit dependency specifications or split into two bundles: base (grid, typography) and dynamic (widgets).

Inline styles and scripts. Many components (e.g., bitrix:news.list) output render directly into HTML. This is not bundled. For custom solutions, we replace echo '<style>' with $this->addExternalCSS() so that the file goes into the common bundle and is cached.

Duplicates and version conflicts. On one project, we found 12 identical normalize.css files and three versions of jQuery (1.9, 2.1, 3.6) on a single page. Auditing by intercepting AddCSSLink() and logging helped unify libraries. Tree shaking and code splitting are the next steps.

How We Do It: Vite and Webpack

For modern projects, Bitrix's built-in tools are insufficient. We use Vite or Webpack.

Example structure with Vite:

local/templates/mytemplate/ ├── src/ │ ├── css/ │ │ ├── main.scss │ │ └── components/ │ ├── js/ │ │ ├── app.js │ │ └── pages/ ├── dist/ ← Vite builds here │ ├── app.[hash].css │ └── app.[hash].js ├── header.php ← includes dist/ └── vite.config.js 

In header.php, we include the bundles via CMain::AddCSSLink() so they are cached by Bitrix. Vite provides HMR during development and tree shaking for production.

Why Vite Is Better Than the Built-In Mechanism

Vite supports SCSS, TypeScript, and automatic code splitting. Bitrix's built-in bundling does not. In practice, we achieve a bundle size reduction of 30–50% by removing dead code. Comparison:

Characteristic Built-in Mechanism Vite/Webpack
SCSS support No Yes
Code splitting No Yes
Tree shaking No Yes
HMR No Yes
Configuration Simple Requires config

Case Study: Portal with Three Developer Teams

One of our clients is a B2B portal, 5 years in development, with three teams that have come and gone. On the catalog page: 47 CSS requests (890 KB uncompressed), 68 JS requests (1.4 MB). Our audit revealed: 12 CSS duplicates, 8 JavaScript libraries in multiple versions.

What we did:

  • Audited all inclusions by intercepting CMain::AddCSSLink() and AddHeadScript() with logging.
  • Unified libraries: single jQuery version, removed duplicates.
  • Migrated all direct <link> tags to the Bitrix API.
  • Set up Vite for new code, packaged legacy code into a single bundle.
  • Code splitting: critical bundle + 4 page-specific bundles.

Result:

  • 47 CSS requests → 3.
  • 68 JS requests → 5.
  • Total CSS+JS weight (after gzip) dropped from 850 KB to 210 KB due to duplicate removal and tree shaking.
  • Page load speed improved from 4.2 s to 1.1 s (Lighthouse).

Process of Work:

  1. Analytics — collect logs of all inclusions, identify duplicates, conflicts, dead code.
  2. Design — define bundle architecture: critical, page-specific, deferred.
  3. Implementation — migrate inclusions to API, set up builder (Vite/Webpack), write configurations.
  4. Testing — verify every page for correct display and JS functionality.
  5. Deployment — upload to production, check caching.

Bitrix's tagged caching invalidates cache when data changes. When bundling files, it is important that CSS/JS cache invalidation works correctly. We configure this via $arParams["CACHE_TAGS"].

What Is Included in the Work:

  • Full audit of current inclusions with a report.
  • Library unification and duplicate removal.
  • Setup of the builder (Vite or Webpack) for your template.
  • Integration with Bitrix tagged caching.
  • Documentation on the build process and inclusion.
  • Team training on the new architecture.
  • One week of post-release support.

Timelines

Project Type Scope of Work Timeline
Simple site (1 template, <30 components) Migrate inclusions to API, enable minification 1–2 days
Medium project (custom frontend, multiple templates) Audit + library unification + build setup 3–7 days
Large portal (multiple teams, legacy code) Full audit, refactor inclusions, set up Webpack/Vite 7–20 days

Cost is determined individually after the audit. Contact us for a consultation — we will propose the optimal solution. We guarantee at least a 2x loading speed improvement, confirmed by tests.

Load testing after bundling is mandatory — in rare cases, the loading sequence of scripts matters, and breaking it can cause JS errors on specific pages. Our experience allows us to anticipate such nuances and avoid them at the design stage. Get a consultation and optimization plan.