Problem/Motivation

Expected: drupal_add_js('something.js', array('scope' => 'footer')) (or the library definition equivalent) is listed in drupalSettings.ajaxPageState.js.
Result: only the JS files that are in the 'header' scope are listed in drupalSettings.ajaxPageState.js.

Proposed resolution

diff --git a/core/includes/common.inc b/core/includes/common.inc
index 10b09d6..70c0b81 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -3831,7 +3831,7 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS
 
       // Provide the page with information about the individual JavaScript files
       // used, information not otherwise available when aggregation is enabled.
-      $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
+      $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($javascript), 1);
       unset($setting['ajaxPageState']['js']['settings']);
 
       // Provide the page with information about the individual CSS files used,

This is analogous to how the CSS page state is stored. $items only contains the items for the current scope; and since settings on most pages are added when $scope == 'header', that explains why only JS files that are in the 'header' scope get added to the page state.

Remaining tasks

Get proper test coverage. In my initial testing it turned out to be impossible to reproduce the same problem in the tests, likely because WebTestBase does not actually execute JavaScript, it merely simulates the crucial aspects.

User interface changes

None.

API changes

None.

CommentFileSizeAuthor
#1 1864944-1.patch732 bytesWim Leers
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers’s picture

Status: Active » Needs review
Issue tags: +JavaScript
FileSize
732 bytes
Wim Leers’s picture

Now that #1824500: In-place editing for Fields has landed, this is super easy to reproduce.

  1. Install D8 using the Standard install profile (which has Edit enabled).
  2. Go to the front page.
  3. Open your browser's console and type drupalSettings.ajaxPageState.js.
  4. Check whether edit.js is listed there (which has 'scope' => 'footer') . It's not. This is the bug.
nod_’s picture

Status: Needs review » Reviewed & tested by the community

Works well as a stop-gap measure. It's the whole API that needs to be worked on to avoid this kind of uglyness, see #1871596: Create a PartialResponse class to encapsulate html-level data [Moving to sandbox].

In the meanwhile, this works.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Hm. Unfortunately, doesn't sound like it's possible to work #2 into a test since it's JS-only. :(

Committed and pushed to 8.x. Thanks!

Wim Leers’s picture

It is in fact possible to add tests for this, similarly to how I'm doing tests over at #1875632: JS settings merging behavior: preserve integer keys (allow for array literals in drupalSettings): using json_decode().

However, all of this will change in the issue that nod_ pointed to in #3 so it's not clear if it's worth the effort. I personally believe it's worth it, even if only because that'll ensure tests in #1871596: Create a PartialResponse class to encapsulate html-level data [Moving to sandbox] will test this edge case too. Thoughts?

webchick’s picture

Title: ajaxPageState does not contain JS files that are in scopes other than 'header' » Tests for ajaxPageState does not contain JS files that are in scopes other than 'header'
Category: bug » task
Status: Fixed » Active
Issue tags: +Needs tests

Yeah, let's do it. That issue can always remove the test if it turns out to not be relevant.

Switching some metadata around.

Wim Leers’s picture

I'll likely get this done tomorrow.

lmeurs’s picture

The patch from #1 to set scripts paths to Drupal.settings.ajaxPageState.js scope independently has not been backported to Drupal 7, is this something that should be done? (sorry, am not very familiar with the backporting policy)

And sort of on the same subject (though D7): when the scope of the settings javascript has been altered to footer, on AJAX calls newly attached files are not being loaded. Explanation: When the scope of the settings variable is no longer header, drupal_get_js() does not add the Drupal.settings.ajaxPageState.js array, Drupal.settings.ajaxPageState.js is not defined on page load, ajax_render() does not receive $_POST['ajax_page_state']['js'] on AJAX calls, ajax_render() cannot determine which files are newly attached and thus none are appended to the head.

Is there a good reason for only adding Drupal.settings.ajaxPageState.js when the scope of the settings variable is set to header or should we create a new issue for this?

BTW: Workarounds are setting these variables for JS and CSS manually on hook_js_alter() or better to not change the scope of scripts but print the $scripts variable at the bottom of your page using this great little trick.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • webchick committed 7f66670 on 8.3.x
    Issue #1864944 by Wim Leers: Fixed ajaxPageState does not contain JS...

  • webchick committed 7f66670 on 8.3.x
    Issue #1864944 by Wim Leers: Fixed ajaxPageState does not contain JS...

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • webchick committed 7f66670 on 8.4.x
    Issue #1864944 by Wim Leers: Fixed ajaxPageState does not contain JS...

  • webchick committed 7f66670 on 8.4.x
    Issue #1864944 by Wim Leers: Fixed ajaxPageState does not contain JS...

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Title: Tests for ajaxPageState does not contain JS files that are in scopes other than 'header' » ajaxPageState does not contain JS files that are in scopes other than 'header'
Version: 9.3.x-dev » 8.0.x-dev
Assigned: Wim Leers » Unassigned
Category: Task » Bug report
Status: Active » Fixed
Issue tags: -JavaScript, -Needs tests +JavaScript, +Bug Smash Initiative

This was committed in Drupal 8.0 and reopened to add a test. Between then and now the function changed in this patch, drupal_get_js, was removed in #2368797: Optimize ajaxPageState to keep Drupal 8 sites fast on high-latency networks, prevent CSS/JS aggregation from taking down sites and use HTTP GET for AJAX requests. The replacement is AssetResolver with has its own tests so there no more work needed to be done here.

Therefor, I am restoring the issue meta data to the version when this was committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.