Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

288: Removes async, adds data attribute #7

Merged
merged 3 commits into from
Nov 27, 2018
Merged

288: Removes async, adds data attribute #7

merged 3 commits into from
Nov 27, 2018

Conversation

evanmwillhite
Copy link

Companion to fourkitchens/emulsify#288, this removes the troublesome async attribute and replaces it with a data attribute that is detected in JS by a new script in the companion PR.

@philwolstenholme
Copy link
Collaborator

I've updated #4 to include this change, hopefully this'll prevent any merge conflicts if this PR is merged first

@philwolstenholme
Copy link
Collaborator

@evanmwillhite I've given this a try, but I'm getting errors in my console to do with missing jQuery, because my component library scripts are trying to run before my global library (which contains jQuery) is being loaded in the footer (this is done before <script src="../../js/reload-scripts.js"></script> is mentioned, so I think this setup should be okay.

I think we could prevent this by doing some more data-attribute trickery, for example, instead of using

<script data-name="reload" src="/path-to-file.min.js></script>

We could use:

<script data-name="reload" data-src="/path-to-file.min.js></script>

And then use the JS in Emulsify to swap the data-src for an actual src. This way, the libraries won't actually be loaded until reload-scripts has a chance to run.

@evanmwillhite
Copy link
Author

Great idea, @philwolstenholme! I've made this change here and it is working well for me (see latest on fourkitchens/emulsify#288). Can you verify on your end?

philwolstenholme added a commit that referenced this pull request Nov 5, 2018
@philwolstenholme
Copy link
Collaborator

Hi @evanmwillhite , I tried to test this but the patch (https://patch-diff.githubusercontent.com/raw/drupal-pattern-lab/attach-library-twig-extension/pull/7.patch) wouldn't apply as I'm already applying #4 as a patch and they both change the same lines.

Would it be possible for you to review and merge in #4 ?

@philwolstenholme
Copy link
Collaborator

philwolstenholme commented Nov 5, 2018

@evanmwillhite I wonder if we also need to do something to defer Emulsify's call to Drupal.attachBehaviors until all the script elements have had their data-src turned into a src, and then had their contents loaded so that Drupal behaviours have a chance to add the behaviour to Drupal.behaviors before the attachBehaviors function is called.

At the moment I'm doing something similar in my theme to what this PR proposes, but attachBehaviors (at the bottom of my _01-foot.twig file) is being called too soon.

I tried adding this to the end of reload-scripts.js:

if (
  typeof Drupal === 'object' &&
  typeof Drupal.attachBehaviors === 'function'
) {
  Drupal.attachBehaviors();
}

But it's still called too soon, presumably because even though the scripts.forEach will have run, the scripts will not necessarily have loaded?

Edit: for now I've done something very basic in _01-foot.twig:

  <!-- If using Drupal.behaviors, uncomment for them to work in Pattern Lab -->
  <script>Drupal.attachBehaviors();</script>
  <script>
    // A *very* basic way to account for the fact that libraries added in Pattern
    // Lab via a attach-library-twig-extension will be done after the above line
    // and pattern-lab/public/js/reload-scripts.js have been executed.
    // The timeout value can be adjusted if it is too generous or too conservative.

    // See https://github.com/drupal-pattern-lab/attach-library-twig-extension/pull/7
    setTimeout(function () {
      Drupal.attachBehaviors();
    }, 1000);
  </script>

@amazingrando amazingrando self-assigned this Nov 21, 2018
Copy link

@amazingrando amazingrando left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code and functional review pass.

@evanmwillhite
Copy link
Author

@philwolstenholme I used some of your code here, but I wanted to see if I could avoid the timeout since it could be variable. I pinged you on the other one already - just wanted to note it here.

@evanmwillhite evanmwillhite merged commit 78a3ed2 into master Nov 27, 2018
@evanmwillhite evanmwillhite deleted the data-name branch November 27, 2018 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants