Skip to content

Commit 37ff677

Browse files
committed
Add the ability to pass through the keepalive configuration option
1 parent ade077a commit 37ff677

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ Specifies which response format will be accepted. Default is `html`.
105105

106106
Options are `html`, `turbo-stream`, `json`, and `script`.
107107

108+
##### keepalive
109+
110+
Specifies the `keepalive` option. Default is `false`.
111+
108112
#### Turbo Streams
109113

110114
Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the `window.Turbo` global variable:

__tests__/fetch_request.js

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ describe('header handling', () => {
221221
expect(request.fetchOptions.credentials).toBe('include')
222222
})
223223

224+
test('has keepalive setting which can be changed', () => {
225+
let request
226+
request = new FetchRequest("get", "localhost")
227+
expect(request.fetchOptions.keepalive).toBe(false)
228+
229+
request = new FetchRequest("get", "localhost", { keepalive: true})
230+
expect(request.fetchOptions.keepalive).toBe(true)
231+
})
232+
224233
describe('csrf token inclusion', () => {
225234
// window.location.hostname is "localhost" in the test suite
226235
test('csrf token is not included in headers if url hostname is not the same as window.location (http)', () => {

src/fetch_request.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ export class FetchRequest {
6767
body: this.formattedBody,
6868
signal: this.signal,
6969
credentials: this.credentials,
70-
redirect: this.redirect
70+
redirect: this.redirect,
71+
keepalive: this.keepalive
7172
}
7273
}
7374

@@ -161,6 +162,10 @@ export class FetchRequest {
161162
return this.options.credentials || 'same-origin'
162163
}
163164

165+
get keepalive () {
166+
return this.options.keepalive || false
167+
}
168+
164169
get additionalHeaders () {
165170
return this.options.headers || {}
166171
}

0 commit comments

Comments
 (0)