SFRA Server-side Javascript - Source: app_storefront_base/cartridge/scripts/assets.js menu

SFRA / Server-side JS / Source: app_storefront_base/cartridge/scripts/assets.js

  1. 'use strict';
  2. var URLUtils = require('dw/web/URLUtils');
  3. var styles = [];
  4. var scripts = [];
  5. /**
  6. * If the resource to add is not already in the resource array then add it to the array
  7. * @param {Array} resourceArray - Either the scripts or styles array to which you want to add the resource src to.
  8. * @param {string} src - URI of the resource to add
  9. * @param {string} integrity - cryptographic hash of a resource
  10. */
  11. function addResource(resourceArray, src, integrity) {
  12. var result = {};
  13. var exists = resourceArray.some(function (element) {
  14. return element.src === src;
  15. });
  16. if (!exists) {
  17. result.src = src;
  18. if (integrity) {
  19. result.integrity = integrity;
  20. }
  21. resourceArray.push(result);
  22. }
  23. }
  24. module.exports = {
  25. addCss: function (src, integrity) {
  26. if (/((http(s)?:)?\/\/).*.css/.test(src)) {
  27. addResource(styles, src, integrity);
  28. } else {
  29. addResource(styles, URLUtils.staticURL(src).toString(), integrity);
  30. }
  31. },
  32. addJs: function (src, integrity) {
  33. if (/((http(s)?:)?\/\/).*.js/.test(src)) {
  34. addResource(scripts, src, integrity);
  35. } else {
  36. addResource(scripts, URLUtils.staticURL(src).toString(), integrity);
  37. }
  38. },
  39. scripts: scripts,
  40. styles: styles
  41. };