If a module attaches JS files in hook_page_alter() the files are not aggregated.
How to repro?
1. Download latest Google Analytics 7.x
2. Enable JS aggregation
3. JS file sites/all/modules/google_analytics/googleanalytics.js is not aggregated.
Tested with drupal-7.0-alpha7.
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | js-hook-page-build-929096-27.patch | 3.02 KB | jhedstrom |
Comments
Comment #1
jacinesubscribe.
Comment #2
jacineHm, I took a look at Google Analytics and it doesn't seem like proper usage of
hook_page_alter(). From what I understand, the point of implementinghook_page_alter()is to actually alter the$pagearray. In doing so, if you wanted to add CSS or JS files, you'd use the#attachedproperty (which definitely works with aggregation on).It doesn't appear that Google Analytics is using anything in the
$pagearray, and I could be wrong, buthook_js_alter()seems like a better candidate.Comment #3
jacineOh, and yeah, I can confirm that adding CSS and JS files via
drupal_add_css()anddrupal_add_js()in ahook_page_alter()implementation works great until you turn aggregation on, which is definitely a WTF at the very least.Comment #4
hass commentedIt's written in the upgrade docs that hook_footer() has become hook_page_alter(). GA need to attach the code in this late stage... I see no way to change this. So - aggregation need to be fixed on the end of the day or many modules JS/CSS files are not aggregated.
Comment #5
jacineWell,
hook_footer()and$closureare now supposed to be in the$page['page_bottom']region, which is alterable inhook_page_alter(). You can use that, but you'd still need to use#attachedin the$pagearray, as opposed todrupal_add_js()i.e.It prints right before the closing
<body>tag inhtml.tpl.php, and the aggregation works.Does that solve your problem?
Comment #6
sunAlways start with a test.
Comment #7
jacineCSS files are also affected in the same way.
Comment #8
hass commentedIn past D7 days the drupal_add_js() has not worked in page alter... But in #946550: Javascript should be attached to page_bottom, not footer (otherwise it won't ever be included by some themes) i was told by JacobSign and David Rothenstein to go back to drupal_add_js and jacob said WTF to the #attached code snippets I've used since Gabor pointed this out as the way how to attach JS in page alter... So this is not a WTF question only here and if something may work for me or not. I'd do not like to role back again and role forward later again. I'd like to get this fixed or a clear doc how it works. It looks like I'm not the only confused here... So I'd like to *know* and not to guess any longer...
Comment #9
sunwell, in order to continue here, we need to figure out why this patch passed (and therefore works) and your code does not.
Comment #10
hass commentedI can try again with latest dev. Changing title.
Comment #11
hass commentedI'm currently not able to repro this issue again with beta3. Maybe someone fixed this bug since alpha7. Before closing this case it should be repro'd again with alpha7 - only to verify. Additional it cannot be wrong to get the new test in for future.
Comment #12
David_Rothstein commentedI looked into this and I think there are no bugs here, just API changes:
So no bug here, just that alpha7 did not aggregate any JS or CSS files except when specifically requested by the caller, and then later that behavior was reverted.
Comment #13
David_Rothstein commentedThe patch looks pretty reasonable, though - I guess it doesn't hurt to test :)
I think it is a little fragile to rely on the specific content of filter.admin.js like that, though? Maybe it would be better to just have a specific test JS file that is used for the test.
Comment #14
hass commentedLet's go with the test only. As david said, this was an alpha7 bug.
Comment #15
nod_What's the status in 8.x for this?
Sounds like it was not actually a bug hence might not be worth porting. Is that a wrong assumption?
Comment #16
wim leershook_page_alter()is going away in D8 andhook_page_build()is the recommended way to attach CSS/JS that is "global" to the page. CSS/JS aggregation for CSS/JS added inhook_page_build()definitely works.Therefor, we must only look at Drupal 7. As per #14 and earlier, this was an alpha7 bug. Which means the only thing left to be done in this issue is adding test coverage for D7, which I think is rather unlikely to happen. So: closing.
Comment #17
David_Rothstein commentedWhy wouldn't the tests be written for Drupal 8 also? Especially since it's now the recommended way to add global CSS/JS to the page, but there's no test for this at all that I can see.
Agreed it's more important to test hook_page_build() than hook_page_alter(), though.
Comment #18
wim leersEditLoadingTest.php+edit.module/edit_page_build()test this. It's likely it's also tested in other places.I don't see why we need test coverage for this, it's guaranteed to work as long as the theme system/render layer does not mess things up, and *they* should have test coverage to guarantee that. It's rather silly to explicitly test that adding CSS/JS works within this hook; then we should also provide test coverage for any other hook where they may be added.
Or am I missing something here? I'm just getting wary of the boatloads of pointless tests in Drupal 8…
Comment #19
David_Rothstein commentedIt's not something I'd spend my time on writing a test from scratch, but given that the code is already written and it takes very little effort to update it, I think it's worth doing.
It's probably less about testing aggregation specifically, than testing that the whole system of using #attached to add JavaScript to the page array actually gets the JavaScript onto the page. I can imagine that breaking, if JavaScript is ever accidentally compiled and rendered in the page's header earlier in the page request than drupal_render() runs on the $page itself.
Here's a reroll for D8 which also adds a specific test JavaScript file (rather than using Filter module's).
Comment #21
hass commentedWhy is inline, external, etc. not tested?
Comment #22
David_Rothstein commented#19: js-hook-page-build-929096-19.patch queued for re-testing.
Comment #23
David_Rothstein commentedThe test failure looks unrelated, so I triggered a retest.
@hass, there are JavaScript tests for inline, external, etc., already (although not "full-stack" tests like this one, testing the use of #attached in hook_page_build()). It could certainly be added, but I figured having one test that starts with the basics is good to begin with.
Comment #24
hass commentedI need inline in hook_page_alter. If this hook no longer exists I need to know the migration path really works or Google Analytics may be broken. Please make sure this is all fully tested and examples exist.
Comment #25
David_Rothstein commentedhook_page_alter() definitely still exists in Drupal 8.
I'm not sure offhand which issue it is that's proposing its removal, but whichever one it is, that's probably the issue where you should leave the above comment about Google Analytics.
Comment #26
hass commentedSee comment #16...
Comment #27
jhedstromReroll of #19.
Comment #28
jhedstromRemoving tag.
Comment #30
hass commented#2362987: Remove hook_page_build() and hook_page_alter()
Comment #31
hass commentedComment #42
nod_I'm not sure this issue is still relevant. Feel free to reopen if that is the case.