Skip to content
Merged
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ async myMethod () {
}
```

#### Fetch Priority

You can use the [`priority`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) option to hint the browser on the relative importance of a request

```js
import { get, post } from '@rails/request.js'

// High priority fetch (e.g. critical data above the fold)
const response = await get('/api/critical-data', { priority: 'high', responseKind: 'json' })

// Low priority fetch (e.g. prefetch or analytics)
await post('/api/analytics', { body: payload, priority: 'low' })
```

#### Request Options

You can pass options to a request as the last argument. For example:
Expand Down Expand Up @@ -109,6 +123,10 @@ Options are `html`, `turbo-stream`, `json`, and `script`.

Specifies the `keepalive` option. Default is `false`.

##### priority

Specifies the [fetch priority](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) of the request relative to other requests of the same type. Must be one of `"high"`, `"low"`, or `"auto"` (default browser behavior when not specified).

#### Turbo Streams

Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the `window.Turbo` global variable:
Expand Down
15 changes: 15 additions & 0 deletions __tests__/fetch_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ describe('header handling', () => {
expect(request.fetchOptions.keepalive).toBe(true)
})

test('sets priority', () => {
let request
request = new FetchRequest("get", "localhost")
expect(request.fetchOptions.priority).toBe(undefined)

request = new FetchRequest("get", "localhost", { priority: "high" })
expect(request.fetchOptions.priority).toBe("high")

request = new FetchRequest("get", "localhost", { priority: "low" })
expect(request.fetchOptions.priority).toBe("low")

request = new FetchRequest("get", "localhost", { priority: "auto" })
expect(request.fetchOptions.priority).toBe("auto")
})

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 @@ -65,7 +65,8 @@ export class FetchRequest {
signal: this.signal,
credentials: this.credentials,
redirect: this.redirect,
keepalive: this.keepalive
keepalive: this.keepalive,
priority: this.priority
}
}

Expand Down Expand Up @@ -155,6 +156,10 @@ export class FetchRequest {
return this.options.redirect || 'follow'
}

get priority () {
return this.options.priority
}

get credentials () {
return this.options.credentials || 'same-origin'
}
Expand Down