Enqueueing additional ajax javascript file in Sage 9?

You can add code formatting using backticks. I’ve edited your post to demonstrate their use.

re: Your problem

When you say

But when I try to call my function on a page,

How are you attempting to call it? Inside a script tag in your body (i.e. <script>some_function()</script>)? Or are you trying to call it in main.js or somewhere else?

If you’re using a script tag in the body, then you’re attempting to execute the function before it’s been defined: Your wp_enqueue_script() call that enqueues ajax.js is enqueuing it in the footer. Changing the last argument of that call to false (or simply removing it) would enqueue ajax.js in the <head>.

If you’re calling some_function() in main.js, then you would need to add sage/ajax.js to the array of dependencies when you enqueue sage/main.js.

You may also want to double-check the following:

  • Does ajax.js appear in dist/scripts/ after you run yarn build?
  • Do the correct generated links to your scripts, and your embedded localized script, appear in the source of the page when WordPress generates it?
  • You appear to be localizing your ajax parameters to the object the_ajax_script but some_function() attempts to access ajax_object. (This probably isn’t causing your current problem, but it might create future ones.)