exclude only specific JS - inlined or referenced -- from action by defer_javascript filter?

705 views
Skip to first unread message

pgndev

unread,
Jul 12, 2012, 6:02:15 PM7/12/12
to mod-pagesp...@googlegroups.com
i'm working with MPS on a Drupal site.

i'm evaluating the function/performance of labjs script loading/deferral, in comparison to MPS's defer_javascript filter.

it appears that enabling defer_javascript defers *all* JS on the page.

i need to exclude a couple of specific, inline & external-referenced JS from mgmt/deferral by the filter.

Drupal's implementation of labjs, in conjunction with its page preprocessing capabilities provides nicely for this.

what's the exclusion mechanism in mod_pagespeed?

Shawn Ligocki

unread,
Jul 12, 2012, 6:06:53 PM7/12/12
to mod-pagesp...@googlegroups.com

pgndev

unread,
Jul 12, 2012, 6:09:51 PM7/12/12
to mod-pagesp...@googlegroups.com
On Thu, Jul 12, 2012 at 3:06 PM, Shawn Ligocki <slig...@google.com> wrote:
> ModPagespeedDisallow. See
> https://developers.google.com/speed/docs/mod_pagespeed/restricting_urls

I'm failing to glean how to apply any of that to *inline* scripts that
I've specifically called out, above ...

pgndev

unread,
Jul 12, 2012, 6:17:30 PM7/12/12
to mod-pagesp...@googlegroups.com
It appears that 'pagespeed_no_defer', undocumented as far I can tell
-- certainly not mentioned at the aforementioned dev.g.c page -- but
referenced here:

https://code.google.com/p/modpagespeed/source/browse/trunk/src/install/mod_pagespeed_test/js_defer_order_before_after_hooks.html?r=1536

is relevant. Changing

- <script type="text/javascript">TypekitConfig={kitId: ... </script>
+ <script type="text/javascript"
pagespeed_no_defer="true">TypekitConfig={kitId: ... </script>

seems, initially, to have the desired effect.

As it's not documented (is it?), can we rely on it as a stable API function?

Joshua Marantz

unread,
Jul 12, 2012, 6:26:32 PM7/12/12
to mod-pagesp...@googlegroups.com
pagespeed_no_defer is correct.  We wrote doc for that but it's not pushed yet, but the attribute does work in 0.10.22.4 and will continue to work going forward.

ModPagespeedDisallow* actually does *not* apply to defer_javascript.

-Josh

pgndev

unread,
Jul 12, 2012, 6:36:01 PM7/12/12
to mod-pagesp...@googlegroups.com
On Thu, Jul 12, 2012 at 3:26 PM, Joshua Marantz <jmar...@google.com> wrote:
> pagespeed_no_defer is correct. We wrote doc for that but it's not pushed
> yet, but the attribute does work in 0.10.22.4 and will continue to work
> going forward.

ok, thanks. that works for inline script.

> ModPagespeedDisallow* actually does *not* apply to defer_javascript.

hm. then what, if anything, does? specifically, how to exclude
referenced *.js from deferral?

Joshua Marantz

unread,
Jul 12, 2012, 6:58:33 PM7/12/12
to mod-pagesp...@googlegroups.com
pagespeed_no_defer will work for both inline & external scripts.

-Josh

pgndev

unread,
Jul 12, 2012, 7:11:35 PM7/12/12
to mod-pagesp...@googlegroups.com
On Thu, Jul 12, 2012 at 3:58 PM, Joshua Marantz <jmar...@google.com> wrote:
> pagespeed_no_defer will work for both inline & external scripts.

Fine, for references that I can hardcode the addition of the attribute
to the script tag.

For app-added javascript references, as is typical with a webapp such
as Drupal, I can't get at that ...

I'd need some way, ideally of the aformentioned form,
@restricting_urls, to idenfity the tag to modified -- at MPS render
time

Matt Atterbury

unread,
Jul 13, 2012, 7:50:43 AM7/13/12
to mod-pagesp...@googlegroups.com
For app-added javascript references, as is typical with a webapp such
as Drupal, I can't get at that ...

I'd need some way, ideally of the aformentioned form,
@restricting_urls, to idenfity the tag to modified -- at MPS render
time

Can you control anything in the generated JS? If you can set the type or lang attributes of the script element we might be able to trick defer_javascript into skipping it.

m.

pgndev

unread,
Jul 13, 2012, 10:05:12 AM7/13/12
to mod-pagesp...@googlegroups.com
> Can you control anything in the generated JS? If you can set the type or
> lang attributes of the script element we might be able to trick
> defer_javascript into skipping it.

technically, yes. practically, no.

JS that is provided by a module/theme can certainly have its source
changed, but that'd need to be done for every instance, and likely
would not survive upgrades.

One might be able to add some additional JS to modify JS on page load,
given a list of to-be-modified files, but that's getting rather messy.

Of course, anything that I write can easily be changed ...

pgndev

unread,
Jul 13, 2012, 12:31:32 PM7/13/12
to mod-pagesp...@googlegroups.com
On Fri, Jul 13, 2012 at 4:50 AM, Matt Atterbury <matte...@google.com> wrote:
> Can you control anything in the generated JS? If you can set the type or
> lang attributes of the script element we might be able to trick
> defer_javascript into skipping it.

Well ... maybe. Sort of.

Drupal's css/js load can be controlled on a per-file basis:

http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_js/7
http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_add_css/7

An individual *js can be config'd to load, e.g., as

drupal_add_js(
$my_theme_dir . '/js/my.js',
array(
'type' => 'file',
'scope' => 'header',
'group' => JS_LIBRARY,
'every_page' => FALSE,
'weight' => -198,
'defer' => TRUE,
'cache' => TRUE,
'preprocess' => TRUE,
)
);

Iiuc, those flags are exposed only to the internal prepreocessing
system -- and not to any external functionality, ala MPS.

But ...

those parameters might be 'hook-able' -- code written to export as a
ENV, _SESSION or global var, or somesuch. I've never tried it myself,
and still think crude, app-specific workarounds are not the best idea.
Reply all
Reply to author
Forward
0 new messages