diff --git a/src/core/drive/preloader.js b/src/core/drive/preloader.js index b971cea19..62e0825f6 100644 --- a/src/core/drive/preloader.js +++ b/src/core/drive/preloader.js @@ -1,4 +1,5 @@ import { PageSnapshot } from "./page_snapshot" +import { fetch } from "../../http/fetch" export class Preloader { selector = "a[data-turbo-preload]" diff --git a/src/core/index.js b/src/core/index.js index 440d67302..b2ada177e 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -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" diff --git a/src/http/recent_fetch_requests.js b/src/http/fetch.js similarity index 71% rename from src/http/recent_fetch_requests.js rename to src/http/fetch.js index 36f878ea8..c82ea081e 100644 --- a/src/http/recent_fetch_requests.js +++ b/src/http/fetch.js @@ -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 }) diff --git a/src/http/fetch_request.js b/src/http/fetch_request.js index 9b61b8b49..3d79f24ae 100644 --- a/src/http/fetch_request.js +++ b/src/http/fetch_request.js @@ -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()) { diff --git a/src/index.js b/src/index.js index b76e23871..07e3e097d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,6 @@ import "./polyfills" import "./elements" import "./script_warning" -import "./http/recent_fetch_requests" import * as Turbo from "./core" diff --git a/src/tests/functional/page_refresh_stream_action_tests.js b/src/tests/functional/page_refresh_stream_action_tests.js index c3c5b869c..cbdeb272a 100644 --- a/src/tests/functional/page_refresh_stream_action_tests.js +++ b/src/tests/functional/page_refresh_stream_action_tests.js @@ -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() }) }