Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hydrosquall opened this issue Mar 27, 2022 · 2 comments

Comments

@hydrosquall
Copy link
Contributor

Motivation

  • The build system for my new plugin has two output JS files, one for browsers that support ES modules, one for browsers that don't. At present, I'm only passing one of them into Datasette.
  • I'd like to specify the non-es-module script as a fallback for older browsers. I don't want to load it by default, because browsers will only need one, and it's heavy, so for now I'm only supporting modern browsers.

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

  • MDN on the script tag
    • There may be other properties that could be added that are potentially valuable, like async or referrerpolicy, but I don't have an immediate need for those.
@simonw
Copy link
Owner

simonw commented Mar 30, 2022

This is a good idea.

@simonw
Copy link
Owner

simonw commented Mar 30, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants