Skip to content

@gitbook/adaptive #808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 112 additions & 24 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/adaptive/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
42 changes: 42 additions & 0 deletions packages/adaptive/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const esbuild = require('esbuild');

// Common settings
const commonSettings = {
entryPoints: ['src/index.ts'],
bundle: true,
minify: true,
sourcemap: true,
target: ['es2020', 'chrome70', 'firefox70', 'safari12', 'edge79'],
plugins: [],
logLevel: 'info',
};

// Build ESM module
esbuild
.build({
...commonSettings,
outfile: 'dist/index.esm.js',
format: 'esm',
})
.catch(() => process.exit(1));

// Build CommonJS module
esbuild
.build({
...commonSettings,
outfile: 'dist/index.js',
format: 'cjs',
})
.catch(() => process.exit(1));

// Build UMD/IIFE bundle for direct browser usage
esbuild
.build({
...commonSettings,
outfile: 'dist/index.umd.js',
format: 'iife',
globalName: 'MyPackage',
// Remove external modules handling for IIFE build
plugins: [],
})
.catch(() => process.exit(1));
35 changes: 35 additions & 0 deletions packages/adaptive/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "@gitbook/adaptive",
"description": "Utility to use adaptive content in GitBook",
"type": "module",
"version": "0.0.1",
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"files": [
"README.md",
"dist/**"
],
"dependencies": {
"js-cookie": "^3.0.5"
},
"devDependencies": {
"@gitbook/tsconfig": "*",
"typescript": "^5.6.2",
"@bucketco/browser-sdk": "^3.1.2",
"@types/js-cookie": "^3.0.6",
"esbuild": "^0.25.3"
},
"scripts": {
"build": "bun esbuild.config.js && tsc --emitDeclarationOnly",
"typecheck": "tsc --noEmit"
},
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
}
}
53 changes: 53 additions & 0 deletions packages/adaptive/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { BucketClient } from '@bucketco/browser-sdk';
import Cookies from 'js-cookie';

const COOKIE_NAME = 'gitbook-visitor-public-bucket';

/**
* Sets a client-side session cookie with the features enabled in the bucket client.
*/
export function withBucket(client: BucketClient): () => void {
const handler = () => {
const features: Record<string, boolean> = {};
// Check features enabled in the client
for (const [key, value] of Object.entries(client.getFeatures())) {
const enabled =
typeof value.isEnabledOverride === 'boolean'
? value.isEnabledOverride
: value.isEnabled;
features[key] = enabled;
}

// Set a session cookie with the features
Cookies.set(COOKIE_NAME, JSON.stringify(features), {
secure: true,
});
};

// Determine if we're in a browser environment
const isBrowser = typeof window !== 'undefined';

if (!isBrowser) {
console.warn('withBucket was called in a non-browser environment');
return () => {};
}

const cleanup: Array<() => void> = [];

// Add event listener for when the DOM is ready
if (document.readyState === 'interactive' || document.readyState === 'complete') {
handler();
} else {
document.addEventListener('DOMContentLoaded', handler);
cleanup.push(() => {
document.removeEventListener('DOMContentLoaded', handler);
});
}
// Add event listeners for feature updates
cleanup.push(client.on('featuresUpdated', handler));

// Return cleanup function
return () => {
cleanup.forEach((fn) => fn());
};
}
1 change: 1 addition & 0 deletions packages/adaptive/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './client';
12 changes: 12 additions & 0 deletions packages/adaptive/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@gitbook/tsconfig/base.json",
"include": ["src/**/*"],
"rootDir": "./src",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./src",
"allowImportingTsExtensions": false,
"noEmit": false,
"declaration": true
}
}