From 242665bd02a141bf78fdbf779cb177a683ba429e Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sat, 6 Apr 2024 03:39:05 -0700 Subject: [PATCH] Oh wow, don't use the images.neopets.com asset proxy anymore either! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Huh, I was writing up an API inventory doc to send to TNT, and was gonna explain why we proxy these assets… but turns out we don't need to anymore! Nice! This is a bit fragile if they ever change their headers, so I'll mention that in the doc, but for now, yeah sure let's save the planet some computational effort! --- src/app/util.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/app/util.js b/src/app/util.js index 518184d..d0f05fb 100644 --- a/src/app/util.js +++ b/src/app/util.js @@ -117,7 +117,7 @@ export function useCommonStyles() { */ export function safeImageUrl( urlString, - { crossOrigin = null, preferArchive = false } = {} + { crossOrigin = null, preferArchive = false } = {}, ) { if (urlString == null) { return urlString; @@ -133,13 +133,13 @@ export function safeImageUrl( // So, we provide "http://images.neopets.com" as the base URL when // parsing. Most URLs are absolute and will ignore it, but relative URLs // will resolve relative to that base. - "http://images.neopets.com" + "http://images.neopets.com", ); } catch (e) { logAndCapture( new Error( - `safeImageUrl could not parse URL: ${urlString}. Returning a placeholder.` - ) + `safeImageUrl could not parse URL: ${urlString}. Returning a placeholder.`, + ), ); return "https://impress-2020.openneo.net/__error__URL-was-not-parseable__"; } @@ -154,12 +154,16 @@ export function safeImageUrl( if (preferArchive) { const archiveUrl = new URL( `/api/readFromArchive`, - window.location.origin + window.location.origin, ); archiveUrl.search = new URLSearchParams({ url: url.toString() }); url = archiveUrl; } else if (crossOrigin) { - url.host = "images.neopets-asset-proxy.openneo.net"; + // NOTE: Previously we would rewrite this to our proxy that adds an + // `Access-Control-Allow-Origin` header (images.neopets-asset-proxy. + // openneo.net), but images.neopets.com now includes this header for us! + // + // So, do nothing! } } else if ( url.origin === "http://pets.neopets.com" || @@ -175,8 +179,8 @@ export function safeImageUrl( logAndCapture( new Error( `safeImageUrl was provided an unsafe URL, but we don't know how to ` + - `upgrade it to HTTPS: ${urlString}. Returning a placeholder.` - ) + `upgrade it to HTTPS: ${urlString}. Returning a placeholder.`, + ), ); return "https://impress-2020.openneo.net/__error__URL-was-not-HTTPS__"; } @@ -197,11 +201,11 @@ export function safeImageUrl( export function useDebounce( value, delay, - { waitForFirstPause = false, initialValue = null, forceReset = null } = {} + { waitForFirstPause = false, initialValue = null, forceReset = null } = {}, ) { // State and setters for debounced value const [debouncedValue, setDebouncedValue] = React.useState( - waitForFirstPause ? initialValue : value + waitForFirstPause ? initialValue : value, ); React.useEffect( @@ -218,7 +222,7 @@ export function useDebounce( clearTimeout(handler); }; }, - [value, delay] // Only re-call effect if value or delay changes + [value, delay], // Only re-call effect if value or delay changes ); // The `forceReset` option helps us decide whether to set the value @@ -322,7 +326,7 @@ export function useLocalStorage(key, initialValue) { console.error(error); } }, - [key] + [key], ); const reloadValue = React.useCallback(() => { @@ -349,7 +353,7 @@ export function useLocalStorage(key, initialValue) { export function loadImage( rawSrc, - { crossOrigin = null, preferArchive = false } = {} + { crossOrigin = null, preferArchive = false } = {}, ) { const src = safeImageUrl(rawSrc, { crossOrigin, preferArchive }); const image = new Image(); @@ -403,7 +407,7 @@ export function loadable(load, options) { // Return a component that renders nothing, while we reload! return () => null; }), - options + options, ); }