# Combine JavaScript (combine_javascript)

Combines multiple JS files into one. The combine_javascript filter: what it does, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/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](https://modpagespeed.com/examples/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_javascript` and compare behavior and the console; the `X-Mod-Pagespeed` response header confirms whether the filter ran. [Is it working?](https://modpagespeed.com/docs/is-it-working/) covers the routine.

## Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

**Apache**

```apache
ModPagespeedEnableFilters combine_javascript
```

**nginx**

```nginx
pagespeed EnableFilters combine_javascript;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters combine_javascript
```

It is on by default under CoreFilters; to turn it off:

```text
ModPagespeedDisableFilters combine_javascript   # Apache
pagespeed DisableFilters combine_javascript;   # nginx
pagespeed DisableFilters combine_javascript   # IIS
```

```text
<!-- 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>
```

```apache
ModPagespeedEnableFilters combine_javascript
ModPagespeedMaxCombinedJsBytes 92160
```

```nginx
pagespeed EnableFilters combine_javascript;
pagespeed MaxCombinedJsBytes 92160;
```

Scoping, `ForbidFilters` and the thresholds filters read: [Choosing filters](https://modpagespeed.com/docs/filter-selection/).

## Live example

See the before and after, with the source diff: [combine_javascript example](https://modpagespeed.com/examples/combine_javascript/).

## Frequently asked questions

**What does the combine_javascript filter do?**

`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.

**Is combine_javascript enabled by default?**

Yes. `combine_javascript` is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with `DisableFilters`.

**How do I turn off combine_javascript?**

Add `ModPagespeedDisableFilters combine_javascript` on Apache or `pagespeed DisableFilters combine_javascript;` on nginx. On IIS, add `pagespeed DisableFilters combine_javascript` to pagespeed.config.

## Related filters

- [`rewrite_javascript`](https://modpagespeed.com/docs/filters/rewrite_javascript/): Minify JavaScript
- [`inline_javascript`](https://modpagespeed.com/docs/filters/inline_javascript/): Inline JavaScript
- [`defer_javascript`](https://modpagespeed.com/docs/filters/defer_javascript/): Defer JavaScript
- [`extend_cache_scripts`](https://modpagespeed.com/docs/filters/extend_cache_scripts/): Extend cache for scripts
- [`include_js_source_maps`](https://modpagespeed.com/docs/filters/include_js_source_maps/): Include JS source maps
- [`make_show_ads_async`](https://modpagespeed.com/docs/filters/make_show_ads_async/): Async AdSense
- [`outline_javascript`](https://modpagespeed.com/docs/filters/outline_javascript/): Outline JavaScript
- [`rewrite_javascript_external`](https://modpagespeed.com/docs/filters/rewrite_javascript_external/): Minify external JavaScript

This page is drawn from the [combine_javascript entry on JavaScript filters](https://modpagespeed.com/docs/javascript-filters/#combine_javascript). Every filter in one table: [PageSpeed filters](https://modpagespeed.com/docs/filters/).
