trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Avoiding inline script tag in built index.html

Open ulf5 opened this issue 3 years ago • 9 comments

It would be great if it was possible to avoid having the

import init from '/index-....js';init('/index-..._bg.wasm');

part of the html in a separate file and load it as a module, similar to React's INLINE_RUNTIME_CHUNK=false

This is so allowing script-src 'unsafe-inline' can be avoided when setting Content Security Policy headers.

ulf5 avatar Dec 24 '21 20:12 ulf5

That's a good point. I gave this a try by just appending the init('....wasm'); call to the end of the index....js file and changing the HTML line to <script type="module" src="/index....js"></script>. That worked just fine, so I think we can implement it rather easy.

I think a good place for this setting would be the special HTML link:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link data-trunk rel="rust" data-no-inline>
    <!--                        ^ new option here -->
  </head>
  <body>
  </body>
</html>

If that is set, we simply append the call to the wasm-bindgen output (it updates the wasm file and generates the JS bindings file) and change the import statement in the output HTML. What do you think @thedodd ?

dnaka91 avatar Jan 12 '22 03:01 dnaka91

Is the additional attribute required? I'd argue that putting import ... in its own file and including that file from index.html could just be the default behaviour.

oberien avatar Feb 23 '22 13:02 oberien

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 24 '23 00:11 github-actions[bot]

Since I care a great deal about proper CSP headers, I'm going to poke this to ward off the bot. If it's been fixed, let a human actively close it.

ssokolow avatar Nov 24 '23 01:11 ssokolow

Since I care a great deal about proper CSP headers, I'm going to poke this to ward off the bot. If it's been fixed, let a human actively close it.

@ssokolow Honestly I am not sure what the state is. If you know more, maybe you could re-evaluate this. And of course: PRs welcome :)

ctron avatar Dec 13 '23 10:12 ctron