rbren / rss-parser

A lightweight RSS parser, for Node and the browser
MIT License
1.38k stars 209 forks source link

Don't change User-Agent to prevent preflight request. #226

Open hamano opened 2 years ago

hamano commented 2 years ago

Hi, I found a weired issue. rss-parser does not fetch feed that is hosted github.io with Firefox, but no problem with Chrome. github.io responds Access-Control-Allow-Origin: * as expected. The cause was that Firefox was sending a CORS preflight request. There are several conditions for sending a preflight request, one of which is User-Agent changing. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#examples_of_access_control_scenarios

I think many people will be bothered by this issue, so I recommend not to set User-Agent by default.

rbren commented 2 years ago

Hmm. If I remember correctly, when running server-side, we need to add a User-Agent header for a lot of feeds to work. So this could be a breaking change.

Does passing in the option {headers: {'User-Agent': ''}} reset to using the browser's user agent?

hamano commented 2 years ago

@rbren thanks for the reply. I tryied: {headers: {'User-Agent': ''}} and {headers: {'User-Agent': null}} but they didn't works. I've put test file here, please try with Firefox: https://www.osstech.co.jp/~hamano/test.html

rbren commented 2 years ago

OK, I think this might be a better solution (untested pseudocode):

let headers = {
  'User-Agent': 'rss-parser',
}
if (typeof window !== 'undefined' && window.navigator && window.navigator.userAgent) {
  headers['User-Agent'] = window.navigator.userAgent;
}

This way we won't create a breaking change for server-side rss-parser