Skip to content

Commit 8f9631d

Browse files
Adjust bundle.mjs, add CLIENT_BUNDLE for reporting
1 parent 8bc5d92 commit 8f9631d

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

scripts/bundle.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const commonBuildOptions = {
2828
},
2929
};
3030

31+
/**
32+
* process.env.CLIENT_BUNDLE values:
33+
*
34+
* - index.js - browser-esm
35+
* - index.browser.cjs - browser-cjs
36+
* - index.node.cjs - node-cjs
37+
*/
38+
3139
// We build two CJS bundles: for browser and for node. The latter one can be
3240
// used e.g. during SSR (although it makes little sence to SSR chat, but still
3341
// nice for import not to break on server).
@@ -43,7 +51,7 @@ const bundles = [
4351
platform,
4452
define: {
4553
...commonBuildOptions.define,
46-
'process.env.BUILD': JSON.stringify(`${platform}-cjs`),
54+
'process.env.CLIENT_BUNDLE': JSON.stringify(`${platform}-cjs`),
4755
},
4856
})),
4957
// ESM (browser only)
@@ -55,7 +63,7 @@ const bundles = [
5563
platform: 'browser',
5664
define: {
5765
...commonBuildOptions.define,
58-
'process.env.BUILD': JSON.stringify(`browser-esm`),
66+
'process.env.CLIENT_BUNDLE': JSON.stringify('browser-esm'),
5967
},
6068
},
6169
]

src/client.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,12 +2923,23 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
29232923
if (this.userAgent) {
29242924
return this.userAgent;
29252925
}
2926+
29262927
const version = process.env.PKG_VERSION;
2928+
const clientBundle = process.env.CLIENT_BUNDLE;
2929+
2930+
let userAgentString = '';
29272931
if (this.sdkIdentifier) {
2928-
return `stream-chat-${this.sdkIdentifier.name}-v${this.sdkIdentifier.version}-llc-v${version}`;
2932+
userAgentString = `stream-chat-${this.sdkIdentifier.name}-v${this.sdkIdentifier.version}-llc-v${version}`;
29292933
} else {
2930-
return `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
2934+
userAgentString = `stream-chat-js-v${version}-${this.node ? 'node' : 'browser'}`;
29312935
}
2936+
2937+
const additionalOptions = ([
2938+
// reports which bundle is being picked from the exports
2939+
['client_bundle', clientBundle],
2940+
] as const).map(([key, value]) => `${key}=${value ?? ''}`);
2941+
2942+
return [userAgentString, ...additionalOptions].join('|');
29322943
}
29332944

29342945
/**

test/unit/client.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ describe('StreamChat.queryChannels', async () => {
644644

645645
describe('X-Stream-Client header', () => {
646646
process.env.PKG_VERSION = '1.2.3';
647+
process.env.CLIENT_BUNDLE = 'browser-esm';
647648
let client;
648649

649650
beforeEach(async () => {
@@ -653,21 +654,21 @@ describe('X-Stream-Client header', () => {
653654
it('server-side integration', () => {
654655
const userAgent = client.getUserAgent();
655656

656-
expect(userAgent).to.be.equal('stream-chat-js-v1.2.3-node');
657+
expect(userAgent).to.be.equal('stream-chat-js-v1.2.3-node|client_bundle=browser-esm');
657658
});
658659

659660
it('client-side integration', () => {
660661
client.node = false;
661662
const userAgent = client.getUserAgent();
662663

663-
expect(userAgent).to.be.equal('stream-chat-js-v1.2.3-browser');
664+
expect(userAgent).to.be.equal('stream-chat-js-v1.2.3-browser|client_bundle=browser-esm');
664665
});
665666

666667
it('SDK integration', () => {
667668
client.sdkIdentifier = { name: 'react', version: '2.3.4' };
668669
const userAgent = client.getUserAgent();
669670

670-
expect(userAgent).to.be.equal('stream-chat-react-v2.3.4-llc-v1.2.3');
671+
expect(userAgent).to.be.equal('stream-chat-react-v2.3.4-llc-v1.2.3|client_bundle=browser-esm');
671672
});
672673

673674
it('setUserAgent is now deprecated', () => {

0 commit comments

Comments
 (0)