diff --git a/README.md b/README.md index 874e1d0..2207a55 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,10 @@ Specifies which response format will be accepted. Default is `html`. Options are `html`, `turbo-stream`, `json`, and `script`. +##### keepalive + +Specifies the `keepalive` option. Default is `false`. + #### Turbo Streams Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the `window.Turbo` global variable: diff --git a/__tests__/fetch_request.js b/__tests__/fetch_request.js index 1a2f82e..01130c9 100644 --- a/__tests__/fetch_request.js +++ b/__tests__/fetch_request.js @@ -221,6 +221,15 @@ describe('header handling', () => { expect(request.fetchOptions.credentials).toBe('include') }) + test('has keepalive setting which can be changed', () => { + let request + request = new FetchRequest("get", "localhost") + expect(request.fetchOptions.keepalive).toBe(false) + + request = new FetchRequest("get", "localhost", { keepalive: true}) + expect(request.fetchOptions.keepalive).toBe(true) + }) + describe('csrf token inclusion', () => { // window.location.hostname is "localhost" in the test suite test('csrf token is not included in headers if url hostname is not the same as window.location (http)', () => { diff --git a/src/fetch_request.js b/src/fetch_request.js index 11a78bf..9e8c2e5 100644 --- a/src/fetch_request.js +++ b/src/fetch_request.js @@ -67,7 +67,8 @@ export class FetchRequest { body: this.formattedBody, signal: this.signal, credentials: this.credentials, - redirect: this.redirect + redirect: this.redirect, + keepalive: this.keepalive } } @@ -161,6 +162,10 @@ export class FetchRequest { return this.options.credentials || 'same-origin' } + get keepalive () { + return this.options.keepalive || false + } + get additionalHeaders () { return this.options.headers || {} }