algoliasearch-client-javascript
algoliasearch-client-javascript copied to clipboard
Bug: process variable is using with some modules what makes them non cross-platform
For example when I do in webpack:
import { createNodeHttpRequester } from '@algolia/requester-node-http';
It falls in the browser with the error:
Uncaught ReferenceError: process is not defined
And navigates to this code:
/*<replacement>*/
var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : pna.nextTick;
/*</replacement>*/
Possible to check typeof process !== 'undefined' in this place? Thanks
why are you trying to use the node http requester in the browser instead of the browserXhrRequester? Could you clarify which code causes this import?
why are you trying to use the node http requester in the browser instead of the browserXhrRequester? Could you clarify which code causes this import?
@Haroenv we need to monitor all the requests and we have a custom logging system for it. So we have to extend the requester here.
Something like this
const CLIENT = algoliasearch(config.algolia.applicationId, config.algolia.apiKey, {
/** caches Promises with the same request payload */
requestsCache: customAlgoliaCache,
requester: (() => {
const requester = Object.create(typeof window !== 'undefined' ? createBrowserXhrRequester() : createNodeHttpRequester());
requester.send = (request) => {
const stopLogRequest = logRequest({
url: request.path,
method: request.method,
queryName: request.keyAsString,
queryType: 'AlgoliaFetch',
fetcher: typeof window !== 'undefined' ? 'node-fetch' : 'fetch',
});
return Object.getPrototypeOf(requester).send(request).then((result) => {
stopLogRequest({ statusCode: result.statusCode });
return result;
});
};
return requester;
})(),
});
Options:
- use fetch instead xhr/request and pass the fetch function inside the initialization, (can be node-fetch)
- add opportunity to user, to extend the fetch functions with the custom callbacks
Temporary workarounds:
- use custom fetcher
- start request log in the custom requestsCache cache set and measure it in the custom responsesCache cache set
That's very odd, you're already only using the node requester on node, how come that would need to use fetch? I don't understand why this code would even be evaluated on the browser. since typeof window !== 'undefined' is essentially static per bundle, I don't see why the other branch would not be dead code (eliminated). Do you have this code in a full GitHub project so it can be easier investigated what's causing the node requester to be present in the browser still?
Alternatively, you can of course make a custom requester (looking at either the node or the browser one as reference) where you can use an isomorphic function like fetch/node-fetch, but with your setup, I feel like you're still going to have problems, as the node code should never be executed on the browser