viewScript functions from dynamic block not available
-
Hi!
For context, I’m running WordPress 6.4.2.
I have created a dynamic block with the following block.json content (only showing relevant part here):
}, "editorScript": "file:./index.js", "editorStyle": "file:./index.css", "style": "file:./style-index.css", "render": "file:./render.php", "viewScript": "file:./view.js" }I have added several JavaScript functions to view.js that are later used in the block.
The script is loaded into the matching page like this:
<script type="text/javascript" src="<websiteURL - redacted>/wp-content/plugins/xtradeb-launchpad-integration/build/xd-lp-package-details/view.js?ver=df18059b4cb4a722b872" id="xd-lp-package-details-xtradeb-launchpad-integration-view-script-js" defer="defer" data-wp-strategy="defer"></script>I can browse and see the script content in the browser. However, none of its functions seem to be available and I get errors like this:
Uncaught ReferenceError: installButton is not defined at HTMLButtonElement.onclick (<post name - redacted>/:568:1336) onclick @ <post name - redacted>/:568If instead of using viewScript, I enqueue the same file as a dependency (here I renamed the file from view.js to details.js):
wp_enqueue_script( 'xd-lp-details-js', plugin_dir_url( __FILE__ ) . 'includes/js/details.js', array(), '', true );The script is loaded like this into the page in roughly the same location:
<script type="text/javascript" src="<websiteURL - redacted>/wp-content/plugins/xtradeb-launchpad-integration/includes/js/details.js?ver=4fd0f5bc13853fae144764d9c4a67c2c" id="xd-lp-details-js-js"></script>Now, all required functions are available and everything works as expected.
Why can’t I use the block “viewScript”? Why doesn’t it work?
What is the best way forward inline with best practices?
Thank you!
The topic ‘viewScript functions from dynamic block not available’ is closed to new replies.