{"library":"oc-client-browser","title":"OpenComponents Browser Client","description":"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.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install oc-client-browser"],"cli":null},"imports":["<!-- Add <script src=\"https://your-registry.com/oc-client/client.js\"></script> to your HTML -->\n<script>\n  window.oc.cmd.push(function(ocClient) {\n    // Use ocClient object here\n    console.log(ocClient);\n  });\n</script>","declare global {\n  interface Window {\n    oc: OCObject;\n  }\n}\n// Then use `window.oc` in your TypeScript code\n// import type { OCObject } from 'oc-client-browser'; // For types if bundled and exported","window.oc.cmd.push(function(ocClient) {\n  const componentHtml = ocClient.build({\n    name: 'my-component',\n    version: '~1.0.0',\n    parameters: { data: 'value' }\n  });\n  document.getElementById('container').innerHTML = componentHtml;\n});"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>OC Client Browser Quickstart</title>\n    <style>\n        body { font-family: sans-serif; margin: 20px; }\n        .component-container { border: 1px dashed #ccc; padding: 15px; margin-top: 20px; }\n        oc-component { display: block; min-height: 50px; background-color: #f9f9f9; padding: 10px; }\n    </style>\n</head>\n<body>\n    <h1>OpenComponents Browser Client Example</h1>\n    <p>This page demonstrates client-side rendering of an OpenComponent fetched from a registry.</p>\n\n    <div class=\"component-container\">\n        <h2>Hello World Component</h2>\n        <!-- The oc-component tag tells the client where to render -->\n        <oc-component \n            href=\"https://your-oc-registry.com/components/hello-world/~1.0.0/?name=RegistryUser\"\n            data-oc-params='{\"message\":\"Hello from the browser!\"}'>\n            Loading 'hello-world' component...\n        </oc-component>\n    </div>\n\n    <div class=\"component-container\">\n        <h2>Dynamic Component</h2>\n        <div id=\"dynamic-component-mount\">Loading dynamic component...</div>\n    </div>\n\n    <!-- Optional: Configure global 'oc' settings before loading the client script -->\n    <script>\n        var oc = oc || {};\n        oc.conf = oc.conf || {};\n        oc.conf.retryInterval = 1500; // Retry fetching components every 1.5 seconds on failure\n        oc.conf.baseUrl = 'https://your-oc-registry.com/components'; // Base URL for `oc.build` calls\n    </script>\n\n    <!-- Load the OpenComponents browser client script, ideally at the end of <body> -->\n    <script src=\"https://your-oc-registry.com/oc-client/client.js\"></script>\n\n    <script>\n        // Ensure the global 'oc' object is ready before using it\n        window.oc.cmd.push(function(ocClient) {\n            console.log(\"OpenComponents client is ready! Version:\", ocClient.version);\n\n            // Example of dynamically building and rendering a component\n            const dynamicComponentHtml = ocClient.build({\n                name: 'another-component', // Replace with a valid component name from your registry\n                version: '~1.0.0', // Specify version\n                parameters: { title: 'Generated Component', content: 'This component was built dynamically!' }\n            });\n\n            const mountPoint = document.getElementById('dynamic-component-mount');\n            if (mountPoint) {\n                mountPoint.innerHTML = dynamicComponentHtml;\n                ocClient.renderUnloadedComponents(); // Important: Render dynamically added <oc-component> tags\n            }\n        });\n    </script>\n</body>\n</html>","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}