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

Add the ability to pass through the keepalive configuration option #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions __tests__/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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 || {}
}
Expand Down
Loading