From 6dc459c2f2844ccfd81673d5d2891c96482e4cff Mon Sep 17 00:00:00 2001 From: Robin Gersabeck <122107909+robinizea@users.noreply.github.com> Date: Wed, 4 Mar 2026 12:07:00 -0500 Subject: [PATCH] Allow passing through 'priority' on requests --- README.md | 18 ++++++++++++++++++ __tests__/fetch_request.js | 15 +++++++++++++++ src/fetch_request.js | 7 ++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2207a55..c461743 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: diff --git a/__tests__/fetch_request.js b/__tests__/fetch_request.js index 2c8d22c..3908c4b 100644 --- a/__tests__/fetch_request.js +++ b/__tests__/fetch_request.js @@ -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)', () => { diff --git a/src/fetch_request.js b/src/fetch_request.js index 8aae768..89393d5 100644 --- a/src/fetch_request.js +++ b/src/fetch_request.js @@ -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 } } @@ -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' }