diff --git a/plugins/woocommerce-blocks/assets/js/base/utils/lazy-load-script.ts b/plugins/woocommerce-blocks/assets/js/base/utils/lazy-load-script.ts new file mode 100644 index 00000000000..06a85308dad --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/base/utils/lazy-load-script.ts @@ -0,0 +1,82 @@ +interface lazyLoadScriptParams { + handle: string; + src: string; + version?: string; + after?: string; + before?: string; + translations?: string; +} + +/** + * In WP, registered scripts are loaded into the page with an element like this: + * `` + * This function checks whether an element matching that selector exists. + * Useful to know if a script has already been appended to the page. + */ +const isScriptTagInDOM = ( scriptId: string ): boolean => { + const scriptElements = document.querySelectorAll( `script#${ scriptId }` ); + return scriptElements.length > 0; +}; + +/** + * Appends a `