rss-parser icon indicating copy to clipboard operation
rss-parser copied to clipboard

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

Open hamano opened this issue 3 years ago • 3 comments

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.

hamano avatar Jun 09 '22 10:06 hamano

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?

rbren avatar Jun 15 '22 14:06 rbren

@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

hamano avatar Jun 22 '22 01:06 hamano

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

rbren avatar Jun 22 '22 13:06 rbren