|
| 1 | +--- |
| 2 | +sidebar_position: 10 |
| 3 | +title: Hosting Bitbybit Assets on Your Own CDN |
| 4 | +sidebar_label: Self-Hosting Assets |
| 5 | +description: Learn how to host Bitbybit assets on your own CDN infrastructure for improved reliability, performance, and control. |
| 6 | +tags: [cdn, hosting, infrastructure, deployment, performance, assets] |
| 7 | +--- |
| 8 | + |
| 9 | +import Admonition from '@theme/Admonition'; |
| 10 | + |
| 11 | +# Hosting Bitbybit Assets on Your Own CDN |
| 12 | + |
| 13 | +To simplify the setup process for new users and customers, our runners and various assets are hosted on a generally available CDN called **jsDelivr**. Our online editors and runners that you set up on your website all load critical assets from the jsDelivr CDN. While convenient, this dependency introduces a potential point of failure: what if your website suddenly stops working because jsDelivr went down? This is a realistic scenario that happens more often than we'd like to admit. |
| 14 | + |
| 15 | +The solution? Host Bitbybit assets on your own infrastructure using a CDN provider you trust. |
| 16 | + |
| 17 | +## Understanding CDNs |
| 18 | + |
| 19 | +### What is a CDN? |
| 20 | + |
| 21 | +A **Content Delivery Network (CDN)** is a geographically distributed network of servers designed to deliver web content efficiently to users based on their physical location. Instead of serving all content from a single origin server, CDNs replicate and cache your static assets (JavaScript files, images, fonts, etc.) across multiple data centers worldwide. |
| 22 | + |
| 23 | +### How CDNs Function |
| 24 | + |
| 25 | +When a user requests content from a CDN-enabled website: |
| 26 | + |
| 27 | +1. The CDN receives the request and determines the user's geographic location |
| 28 | +2. The request is routed to the nearest edge server (Point of Presence) |
| 29 | +3. If the content is cached on that edge server, it's delivered immediately |
| 30 | +4. If not cached, the edge server retrieves it from the origin, caches it, and serves it to the user |
| 31 | +5. Subsequent requests from nearby users receive the cached version |
| 32 | + |
| 33 | +### Performance Benefits of CDNs |
| 34 | + |
| 35 | +CDNs significantly improve website performance through: |
| 36 | + |
| 37 | +- **Reduced Latency**: Content is served from geographically closer servers, minimizing the physical distance data travels |
| 38 | +- **Faster Load Times**: Cached static assets load instantly without hitting the origin server |
| 39 | +- **Bandwidth Optimization**: CDNs compress and optimize content delivery |
| 40 | +- **Load Distribution**: Traffic is distributed across multiple servers, preventing bottlenecks |
| 41 | +- **High Availability**: If one server fails, requests automatically route to another |
| 42 | + |
| 43 | +### Why Consider Self-Hosting Bitbybit Assets? |
| 44 | + |
| 45 | +While we provide jsDelivr hosting for convenience, there are compelling reasons to host assets on your own infrastructure: |
| 46 | + |
| 47 | +1. **Reliability & Control**: You're not dependent on third-party uptime. If jsDelivr experiences an outage, your application continues functioning |
| 48 | +2. **Performance Optimization**: Choose a CDN provider optimized for your specific user base and geographic regions |
| 49 | +3. **Version Stability**: Pin specific versions without worrying about external changes or deprecations |
| 50 | +4. **Compliance & Security**: Meet enterprise requirements for asset hosting and security policies |
| 51 | +5. **Custom Caching Strategies**: Implement caching rules tailored to your application's needs |
| 52 | +6. **Cost Management**: For high-traffic applications, your own CDN plan might be more cost-effective |
| 53 | + |
| 54 | +## Downloading Bitbybit Assets |
| 55 | + |
| 56 | +All assets used by the Bitbybit platform are available for download from our GitHub repository: |
| 57 | + |
| 58 | +**[Bitbybit Assets Releases](https://github.com/bitbybit-dev/bitbybit-assets/releases)** |
| 59 | + |
| 60 | +Each release contains a complete set of assets packaged as a `.zip` file. Download the version that matches your Bitbybit implementation. |
| 61 | + |
| 62 | +## Identifying Required Assets |
| 63 | + |
| 64 | +### You Don't Need Everything |
| 65 | + |
| 66 | +An important consideration: **you probably don't need to host all assets**, only the ones your specific implementation requires. |
| 67 | + |
| 68 | +**Examples:** |
| 69 | +- If you use `bitbybit-runner-babylonjs.js`, you won't need `bitbybit-runner-threejs.js` or the lite runners |
| 70 | +- If you only use the OCCT kernel, you'll only need to host `bitbybit-occt-webworker.js` |
| 71 | +- If you never load GLTF files with Draco compression, you don't need the Draco decompressor |
| 72 | + |
| 73 | +### Determining Your Asset Dependencies |
| 74 | + |
| 75 | +Identifying exactly which assets you need depends on the features you're implementing. Some assets are loaded dynamically only when specific features are activated. |
| 76 | + |
| 77 | +<Admonition type="tip" title="Recommended Approach for Identifying Assets"> |
| 78 | + |
| 79 | +1. **Use Browser DevTools**: Open your application and access the Network tab |
| 80 | +2. **Exercise All Features**: Navigate through your application, activating all features and workflows |
| 81 | +3. **Filter CDN Requests**: Look for requests matching the pattern: |
| 82 | + ``` |
| 83 | + https://cdn.jsdelivr.net/gh/bitbybit-dev/bitbybit-assets@<version-number-of-bitbybit> |
| 84 | + ``` |
| 85 | +4. **Document Assets**: Note every asset loaded from this domain—these are the files you should migrate to your own hosting |
| 86 | + |
| 87 | +</Admonition> |
| 88 | + |
| 89 | +### Alternative Approach: Host Everything |
| 90 | + |
| 91 | +While potentially inefficient, you can simply host the complete assets folder on your infrastructure. This ensures all potential dependencies are available, though it may consume more storage and bandwidth than necessary. |
| 92 | + |
| 93 | +## Configuring the Runner to Use Your CDN |
| 94 | + |
| 95 | +### Using the Runner with `cdnUrl` Option |
| 96 | + |
| 97 | +When initializing the Bitbybit Runner, you pass an options object containing properties like `canvasId`, `enableOCCT`, etc. This same object supports a property called **`cdnUrl`**, which tells the runner where to load assets from. |
| 98 | + |
| 99 | +**Example Configuration:** |
| 100 | + |
| 101 | +```javascript |
| 102 | +const runnerOptions = { |
| 103 | + canvasId: 'myCanvas', |
| 104 | + enableOCCT: true, |
| 105 | + enableJSCAD: false, |
| 106 | + enableManifold: false, |
| 107 | + cdnUrl: 'https://cdn.yourownhosting.com/bitbybit/', // Your custom CDN URL |
| 108 | + loadFonts: [], |
| 109 | +}; |
| 110 | + |
| 111 | +const runner = window.bitbybitRunner.getRunnerInstance(); |
| 112 | +const { bitbybit, Bit } = await runner.run(runnerOptions); |
| 113 | +``` |
| 114 | + |
| 115 | +### Using NPM Packages with `GlobalCDNProvider` |
| 116 | + |
| 117 | +If you're using Bitbybit's NPM packages directly in your project (rather than the standalone runner), you need to configure the CDN URL differently. Import the `GlobalCDNProvider` from `@bitbybit-dev/base` and set the CDN URL before initializing any Bitbybit services. |
| 118 | + |
| 119 | +**Example Configuration:** |
| 120 | + |
| 121 | +```javascript |
| 122 | +import { GlobalCDNProvider } from "@bitbybit-dev/base"; |
| 123 | + |
| 124 | +// Set your custom CDN URL before initializing Bitbybit |
| 125 | +GlobalCDNProvider.BITBYBIT_CDN_URL = "https://cdn.yourownhosting.com/bitbybit/"; |
| 126 | + |
| 127 | +// Now initialize your Bitbybit services as usual |
| 128 | +// All asset requests will use your custom CDN URL |
| 129 | +``` |
| 130 | + |
| 131 | +<Admonition type="info" title="NPM Package Users"> |
| 132 | + <p>If you're using NPM packages like <code>@bitbybit-dev/occt</code>, <code>@bitbybit-dev/babylonjs</code>, or <code>@bitbybit-dev/threejs</code>, you must use the <code>GlobalCDNProvider</code> approach. The <code>cdnUrl</code> option in the runner configuration only applies when using the standalone runner files.</p> |
| 133 | +</Admonition> |
| 134 | + |
| 135 | +### Important Considerations |
| 136 | + |
| 137 | +**Public Access:** All hosted assets must be completely public and accessible without authentication. |
| 138 | + |
| 139 | +**Recommended Hosting Strategy:** |
| 140 | +- Use global CDN providers (e.g., AWS CloudFront, Cloudflare, Azure CDN, Google Cloud CDN) |
| 141 | +- Enable automatic deployment of static assets across regions |
| 142 | +- Implement proper caching headers and strategies |
| 143 | +- Configure cache invalidation when updating versions |
| 144 | + |
| 145 | +**Geographic Distribution:** Quality CDN providers automatically distribute assets to edge locations closest to your users, minimizing latency and maximizing performance. |
| 146 | + |
| 147 | +**Path Structure:** Ensure your CDN URL maintains the same folder structure as the Bitbybit assets repository. The runner expects assets to be organized in specific paths relative to the `cdnUrl`. |
| 148 | + |
| 149 | +<Admonition type="warning" title="Maintain Correct Folder Structure"> |
| 150 | + <p>The runner expects assets to follow the exact same folder structure as found in the Bitbybit assets repository. If the structure doesn't match, assets will fail to load.</p> |
| 151 | +</Admonition> |
| 152 | + |
| 153 | +## Testing Your Configuration |
| 154 | + |
| 155 | +After setting up your custom CDN: |
| 156 | + |
| 157 | +1. Clear your browser cache |
| 158 | +2. Load your application |
| 159 | +3. Open DevTools Network tab |
| 160 | +4. Verify all assets load from your CDN domain |
| 161 | +5. Test all features to ensure no assets are missing |
| 162 | +6. Monitor for any 404 errors indicating missing files |
| 163 | + |
| 164 | +## Popular CDN Providers |
| 165 | + |
| 166 | +Here are some popular CDN providers you might consider for hosting Bitbybit assets: |
| 167 | + |
| 168 | +- **Cloudflare**: Offers a generous free tier with global edge locations |
| 169 | +- **AWS CloudFront**: Integrates seamlessly with S3 storage and offers fine-grained control |
| 170 | +- **Azure CDN**: Excellent for enterprises already using Microsoft Azure |
| 171 | +- **Google Cloud CDN**: Fast global network, ideal for Google Cloud users |
| 172 | +- **Fastly**: Premium CDN with real-time configuration and edge computing capabilities |
| 173 | +- **BunnyCDN**: Cost-effective option with good performance and simple pricing |
| 174 | + |
| 175 | +Choose a provider based on your budget, geographic user base, existing infrastructure, and specific feature requirements. |
| 176 | + |
| 177 | +## Conclusion |
| 178 | + |
| 179 | +While jsDelivr hosting provides convenience for getting started, self-hosting Bitbybit assets gives you greater control, reliability, and performance optimization opportunities. By carefully selecting the assets you need and configuring a robust CDN solution, you can ensure your Bitbybit-powered applications remain fast, reliable, and independent of third-party infrastructure. |
| 180 | + |
| 181 | +For more information about using the Bitbybit Runner, see [Introducing Bitbybit Runner](/learn/runners/intro). |
0 commit comments