Inline JavaScript (inline_javascript)
- Category:
- JavaScript
- CoreFilters:
- Yes
- OptimizeForBandwidth:
- No
- Risk:
- Generally safe
inline_javascript replaces a small external script with an inline <script> block that holds the file’s contents, so the browser skips a request. The element keeps its place in the page, so execution order does not change. Live demo: inline_javascript.
How it works
When it helps and when it does not
Inlining pays when a script is tiny: for a few hundred bytes, the request with its headers and round trip costs more than the bytes it fetches. The trade runs the other way as files grow or get shared. An inlined script is not cached on its own, so it downloads again with every page view, and a file inlined into ten pages is transferred ten times. Scripts that several pages share, and anything well above the default threshold, are better served external with a long cache lifetime. inline_javascript is a CoreFilter, so this trade is already live on a default install; the threshold is the knob.
How it decides
Only external scripts whose contents are no larger than JsInlineMaxBytes (default 2048 bytes) qualify, and only files on domains the module is authorized to fetch. A script with async or defer, or with the IE-specific for and event attributes, is left external: those attributes change when the script runs, and an inline block cannot express that timing. Module scripts (type="module") are never inlined, since inlining would change how their relative imports resolve. Nothing is inlined when the page’s Content-Security-Policy forbids inline scripts. Everything else about the element, including its position in the document, stays as authored.
When to use it
- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Risk rating on these docs: Generally safe.
Risks
-
The page grows by the script’s size on every view, so keep
JsInlineMaxBytessmall. Raising it to inline a large file usually costs more than the saved request returns. -
Verify with the
X-Mod-Pagespeedresponse header and a?PageSpeedFilters=-inline_javascriptcomparison; Is it working? has the steps.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters inline_javascriptnginx
pagespeed EnableFilters inline_javascript;IIS (pagespeed.config)
pagespeed EnableFilters inline_javascriptIt is on by default under CoreFilters; to turn it off:
ModPagespeedDisableFilters inline_javascript # Apache
pagespeed DisableFilters inline_javascript; # nginx
pagespeed DisableFilters inline_javascript # IIS<!-- before: one extra request for a 1.4 KB file -->
<script src="/js/newsletter-popup.js"></script>
<!-- after: the file's contents sit inside the page -->
<script>document.addEventListener("DOMContentLoaded",function(){var d=document.getElementById("newsletter");d&&setTimeout(function(){d.hidden=!1},4e3)});</script>ModPagespeedEnableFilters inline_javascript
ModPagespeedJsInlineMaxBytes 2048pagespeed EnableFilters inline_javascript;
pagespeed JsInlineMaxBytes 2048;
Scoping, ForbidFilters and the thresholds filters read:
Choosing filters
.
Live example
Inlines small external scripts to remove a request.
Measured on the demo page, original against optimized:
-
HTTP requests
2 → 1 −1
Frequently asked questions
- What does the inline_javascript filter do?
inline_javascriptreplaces a small external script with an inline<script>block that holds the file's contents, so the browser skips a request. The element keeps its place in the page, so execution order does not change. Live demo: inline_javascript.- Is inline_javascript enabled by default?
- Yes.
inline_javascriptis in CoreFilters, the default RewriteLevel, so it runs unless you turn it off withDisableFilters. - How do I turn off inline_javascript?
- Add
ModPagespeedDisableFilters inline_javascripton Apache orpagespeed DisableFilters inline_javascript;on nginx. On IIS, addpagespeed DisableFilters inline_javascriptto pagespeed.config.
Related filters
- rewrite_javascript — Minify JavaScript
- combine_javascript — Combine JavaScript
- defer_javascript — Defer JavaScript
- inline_css — Inline CSS
- 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
This page is drawn from the inline_javascript entry on JavaScript filters. Every filter in one table: PageSpeed filters.