Skip to content

Commit

Permalink
Don't patch fetch globally, import patched function where needed inst…
Browse files Browse the repository at this point in the history
…ead.

We'll export a `fetch` function apps can import too. This can be necessary if an app,
for example, is doing a manual `fetch` and it wants to prevent a reflected broadcast
triggered by that request.

We'll also make rails/request use the Turbo fetch version when available. That will be
the preferred form. In general, we don't want apps to care about having to import or
use Turbo's `fetch`, but it will be available for the cases an app needs it.

We'll also expose the patched version via `Turbo.fetch`.

See discussion #1019 (review)
  • Loading branch information
jorgemanrubia committed Nov 6, 2023
1 parent 541679a commit a36ee14
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/core/drive/preloader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PageSnapshot } from "./page_snapshot"
import { fetch } from "../../http/fetch"

export class Preloader {
selector = "a[data-turbo-preload]"
Expand Down
3 changes: 2 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { PageRenderer } from "./drive/page_renderer"
import { PageSnapshot } from "./drive/page_snapshot"
import { FrameRenderer } from "./frames/frame_renderer"
import { FormSubmission } from "./drive/form_submission"
import { fetch } from "../http/fetch"

const session = new Session()
const { cache, navigator } = session
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer }
export { navigator, session, cache, PageRenderer, PageSnapshot, FrameRenderer, fetch }

export { StreamActions } from "./streams/stream_actions"

Expand Down
6 changes: 2 additions & 4 deletions src/http/recent_fetch_requests.js → src/http/fetch.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { uuid } from "../util"

const originalFetch = window.fetch

window.fetch = async function(url, options = {}) {
export function fetch(url, options = {}) {
const modifiedHeaders = new Headers(options.headers || {})
const requestUID = uuid()
window.Turbo.session.recentRequests.add(requestUID)
modifiedHeaders.append("X-Turbo-Request-Id", requestUID)

return originalFetch(url, {
return window.fetch(url, {
...options,
headers: modifiedHeaders
})
Expand Down
1 change: 1 addition & 0 deletions src/http/fetch_request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FetchResponse } from "./fetch_response"
import { expandURL } from "../core/url"
import { dispatch } from "../util"
import { fetch } from "./fetch"

export function fetchMethodFromString(method) {
switch (method.toLowerCase()) {
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "./polyfills"
import "./elements"
import "./script_warning"
import "./http/recent_fetch_requests"

import * as Turbo from "./core"

Expand Down
2 changes: 1 addition & 1 deletion src/tests/functional/page_refresh_stream_action_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function textContent(page) {

async function fetchRequestId(page) {
return await page.evaluate(async () => {
const response = await window.fetch("/__turbo/request_id_header")
const response = await window.Turbo.fetch("/__turbo/request_id_header")
return response.text()
})
}

0 comments on commit a36ee14

Please sign in to comment.