I have a Magento module that currently provides a custom API route. I am wanting to update it to push some Javascript to the /customer/account
page.
I tried following the answer here but this didn't work, my script is not rendered or executed on the customer account page.
My module looks like this:
My module lives in app/Code/James/AddToCart
.
I have a app/code/James/AddToCart/view/frontend/layout/customer_account_index.xml
file that looks like this:
<?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 have the Javascript file living in app/code/James/AddToCart/view/frontend/web/js/postMessage.js
and it looks like this:
console.log('test');
alert('I am working');
The real script I will use is not the above, however it is all just vanilla javascript with no dependencies.
I then ran these commands (not sure if necessary):
php bin/magento maintenance:enable
php bin/magento setup:upgrade
php -d memory_limit=2G bin/magento setup:di:compile
history
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
However when I go to the /customer/account page, I do not see my script run at all.
What am I doing wrong?
Update
As per @xanka's comment, I have updated my customer_account_index.xml
file to be:
<?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>
However this still isn't running the alert nor the console.log statement.
Update 2
I have added the postMessage.js
file to app/code/James/AddToCart/view/frontend/web/postMessage.js
and then added a new app/code/James/AddToCart/view/frontend/layout/customer_account.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>
<link src="James_AddToCart::postMessage.js"/>
</head>
</page>
I now have a postMessage.js
file in app/code/James/AddToCart/view/frontend/web/postMessage.js
and app/code/James/AddToCart/view/frontend/web/js/postMessage.js
that looks like this:
require(["jquery"],
function($){
console.log('test');
alert('I am working');
})
When I visit the /customer/account
page, I do not see an alert or the console.log statement in my dev tools.
James_AddToCart::postMessage.js
./customer/account
page I do not get the alert nor the console log in my dev tools. I have updated thecustomer_account_index.xml
with what you say (now included in question). Is there something more I need to do?<link src="James_AddToCart::js/postMessage.js"/>