I've been trying to find questions, articles, or documentation covering this, but haven't been able to find what I need or haven't understood it yet.
I have a custom module that a colleague wrote (who has since left) that adds some functionality via an API endpoint that we consume in a different application.
I now need to add some javascript to the /customer/account/
page.
I've read this answer and understand that I need to put something like the below into a config file:
<?xml version="1.0"?>
<layout version="0.1.0">
<customer_account_view>
<reference name="head">
<action method="addJs"><script>{my script}.js</script></action>
</reference>
</customer_account_view>
</layout>
Where does {my script}.js
need to live inside my module directory, and which config file do I need to put the above block (if it is correct) inside my etc directory?
The current module directory looks like this:
UPDATE
The namespace for my module is James and the module is called AddToCart (i.e app/code/James/AddToCart
.
Following the advice inside solution 2 by @xanka, I first created the customer_account_index.xml
file at the following location app/code/James/AddToCart/view/frontend/layout/customer_account_index.xml
with the following contents:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<script src="james_addtocart::postMessage.js"/>
</head>
</page>
I then created the script inside the app/code/James/AddToCart/view/frontend/web/js/postMessage.js
file:
console.log('test');
I then ran the following commands, unsure if necessary:
php bin/magento maintenance:enable
php bin/magento setup:upgrade
php -d memory_limit=2G bin/magento setup:di:compile
php -d memory_limit=8G -d max_execution_time=18000 bin/magento setup:static-content:deploy -f
php bin/magento maintenance:disable
php bin/magento cache:clean
php bin/magento cache:flush
redis-cli -p 6379 flushall
Everything ran successfully, however I cannot see 'test' in my dev console when I log in and visit /customer/account
and when I view source, I cannot see that my script is included in the head. Where have I gone wrong?