omrilotan / isbot

🤖/👨‍🦰 Detect bots/crawlers/spiders using the user agent string
https://isbot.js.org/
The Unlicense
905 stars 74 forks source link

Calling method isbot() return isbot_namespaceObject is not a function #174

Closed adnanebrahimi closed 2 years ago

adnanebrahimi commented 2 years ago

Steps to reproduce

npm install isbot --save

in expressjs:

server.get('*', (req, res) => {
    if (isbot(req.header('User-Agent'))) {
      console.log('bot and running on SSR');
    res.render(indexHtml, {
      req,
      providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }, provideLocation(req), provideUserAgent(req)],
    });
   } else {
      console.log('No SSR');
      // No SSR
      // return index.html without pre-rendering
      // app will get rendered on the client
      res.sendFile(path.join(__dirname, '../browser/index.html'));

    }
  });

Expected behaviour

by browser loads webapp as CSR

by bot loads webapp as SSR

Actual behaviour

error occured:

TypeError: isbot_namespaceObject is not a function

Additional details

Angular Universal Version 13

omrilotan commented 2 years ago

Thank you, Adnan. Will you be able to send a dependency and deploy pipeline to reproduce? I'll try to reproduce and resolve this issue promptly

adnanebrahimi commented 2 years ago

I fixed by changing an option in tsconfig to have correct import: in tsconfig.json: "allowSyntheticDefaultImports": true

in server.ts: import isbot from 'isbot';

the wrong import was: import * as isbot from 'isbot';

Thanks for your fast reply 👌

omrilotan commented 2 years ago

I see, thank you for responding. We do export default for historic reasons, I can see how this may be confusing.