question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’m using contentful with the latest Angular version 10.0.5 and I have errors in the dev console.

environment.ts

...
 contentful: {
    space: 'MY_SPACE',
    accessToken: 'MY_TOKEN',
  }

contentful.service.ts

@Injectable({
  providedIn: 'root'
})
export class ContentfulService {
  private cdaClient = createClient(environment.contentful);

  constructor(private localStorageService: LocalStorageService) {
  }

  private getNewsList(query?: ContentfulQuery): Promise<Entry<ContNewsFieldItem>[]> {
    return this.cdaClient.getEntries<ContNewsFieldItem>(Object.assign({
      content_type: 'SOME_CONTENT_TYPE'
    }, query))
      .then((res: EntryCollection<ContNewsFieldItem>) => res.items);
  }
...

Each time when I call contentfulService.getNewsList({limit: 6}) I get a list of news but in the dev console I see 2 errors:

xhr.js:126 Refused to set unsafe header "user-agent"
xhr.js:126 Refused to set unsafe header "Accept-Encoding"

How can I fix it or it’s your bug?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ankhuvecommented, Feb 28, 2022

Experiencing the same issue with v9.1.12. I dug into the stack trace and found out that the reason I’m getting this message is because of Contentful’s isNode function:

function isNode() {
  /**
   * Polyfills of 'process' might set process.browser === true
   *
   * See:
   * https://github.com/webpack/node-libs-browser/blob/master/mock/process.js#L8
   * https://github.com/defunctzombie/node-process/blob/master/browser.js#L156
   **/
  return typeof process !== 'undefined' && !process.browser;
}

When this function evaluates to true, the user-agent and Accept-Enconding headers are set.

Normally, this shouldn’t be a problem, however in edge cases you might have your client side JS polyfilled to use node-only packages. For my case, it’s because we’re using aws-amplify which unfortunately requires node packages. If using a bundler like Vite which doesn’t automatically polyfill these things, you have to add the process global variable into your code, for instance with something like:

<script>
    // Need this for aws-amplify to not throw errors
    // https://github.com/aws-amplify/amplify-js/issues/7499#issuecomment-804386820
    const isBrowser = () => typeof window !== 'undefined';
    const isGlobal = () => typeof global !== 'undefined';
    var exports = {};
    if (!isGlobal() && isBrowser()) {
        var global = window;
        var process = process || {
            env: { DEBUG: undefined },
            version: [],
            // browser property added because Contentful thinks it's running in a node environment otherwise
            browser: true,
        };
    }
</script>

The issue was solved on my end by adding the browser: true line, since the isNode function looks for it in order to decide if it’s running in a browser or node.

Hopefully this helps someone, and perhaps it’s a good thing to put into the docs for cases where you’re using polyfilled client side code.

3reactions
robertsinecommented, Oct 28, 2021

Updating contentful to latest version didn’t help. Also seeing these errors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to open the browser console to view errors
How to open the browser console to view errors ; Browser Console · : - Ctrl + Shift + J (Windows/Linux) - Command...
Read more >
Fix JavaScript errors that are reported in the Console
Open the demo webpage JavaScript error reported in the Console tool in a new window or tab. · Right-click anywhere in the webpage...
Read more >
JavaScript console.error() Method - W3Schools
The error() method writes an error message to the console. The console is useful for testing purposes. Syntax. console.error(message). Parameters ...
Read more >
Debugging Browser Console Errors - Delicious Brains
Open the Chrome developer tools (F12 / ⌘-Option-I) · Click the Console tab · Click the Clear console icon to clear the current...
Read more >
Browser errors were logged to the console - Chrome Developers
# How the Lighthouse browser error audit fails. Lighthouse flags all browser errors logged to the console: Lighthouse audit showing browser ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found