Roots Discourse

Why is main.js not being included on blog page

I noticed that only on all single blog post pages, main.js was not being included as a script tag in the head. Example page:
https://accountability-framework.org/how-to-bake-an-ethical-cookie/

And I’m trying to determine why.

In our layouts/app.blade.php we have:

@include('partials.head')

which I gather brings in partials/head.blade.php which contains:

@php wp_head() @endphp

which I gather brings in app/setup.php which contains:

add_action('wp_enqueue_scripts', function () {
    wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
    wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);

    wp_localize_script( 'sage/main.js', 'ajax_obj', array(
        'url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('ajax_nonce')
    ) );

    if (is_single() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}, 100);

which, according to this:

I thought would include main.js in every template that has:

@extends('layouts.app')

which is the case for all our templates as far as I can tell.

For example,

template-blog.blade.php
template-custom.blade.php
single.blade.php

all contain @extends('layouts.app')

Also note that we don’t appear to have these files:

layouts/base.blade.php
resources/views/index.blade.php

Any ideas as to why main.js is not present only on single blog post pages?

The code you’ve posted will never enqueue your JS in the head because you’ve set the in_footer argument to true:

Your JS will appear in the footer, which it does on all pages I checked of the site you linked except the single blog. It hooks into @php wp_footer() @endphp in the default layouts/app.blade.php, so anything that @extends that should work.

Without seeing more of your theme it’s hard to say the reason, but I would guess one of the following might be the problem:

  • @php wp_footer() @endphp does not appear in your layouts/app.blade.php but does appear in some other partial or template that is used by every template other than your single blog template.
  • On the blog something happens in code executed in the footer or hooked into wp_footer that causes it to error out in a way that prevents the script enequing to happen (error logs might have some evidence).
  • Something else is hooking into wp_enqueue_scripts or another hook and removing or overwriting your enqueue in some way (maybe a plugin?).

Thanks, I see what you mean about main.js coming in via the footer. We do have this in our layouts/app.blade.php:

@php do_action('get_footer') @endphp
@include('partials.footer')
@php wp_footer() @endphp

and I don’t see any errors in the logs. The plugins we have are:

AddToAny Share Buttons
Advanced Custom Fields PRO
Akismet Anti-Spam
Category Order and Taxonomy Terms Order
Mailchimp for WordPress
Post Types Order
WPML Media
WPML Multilingual CMS
WPML String Translation
WPML Translation Management

So I’ll see if I can figure out if one of those is causing the problem by process of elimination…