-
Notifications
You must be signed in to change notification settings - Fork 4
Arweave Fallback #2024
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
Closed
Closed
Arweave Fallback #2024
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect } from "react"; | ||
|
|
||
| const SW_PATH = "/arweave-fallback-sw.js"; | ||
|
|
||
| export default function ArweaveFallbackSwRegistration() { | ||
| useEffect(() => { | ||
| if (typeof navigator === "undefined" || !("serviceWorker" in navigator)) { | ||
| return; | ||
| } | ||
| navigator.serviceWorker | ||
| .register(SW_PATH, { scope: "/" }) | ||
| .catch((e) => { | ||
| if (process.env.NODE_ENV !== "production") { | ||
| console.error(`[ArweaveFallbackSW] Failed to register ${SW_PATH}:`, e); | ||
| } | ||
| }); | ||
| }, []); | ||
| return null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| export const ARWEAVE_HOST = "arweave.net"; | ||
| export const FALLBACK_HOST = "ar-io.net"; | ||
|
|
||
| export function isArweaveUrl(url: string): boolean { | ||
| try { | ||
| const u = new URL(url); | ||
| const h = u.hostname.toLowerCase(); | ||
| return h === ARWEAVE_HOST || h.endsWith("." + ARWEAVE_HOST); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| export function getArweaveFallbackUrl(url: string): string | null { | ||
| if (!isArweaveUrl(url)) return null; | ||
| try { | ||
| const u = new URL(url); | ||
| u.host = FALLBACK_HOST + (u.port ? ":" + u.port : ""); | ||
| return u.toString(); | ||
| } catch { | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| self.addEventListener("fetch", (event) => { | ||
| const url = new URL(event.request.url); | ||
|
|
||
| if (url.hostname !== "arweave.net") return; | ||
| if (event.request.mode === "navigate") return; | ||
|
|
||
| // Safety: only handle normal asset GETs | ||
| if (event.request.method !== "GET") return; | ||
| if (event.request.headers.has("range")) return; | ||
|
|
||
| event.respondWith(handleRequest(event.request)); | ||
| }); | ||
|
|
||
| async function handleRequest(request) { | ||
| try { | ||
| const response = await fetch(request); | ||
|
|
||
| if (response && (response.type === "opaque" || response.ok)) { | ||
| return response; | ||
| } | ||
|
|
||
| return fetchFallback(request); | ||
| } catch (err) { | ||
| return fetchFallback(request); | ||
| } | ||
| } | ||
|
|
||
| function fetchFallback(request) { | ||
| const originalUrl = new URL(request.url); | ||
| const fallbackUrl = | ||
| "https://ar-io.net" + | ||
| originalUrl.pathname + | ||
| originalUrl.search + | ||
| originalUrl.hash; | ||
|
|
||
| return fetch(fallbackUrl); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Fetch API default request properties mode credentials when calling fetch(url) vs original request semantics💡 Result:
When you call
fetch(url)with a string URL, the Fetch spec creates a newRequestusing default “request semantics” for anyRequestInitfields you didn’t provide:method:GET(and no request body)mode:"cors"(so cross-origin requests use CORS; same-origin also works) [1][2]credentials:"same-origin"(send cookies/auth headers only for same-origin requests; not for cross-origin) [1][3]redirect: "follow",cache: "default".) [1]In contrast, when you call
fetch(request)with an existingRequestobject, it uses that object’s already-set properties (includingmode,credentials, etc.), unless you explicitly override them in the second argument (which “clones” with overrides per the Fetch algorithms). The key difference is: string URL ⇒ defaults apply; Request object ⇒ its stored semantics apply. [1][3]Sources:
[1] WHATWG Fetch Standard (request creation defaults for
mode/credentials) (fetch.spec.whatwg.org)[2] MDN: Using the Fetch API (default
modeiscors) (developer.mozilla.org)[3] MDN:
Request.credentials(default issame-origin) (developer.mozilla.org)Citations:
🏁 Script executed:
Repository: 6529-Collections/6529seize-frontend
Length of output: 1232
Preserve request mode/semantics when calling the fallback host.
On Line 36,
fetch(fallbackUrl)creates a new request with default fetch options (specificallymode: "cors"andcredentials: "same-origin"). This can break cross-origin asset requests that were originally made withno-corsmode, since the fallback request will now attempt standard CORS negotiation instead of requesting an opaque response. If ar-io.net lacks the appropriate CORS headers, the fallback will fail.🔧 Proposed fix
function fetchFallback(request) { const originalUrl = new URL(request.url); const fallbackUrl = "https://ar-io.net" + originalUrl.pathname + originalUrl.search + originalUrl.hash; - return fetch(fallbackUrl); + return fetch(fallbackUrl, { + method: request.method, + mode: request.mode, + cache: request.cache, + redirect: request.redirect, + referrer: request.referrer, + referrerPolicy: request.referrerPolicy, + integrity: request.integrity, + credentials: "omit", + }); }🤖 Prompt for AI Agents