simonw / datasette

An open source multi-tool for exploring and publishing data
https://datasette.io
Apache License 2.0
9.54k stars 688 forks source link

[plugins][feature request]: Support additional script tag attributes when loading custom JS #1692

Open hydrosquall opened 2 years ago

hydrosquall commented 2 years ago

Motivation

To be able to support legacy browsers without slowing down users with modern browsers, I would like to be able to set additional HTML attributes on the tag fallback script, nomodule and defer. My injected scripts should look something like this:

<script type="module" src="/index.my-es-module-bundle.js"></script>
<script src="/index.my-legacy-fallback-bundle.js" nomodule="" defer></script>

Proposal

To achieve this, I propose additional optional properties to the API accepted by the extra_js_urls hook and custom JS field the metadata.json described here.

Under this API, I'd write something like this to get the above HTML rendered in Datasette.

{
    "extra_js_urls": [
        {
            "url": "/index.my-es-module-bundle.js",
            "module": true,
        },
        {
            "url": "/index.my-legacy-fallback-bundle.js",
            "nomodule": "",
            "defer": true
        }
    ]
}

Resources

simonw commented 2 years ago

This is a good idea.

simonw commented 2 years ago

I like your design, though I think it should be "nomodule": True for consistency with the other options.

I think "async": True is worth supporting too.