2

I've ran into a strange problem where a custom javascript file 404s when loaded via a RequireJS alias but it works fine when loaded directly.

This does not work

Require JS

var config = {
    map: {
        '*': {
            recentlyViewedCarousel: 'Magento_Reports/js/recently-viewed'
        }
    }
};

Template

File: app/design/frontend/Vendorname/theme/Magento_Reports/templates/widget/viewed/column/viewed_images_list.phtml

<?php if ($exist): ?>
    <script type="text/x-magento-init">
        {
            "*": {
                "recentlyViewedCarousel": {}
            }
        }
    </script>
<?php endif; ?>

Error

GET http://localhost:3000/static/frontend/Vendorname/default/en_GB/recentlyViewedCarousel.js 404

require.js:166 Uncaught Error: Script error for: recentlyViewedCarousel

Yet this does work

Template

File: app/design/frontend/Vendorname/theme/Magento_Reports/templates/widget/viewed/column/viewed_images_list.phtml

<?php if ($exist): ?>
    <script type="text/x-magento-init">
        {
            "*": {
                "Magento_Reports/js/recently-viewed-carousel": {}
            }
        }
    </script>
<?php endif; ?>

The path is the exact same, the only difference is the method that does not work uses a Require alias and the method that does work directly references the file.

I have done this exact same thing as part of the Magento_Theme module and it works fine.

I'm happy with the workaround but I'm intrigued as to what's causing this.

This template is loaded via the CMS recently viewed widget inside a static block, so it may be a bug where Require aliases do not work with CMS widgets?

  • Is it ok that in config you are using the recentlyViewedProductsCarousel alias but then calling it by another name recentlyViewedCarousel? – Siarhey Uchukhlebau Sep 20 '17 at 12:21
  • That was a typo sorry, I changed my code around half way through typing the question. Good spot though thanks – Ben Crook Sep 28 '17 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.