coatless / quarto-webr

Community developed Quarto Extension to Embed webR for HTML Documents, RevealJS, Websites, Blogs, and Books.
https://quarto-webr.thecoatlessprofessor.com/
381 stars 18 forks source link

Consider allowing use of `ChannelType.PostMessage` in webR config #31

Closed georgestagg closed 1 year ago

georgestagg commented 1 year ago

Hey, I noticed you've updated to webR 0.2.1. That version contains a new communication channel, named PostMessage, which works even if the web page is not Cross-Origin Isolated, without the (admin & performance) overhead of setting up the Service Worker channel.

You might consider using it as a fallback in quarto-webr when crossOriginIsolated returns false in JS, in place of the automatic Service Worker-based fallback. Or at least provide it as an option to the user via a yaml option.

Positives:

Negatives:

If you'd like to experiment, you should be able to make the following (temporary!) change to webr-init.html to import ChannelType and force webR to use ChannelType.PostMessage:

  // Retrieve the webr.mjs
  import { WebR, ChannelType } from "https://webr.r-wasm.org/v0.2.1/webr.mjs";

  // Populate WebR options with defaults or new values based on 
  // webr meta
  globalThis.webR = new WebR({
    "baseURL": "{{BASEURL}}",
    "serviceWorkerUrl": "{{SERVICEWORKERURL}}",
    "homedir": "{{HOMEDIR}}",
    "channelType": ChannelType.PostMessage,
  });
coatless commented 1 year ago

@georgestagg Thanks for the note! I just rolled out the new meta variable option in #32. This is a great development since webR is now able to be used on Quarto Pub 🎉 🎉

https://coatless.quarto.pub/hidden-webr-code-cells/

On the fallback note, I noticed that the ChannelType.Automatic option is the default for WebROptions; however, it only tries to setup SharedBufferChannelMain and ServiceWorkerChannelMain.

https://github.com/r-wasm/webr/blob/2537c29aac3ab80555eb90bd3ef4ab56c1fdfaa9/src/webR/chan/channel-common.ts#L42-L52

Is there any way I could convince you to add PostMessageChannelMain fallback upstream?