Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not retry non-GET requests by default #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface FetchEventSourceInit extends RequestInit {
/**
* Called when a response finishes. If you don't expect the server to kill
* the connection, you can throw an exception here and retry using onerror.
*
* For GET requests, default is retry.
*/
onclose?: () => void;

Expand All @@ -44,8 +46,8 @@ export interface FetchEventSourceInit extends RequestInit {

/**
* If true, will keep the request open even if the document is hidden.
* By default, fetchEventSource will close the request and reopen it
* automatically when the document becomes visible again.
* By default for GET requests, fetchEventSource will close the request and
* reopen it automatically when the document becomes visible again.
*/
openWhenHidden?: boolean;

Expand All @@ -54,6 +56,7 @@ export interface FetchEventSourceInit extends RequestInit {
}

export function fetchEventSource(input: RequestInfo, {
method: str,
signal: inputSignal,
headers: inputHeaders,
onopen: inputOnOpen,
Expand All @@ -65,6 +68,16 @@ export function fetchEventSource(input: RequestInfo, {
...rest
}: FetchEventSourceInit) {
return new Promise<void>((resolve, reject) => {
if (method != null && method !== 'GET') {
// non GET requests modify server state, don't reset them
// automatically
if (onerror == null) {
onerror = (e) => { throw e; }
}
if (openWhenHidden == null) {
openWhenHidden = true;
}
}
// make a copy of the input headers since we may modify it below:
const headers = { ...inputHeaders };
if (!headers.accept) {
Expand Down