So I've had this problem for a while now and thought it was related to the dialog module. But I just recently found out that turning off CDN makes it go away...

If I ajax-load anything that has an attached js or library, it usually doesn't work. So I usually have to load whatever required javascript on the original page for it to work. I'll provide an example...

Steps to reproduce:
- disable cdn
- ajax-load a node form that needs misc/tabledrag.js (for example, a node form with a field collection in it - it might fail with a multiple-value text field too). Note that the tabledrag works.
- enable cdn and repeat the previous step. Note that the tabledrag doesn't work.
- manually attach 'misc/tabledrag.js' on the original page (before the ajax load) and try the ajax-load again. Note that the tabledrag again works.

My setup:
CDN (latest dev) using origin pull and farfuture expiration
AdvAgg
bootstrap theme pulling bootstrap files from bootstrap's CDN
jquery update (2.1)

With all the aggregation going on, I don't know how I can debug this. Has anyone else seen this?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

captainack created an issue. See original summary.

Wim Leers’s picture

Category: Bug report » Support request

This is why the CDN module blacklists JS files by default. The reason: the same-origin policy.

captainack’s picture

FileSize
86.88 KB

Hi Wim.

Thanks for taking the time to reply so quickly!

I don't understand why this is a same-origin problem though. I don't get any 403's, and I thought that the CDN module already has this solved with the headers sent to (in my case) amazon.

Also I don't have any problems with normal page loads getting whatever they need from cloudfront. Also, post-ajax-request, I do have succesful hits to cloudfront (image attached showing this).

Is there something different about an ajax request that I'm not thinking about?

Thanks!

captainack’s picture

Title: ajax-loading and attached javascript / libraries » ajax-loading and attached libraries

Okay so I looked through the js and css files that DID get downloaded and tried to compare against the render array to try to find a pattern...

I'm not 100% positive, but it looks like there's a pattern!
- The files that ARE NOT working are attached as a "library"
- The files that ARE working are attached either as "css" or as "js".

I attached any libraries that are needed by the ajax call to the original page, and it seems to work around it.

In light of this new information, I'm tempted to switch this back to bug-report, since I don't see anything in the docs relating to library attachments vs css/js. But I don't want to be rude ;-).

captainack’s picture

Although I've already worked around this, this has been a thorn for me, so I'm still digging...

It looks like there's another pattern:

Files that DO NOT work are also all either augmented or replaced via the bootstrap theme's _js_alter hook.

This seems to be a more likely suspect, since looking through the code seems like the library thing shouldn't make a damn bit of difference.

captainack’s picture

Sigh. Couldn't find the cause yet.

Okay I gotta give up on this for now. Maybe I'll be able to probe further later. For now I'm just working around it.

Wim Leers’s picture

It sounds like this is indeed not related to same-origin policy. I just saw all the trigger words in the issue summary :)

This then very much sounds like a Bootstrap-theme-induced problem. Can you disable that theme for a moment (or at least its alter hook) and see what difference that makes?

In light of this new information, I'm tempted to switch this back to bug-report, since I don't see anything in the docs relating to library attachments vs css/js. But I don't want to be rude ;-).

So far, everything indicates this is a problem in Bootstrap. The CDN module doesn't do anything special to libraries. As you can see, I also still reply to support requests. As long as something isn't a confirmed bug in the CDN module, I keep it as a support request, to make it clearer how many actual bugs there are.

captainack’s picture

Can you disable that theme for a moment (or at least its alter hook) and see what difference that makes?

Lol! I wish I'd thought of that before spending an hour or two digging through code! I'll try that as soon as I'm in the context of chasing this down again. I actually had no idea that the bootstrap theme swaps out and adds to some of the core javascript files. At least I learned that in the process.

And yes, I agree - this looks like something non-CDN-friendly that the bootstrap theme is doing. And thank you again for taking the time to help!

Wim Leers’s picture

You're welcome :)

Wim Leers’s picture

Do you have news?

captainack’s picture

Sorry Wim. None yet. I got sucked into a hundred other things on the project. I forgot about this because I had the workaround of pre-loading the javascript :(.

But thanks for the reminder. I'll try to look into it again as soon as I can.

Wim Leers’s picture

Status: Active » Postponed (maintainer needs more info)

No problem :)

captainack’s picture

Status: Postponed (maintainer needs more info) » Active

Sorry it took so long.

I tried the following:
- Switched the theme back to Bartik
- Switched jquery update to the drupal default
- Disabled advanced aggregation

...and I still see the behavior:

unless I add the following to the render array of the original page:

$retval[9]['#attached']['js'][] = 'misc/tabledrag.js';
$retval[9]['#attached']['library'][] = array('system', 'drupal.collapse');

...ajax-loaded tabledrags (e.g. field collections) don't show up properly (they show raw weights instead of the tabledrag).

When disabling CDN, it's not necessary to brute-force load those on the original page.

It looks like I read us up the wrong tree with the bootstrap stuff :(. But at least we eliminated those things.

When it's failing, I look at the javascripts being downloaded upon the ajax call, and I do see the tabledrag stuff being downloaded. So it's weird that it's not working.

captainack’s picture

FileSize
47.34 KB
47.65 KB

So I took everything back to my production state and pasted a couple screenshots of the network tab (this time Edge's). The files fetched are exactly the same size in the CDN and no-CDN cases. The only difference there is the "initiator" column - in one case it says XMLHttpRequest and in the other it says "script". Not sure why.

Both cases have no errors on the console when you hit the ajax call. This is absolutely bizarre.

Wim Leers’s picture

Status: Active » Postponed (maintainer needs more info)

#14: Looks like you've found a browser bug :) Perhaps the label ("script" or "XMLHttpRequest") depends in part whether the requested asset has the same domain name as the website in question.

#13: Can you still reproduce that? I've got the feeling this was a browser bug, and that the browser updates in the past 2 months fixed it.

Wim Leers’s picture

Version: 7.x-2.x-dev » 8.x-3.x-dev
Component: Origin Pull mode » Code
Category: Support request » Bug report
Status: Postponed (maintainer needs more info) » Closed (duplicate)

Sorry for not thinking of this sooner. But the same problem was reported years ago: #1988968: Drupal.ajax does not guarantee that "add new JS file to page" commands have finished before calling said JS. The root cause is Drupal's AJAX system.

Wim Leers’s picture

Priority: Normal » Critical