From b8aa4f802ae746d8d1d197215dd6010f8da28e34 Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Fri, 18 Aug 2023 20:39:46 +0300 Subject: [PATCH] Do not retry non-GET requests by default --- src/fetch.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 162ea45..4378cd6 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -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; @@ -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; @@ -54,6 +56,7 @@ export interface FetchEventSourceInit extends RequestInit { } export function fetchEventSource(input: RequestInfo, { + method: str, signal: inputSignal, headers: inputHeaders, onopen: inputOnOpen, @@ -65,6 +68,16 @@ export function fetchEventSource(input: RequestInfo, { ...rest }: FetchEventSourceInit) { return new Promise((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) {