Combine JavaScript (combine_javascript)
- Category:
- JavaScript
- CoreFilters:
- Yes
- OptimizeForBandwidth:
- No
- Risk:
- Generally safe
combine_javascript concatenates consecutive external scripts into one file, loads the combined file from a .pagespeed.jc. URL, and replaces each original tag with a small inline script, eval(…), that runs that member in order. A page that loads five scripts back to back makes one request instead of five. The scripts run in their original order, so dependencies between them keep working.
How it works
Live demo: combine_javascript.
When it helps and when it does not
Combining was designed for HTTP/1.1, where a browser opens only a few connections per host and extra requests queue. Over HTTP/2 and HTTP/3 requests multiplex over one connection, so the saving shrinks to per-request overhead. The costs cut the other way too: the combined file is refetched in full when any member changes, and the browser cannot run the first script until the whole combined file has arrived. On a legacy page loading a dozen small files, combining still pays. On a page with two or three scripts over HTTP/2, measure with the filter on and off before keeping it.
How it decides
Only consecutive, synchronously executing external scripts join a group. An inline <script>, a script with async or defer, a type="module" script, a script of an unknown type, and a script carrying integrity= each end the current group, as does other markup between two script tags. Modules are excluded because the combination evaluates member scripts inside one shared file, which cannot represent a module’s isolated scope and deferred execution. A group stops growing when the combined uncompressed contents would pass MaxCombinedJsBytes (default 92160), and every member must come from a domain the module is authorized to fetch. Pages whose Content-Security-Policy forbids eval or inline scripts are not combined, because the browser would block the inline eval tags.
When to use it
- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Risk rating on these docs: Generally safe.
Risks
- If a page misbehaves after combining, load it with
?PageSpeedFilters=-combine_javascriptand compare behavior and the console; theX-Mod-Pagespeedresponse header confirms whether the filter ran. Is it working? covers the routine.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters combine_javascriptnginx
pagespeed EnableFilters combine_javascript;IIS (pagespeed.config)
pagespeed EnableFilters combine_javascriptIt is on by default under CoreFilters; to turn it off:
ModPagespeedDisableFilters combine_javascript # Apache
pagespeed DisableFilters combine_javascript; # nginx
pagespeed DisableFilters combine_javascript # IIS<!-- before -->
<script src="/js/jquery.js"></script>
<script src="/js/carousel.js"></script>
<script src="/js/forms.js"></script>
<!-- after -->
<script src="/js/jquery.js+carousel.js+forms.js.pagespeed.jc.HASH.js"></script><script>eval(mod_pagespeed_HASH1);</script>
<script>eval(mod_pagespeed_HASH2);</script>
<script>eval(mod_pagespeed_HASH3);</script>ModPagespeedEnableFilters combine_javascript
ModPagespeedMaxCombinedJsBytes 92160pagespeed EnableFilters combine_javascript;
pagespeed MaxCombinedJsBytes 92160;
Scoping, ForbidFilters and the thresholds filters read:
Choosing filters
.
Live example
Combines multiple script files into one to cut HTTP requests.
Measured on the demo page, original against optimized:
-
HTTP requests
3 → 2 −1
Frequently asked questions
- What does the combine_javascript filter do?
combine_javascriptconcatenates consecutive external scripts into one file, loads the combined file from a.pagespeed.jc.URL, and replaces each original tag with a small inline script,eval(…), that runs that member in order. A page that loads five scripts back to back makes one request instead of five. The scripts run in their original order, so dependencies between them keep working.- Is combine_javascript enabled by default?
- Yes.
combine_javascriptis in CoreFilters, the default RewriteLevel, so it runs unless you turn it off withDisableFilters. - How do I turn off combine_javascript?
- Add
ModPagespeedDisableFilters combine_javascripton Apache orpagespeed DisableFilters combine_javascript;on nginx. On IIS, addpagespeed DisableFilters combine_javascriptto pagespeed.config.
Related filters
- rewrite_javascript — Minify JavaScript
- inline_javascript — Inline JavaScript
- defer_javascript — Defer JavaScript
- extend_cache_scripts — Extend cache for scripts
- include_js_source_maps — Include JS source maps
- make_show_ads_async — Async AdSense
- outline_javascript — Outline JavaScript
- rewrite_javascript_external — Minify external JavaScript
This page is drawn from the combine_javascript entry on JavaScript filters. Every filter in one table: PageSpeed filters.