# Minify external JavaScript (rewrite_javascript_external)

Minifies external JavaScript files. The rewrite_javascript_external filter: what it does, its risks and the Apache, nginx and IIS directives.

Canonical URL: https://modpagespeed.com/docs/filters/rewrite_javascript_external/

- Category: JavaScript
- CoreFilters: Yes
- OptimizeForBandwidth: Yes
- Risk: Generally safe

`rewrite_javascript_external` handles the external half. Each `<script src>` file on a domain the module is authorized to fetch is minified and served from a rewritten `.pagespeed.jm.` URL with a long cache lifetime, so repeat visitors download the minified file once and keep it; the original file on disk is never modified. In OptimizeForBandwidth mode the minified bytes replace the original response in place and the URL stays as authored.

## How it works

The three filters on this section share one minifier. It removes comments and collapses whitespace; identifiers, string literals, regular expressions and property names are emitted unchanged. It keeps a line break wherever automatic semicolon insertion could otherwise merge two statements into one. [How safe JavaScript minification handles automatic semicolon insertion](https://modpagespeed.com/blog/safe-javascript-minification-semicolon-insertion/) has the details.

Minification saves the most on hand-formatted source; a file a bundler already minified gains nothing and only costs rewrite time. `rewrite_javascript_external` runs as part of the compound and can also be enabled on its own.

Enabled on its own, `rewrite_javascript_external` leaves inline `<script>` blocks exactly as authored; minifying those is the inline sub-filter's job. Files on domains the configuration does not authorize keep their original URLs as well, so a page that mixes own and third-party scripts sees only the own files rewritten.

## When to use it

- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Part of OptimizeForBandwidth, the level that optimizes resources without changing their URLs.
- Also switched on by [`rewrite_javascript`](https://modpagespeed.com/docs/filters/rewrite_javascript/).
- Risk rating on these docs: **Generally safe**.

## Risks

- The minifier never renames identifiers; the historical failure mode is a script that inspects its own source text, for example through `Function.prototype.toString()`, and reacts to the changed formatting.

- Verify with the `X-Mod-Pagespeed` response header and a `?PageSpeedFilters=-rewrite_javascript` comparison; [Is it working?](https://modpagespeed.com/docs/is-it-working/) has the steps.

## Configuration

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

**Apache**

```apache
ModPagespeedEnableFilters rewrite_javascript_external
```

**nginx**

```nginx
pagespeed EnableFilters rewrite_javascript_external;
```

**IIS (pagespeed.config)**

```text
pagespeed EnableFilters rewrite_javascript_external
```

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

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

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

## Frequently asked questions

**What does the rewrite_javascript_external filter do?**

`rewrite_javascript_external` handles the external half. Each `<script src>` file on a domain the module is authorized to fetch is minified and served from a rewritten `.pagespeed.jm.` URL with a long cache lifetime, so repeat visitors download the minified file once and keep it; the original file on disk is never modified. In OptimizeForBandwidth mode the minified bytes replace the original response in place and the URL stays as authored.

**Is rewrite_javascript_external enabled by default?**

Yes. `rewrite_javascript_external` is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with `DisableFilters`. It is also part of the OptimizeForBandwidth level.

**How do I turn off rewrite_javascript_external?**

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

## Related filters

- [`rewrite_javascript`](https://modpagespeed.com/docs/filters/rewrite_javascript/): Minify JavaScript
- [`rewrite_javascript_inline`](https://modpagespeed.com/docs/filters/rewrite_javascript_inline/): Minify inline JavaScript
- [`combine_javascript`](https://modpagespeed.com/docs/filters/combine_javascript/): Combine 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
- [`inline_javascript`](https://modpagespeed.com/docs/filters/inline_javascript/): Inline JavaScript
- [`make_show_ads_async`](https://modpagespeed.com/docs/filters/make_show_ads_async/): Async AdSense

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