OpenComponents Browser Client

2.1.10 · active · verified Tue Apr 21

The `oc-client-browser` package is the client-side JavaScript library for the OpenComponents (OC) framework, facilitating browser-based rendering of independently deployed micro-frontends. OpenComponents, an open-source framework developed at OpenTable, enables building and managing self-contained UI components (HTML, CSS, JS, often with server-side Node.js logic) that are published to a central OC registry. Currently at version 2.1.10, the client integrates by being included as a script in a web page, exposing a global `oc` object. It automatically scans the DOM for `<oc-component>` custom elements, fetches component data and compiled views from a configured OC registry, and dynamically injects rendered HTML and executes client-side JavaScript. This library is crucial for the OC philosophy, which emphasizes framework-agnosticism, granular UI ownership, and independent deployment to combat monolithic frontend architectures.

Common errors

Warnings

Install

Imports

Quickstart

This HTML snippet demonstrates how to include the `oc-client-browser` script, configure global settings, and render OpenComponents using both declarative `<oc-component>` tags and dynamic `oc.build()` calls, ensuring all components are properly initialized and displayed in the browser.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OC Client Browser Quickstart</title>
    <style>
        body { font-family: sans-serif; margin: 20px; }
        .component-container { border: 1px dashed #ccc; padding: 15px; margin-top: 20px; }
        oc-component { display: block; min-height: 50px; background-color: #f9f9f9; padding: 10px; }
    </style>
</head>
<body>
    <h1>OpenComponents Browser Client Example</h1>
    <p>This page demonstrates client-side rendering of an OpenComponent fetched from a registry.</p>

    <div class="component-container">
        <h2>Hello World Component</h2>
        <!-- The oc-component tag tells the client where to render -->
        <oc-component 
            href="https://your-oc-registry.com/components/hello-world/~1.0.0/?name=RegistryUser"
            data-oc-params='{"message":"Hello from the browser!"}'>
            Loading 'hello-world' component...
        </oc-component>
    </div>

    <div class="component-container">
        <h2>Dynamic Component</h2>
        <div id="dynamic-component-mount">Loading dynamic component...</div>
    </div>

    <!-- Optional: Configure global 'oc' settings before loading the client script -->
    <script>
        var oc = oc || {};
        oc.conf = oc.conf || {};
        oc.conf.retryInterval = 1500; // Retry fetching components every 1.5 seconds on failure
        oc.conf.baseUrl = 'https://your-oc-registry.com/components'; // Base URL for `oc.build` calls
    </script>

    <!-- Load the OpenComponents browser client script, ideally at the end of <body> -->
    <script src="https://your-oc-registry.com/oc-client/client.js"></script>

    <script>
        // Ensure the global 'oc' object is ready before using it
        window.oc.cmd.push(function(ocClient) {
            console.log("OpenComponents client is ready! Version:", ocClient.version);

            // Example of dynamically building and rendering a component
            const dynamicComponentHtml = ocClient.build({
                name: 'another-component', // Replace with a valid component name from your registry
                version: '~1.0.0', // Specify version
                parameters: { title: 'Generated Component', content: 'This component was built dynamically!' }
            });

            const mountPoint = document.getElementById('dynamic-component-mount');
            if (mountPoint) {
                mountPoint.innerHTML = dynamicComponentHtml;
                ocClient.renderUnloadedComponents(); // Important: Render dynamically added <oc-component> tags
            }
        });
    </script>
</body>
</html>

view raw JSON →