Skip to content

Instantly share code, notes, and snippets.

@ShopifyEng
Last active November 30, 2021 20:42
Show Gist options
  • Save ShopifyEng/3daf1f47da96be35a7f0df4f18866dd5 to your computer and use it in GitHub Desktop.
Save ShopifyEng/3daf1f47da96be35a7f0df4f18866dd5 to your computer and use it in GitHub Desktop.
Remote Rendering: Shopify’s Take on Extensible UI - Sandbox code that enables a host to load and render a 3rd party extension
// Sandbox
import { createEndpoint, retain } from "@remote-ui/rpc";
import { createRemoteRoot } from "@remote-ui/core";
// The `regsiter` function will be available (as a global) for extensions to use
let renderCallback;
self.register = (callback) => {
renderCallback = callback;
};
// The `load` and `render` functions will be available (via RPC) for the host to use
async function load(scriptUrl) {
await import(scriptUrl);
}
function render(remoteChannel, api) {
retain([remoteChannel, api]);
const root = createRemoteRoot(remoteChannel);
renderCallback(root, api);
}
createEndpoint(self).expose({ load, render });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment