", "author": { "@type": "Person", "name": "rggm", "image": "https://ia800305.us.archive.org/31/items/discordprofilepictures/discordblue.png", "url": "https://discordapp.com/users/1110658808033849484" }, "datePublished": "2024-09-24T16:18:31.576+00:00" }, { "@type": "Comment", "text": "Hi I have set up a bare minimum example of how you could use something like Observerble plot in toddle here https://editor.nordcraft.com/projects/red_tarfful_prickly_gamefowl/branches/start/actions/plot?leftpanel=design&rightpanel=style&canvas-width=800&canvas-height=800I use an action to dynamically load the library. Then in a component use the action and assign the plot onto a div using its ID. Please take a look at the example to understand how it works. If you have the time, I suggest you abstract the third party into a package so others can easily use it as well", "author": { "@type": "Person", "name": "Jacob Kofoed", "image": "https://cdn.discordapp.com/avatars/239798169037176833/0a8ec7d4e964f02f6451ab89dd26a3d7", "url": "https://discordapp.com/users/239798169037176833" }, "datePublished": "2024-09-24T17:45:42.931+00:00" }, { "@type": "Comment", "text": "Wow! It seems to be easier than I imagined XD. Thank you so much!! This is a lifesaver!", "author": { "@type": "Person", "name": "rggm", "image": "https://ia800305.us.archive.org/31/items/discordprofilepictures/discordblue.png", "url": "https://discordapp.com/users/1110658808033849484" }, "datePublished": "2024-09-24T20:22:04.035+00:00" } ], "text": "Hi we are trying import a function from a CDN package (that is added into a page script tag) into a custom javascript action and unable to get it working, just wondering what the correct way to handle this?" }
PricingBlog

Cannot use import statement outside a module

  • darrenbignall-1176859629309931520

    DarrenBignall

    2 years ago

    Hi we are trying import a function from a CDN package (that is added into a page script tag) into a custom javascript action and unable to get it working, just wondering what the correct way to handle this?
  • jacobkofoed-1176862105383731200

    Jacob Kofoed

    2 years ago

    If it's an ESM module, then you can do something similar to https://docs.nordcraft.com with await import('https://cdn/esm-module.js').

    If it's not an ESM module, but just a standard script, then depending on the script you import in head, you should be able to find it somewhere on the global scope. globalThis.script_name.function_name()

    Can you tell me what script you're trying to use?
  • jacobkofoed-1176875709843644416

    Jacob Kofoed

    2 years ago

    There are some different ways to do this:

    1) Use ESM modules, cdn.jsdelivr.net supports this by adding /+esm to the url. So your custom action could look something like:

    async function TriggerAppwrite(args, ctx) {
    const { Client, Account } = await import('https://cdn.jsdelivr.net/npm/appwrite@13.0.1/+esm');
    console.log(Client, Account);
    }


    2) Add a script to your head like in the attached screenshot. Then in your custom actions, you can access Appwrite with:

    async function TriggerAppwrite(args, ctx) {
    const { Client, Account } = globalThis.Appwrite;
    console.log(Client, Account);
    }


    Keep in mind that adding scripts to the head will make them load while blocking rendering. So the first option is usually the better option. However, some scripts requires to be loaded before the page is rendered.
    1176875709583609908-CleanShot_2023-11-22_at_13.01.272x.png
    🔥1
  • darrenbignall-1176889173886640168

    DarrenBignall

    2 years ago

    This is awesome, thank you for your help @Jacob Kofoed will give it a go now
  • mvvdv-1221782046834430052

    MVvdV

    1 year ago

    @Jacob Kofoed have a similar error, i did get it working with the 1st of the 2 methods mentioned above however, in that case any other actions that require the imported JS have no knowledge of the import outside of the action in which the import happens. Is there a way to make the import global other than through the header?
  • mvvdv-1222307709718892617

    MVvdV

    1 year ago

    For others who are looking for this.

    I required a JS package I needed to import to be globally accessible, which means I can have several custom functions refer to it without having to import it within every custom function.

    The script tag in the header is the way forward but the package did not attach itself to the global scope via the esm link alone.

    What I ended up implementing is:

    <script type=“module”>import * as packageName from 'https://cdn.jsdelivr.net/npm/packageName/Version/+esm'; window.packageName = packageName;</script>

    You can attach the package to the global scope yourself with
    window.packageName = packageName;

    Then in any custom action function you can use any of the modules or functions by calling:
    globaThis.packageName.SomeModuleFuncion();

    Hope this is helpful to anyone.
    1222307709651648512-Screenshot_2024-03-27_at_09.07.28.png
  • Tod-1222307711857983540

    Tod

    1 year ago

    Great energy @MVvdV! Your continuous contribution to the toddle Community just made you advance to Community Level 2!
  • mvvdv-1222321617506533517

    MVvdV

    1 year ago

    I realised the attachment of the package to the global scope can also be achieved from within an async function which means you can in an 'onLoad' custom action just as well
  • lucasg-1222324475593232384

    Lucas G

    1 year ago

    Depending on the action, you might not even need to load it in different events. You might be able to load all your functions in the one action and be able to call them as needed.
  • rggm-1288172728540463166

    rggm

    1 year ago

    I'm sorry, I tried but couldn't manage it. I want to integrate some charts with Observable. How can I use the starter example:
    <!DOCTYPE html>
    <div id="myplot"></div>
    <script type="module">
    import * as Plot from "https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm";
    const plot = Plot.rectY({length: 10000}, Plot.binX({y: "count"}, {x: Math.random})).plot();
    const div = document.querySelector("#myplot");
    div.append(plot);
    </script>
  • jacobkofoed-1288194670433927301

    Jacob Kofoed

    1 year ago

    Hi @rggm I have set up a bare minimum example of how you could use something like Observerble plot in toddle here https://editor.nordcraft.com/projects/red_tarfful_prickly_gamefowl/branches/start/actions/plot?leftpanel=design&rightpanel=style&canvas-width=800&canvas-height=800

    I use an action to dynamically load the library. Then in a component use the action and assign the plot onto a div using its ID. Please take a look at the example to understand how it works. If you have the time, I suggest you abstract the third party into a package so others can easily use it as well 👍
    👍1
  • rggm-1288234017635700818

    rggm

    1 year ago

    Wow! It seems to be easier than I imagined XD. Thank you so much!! This is a lifesaver!