I've created a custom block (via Drupal admin) that uses Twitter's API to insert tweets into a page. The necessary JavaScript loads for authenticated users but not anonymous users.

I configured the JS in my theme.libraries.yml file, and use attach_library() in a Twig template to load the JS on pages that use the custom block as seen below:

# theme.libraries.yml

    https://platform.twitter.com/widgets.js: { type: external, minified: true, attributes: { async: true, charset: utf-8 } }
{# web/themes/theme/templates/block--bundle--twitter-embedded-timeline.html.twig #}

<section class="twitter-embedded-timeline">
    href="https://twitter.com/{{ content.field_twitter_account.0 }}"
    >Tweets by us

{{ attach_library('theme/twitter-widgets') }}

For authenticated users, the <script> tag pointing to the external JS is visible in the DOM, which transforms the Twig markup into a full-blown twitter feed widget.

For anonymous users, there is no related <script> tag in the DOM, and only the original, untransformed Twig markup is present in the DOM, not the full-blown twitter feed (since the JS isn't present to transform it).

How to get the JS to load for anonymous users?

  • How are you determining the JS to not load? Are there errors in your console? Are you sure the code works etc? You haven't shown the JS so it's hard to give feedback.
    – Jaypan
    Commented Jul 12, 2022 at 18:42
  • Hi @Jaypan There's no JS to show in this case. It works as shown above for authenticated users, meaning the Twig template and the JS-powered functionality work as expected, the Twitter JS transforms the <a> tag into a full blown twitter feed widget. For anonymous users, only the raw <a> tag from the Twig template markup shows up, and there is no associated <script> tag pointing to the JS like there is for authenticated users. Commented Jul 12, 2022 at 21:06
  • @Jaypan I've updated my question to make the points made in my initial comment to you more clear. Commented Jul 12, 2022 at 21:11
  • 1
    This may seem a silly question but did you rebuild the cache?
    – mona lisa
    Commented Jul 14, 2022 at 13:57
  • 1
    Is any error in the browser console for anon user?
    – Giuseppe
    Commented Jul 18, 2022 at 13:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.