-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ipfs-hosted redirects are not infinite (#215)
* tmp: add ipfs-gateway for hosting local dist like inbrowser.tld * chore: loading sw-gateway at localhost:3334 via ipfs host locally * chore: code cleanup * test: working on ipfs-hosted e2e test * fix: infinite redirects are handled appropriately * chore: fix e2e test servers logging * docs: updating comments for ?helia-sw redirects
- Loading branch information
Showing
11 changed files
with
292 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,16 @@ | ||
/** | ||
* If you host helia-service-worker-gateway with an IPFS gateway, the _redirects file will route some requests from | ||
* `<domain>/<wildcard-splat>` to `https://<domain>/?helia-sw=<wildcard-splat>` when they hit the server instead of | ||
* the service worker. This only occurs when the service worker is not yet registered. | ||
* | ||
* This function will check for "?helia-sw=" in the URL and modify the URL so that it works with the rest of our logic | ||
*/ | ||
export function translateIpfsRedirectUrl (urlString: string): URL { | ||
const url = new URL(urlString) | ||
const heliaSw = url.searchParams.get('helia-sw') | ||
if (heliaSw != null) { | ||
url.searchParams.delete('helia-sw') | ||
url.pathname = heliaSw | ||
} | ||
return url | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,34 +1,63 @@ | ||
import React from 'preact/compat' | ||
import React, { useEffect } from 'preact/compat' | ||
import { findOriginIsolationRedirect } from '../lib/path-or-subdomain.js' | ||
import { translateIpfsRedirectUrl } from '../lib/translate-ipfs-redirect-url.js' | ||
import RedirectPage from './redirect-page' | ||
|
||
/** | ||
* This page is only used to capture the ?helia-sw=/ip[fn]s/blah query parameter that | ||
* is used by IPFS hosted versions of the service-worker-gateway when non-existent paths are requested. | ||
* This will only redirect if the URL is for a subdomain | ||
*/ | ||
export default function RedirectsInterstitial (): React.JSX.Element { | ||
const windowLocation = translateIpfsRedirectUrl(window.location.href) | ||
if (windowLocation.href !== window.location.href) { | ||
/** | ||
* We're at a domain with ?helia-sw=, we can reload the page so the service worker will | ||
* capture the request | ||
*/ | ||
window.location.replace(windowLocation.href) | ||
} | ||
const [subdomainRedirectUrl, setSubdomainRedirectUrl] = React.useState<string | null>(null) | ||
const [isSubdomainCheckDone, setIsSubdomainCheckDone] = React.useState<boolean>(false) | ||
useEffect(() => { | ||
async function doWork (): Promise<void> { | ||
setSubdomainRedirectUrl(await findOriginIsolationRedirect(translateIpfsRedirectUrl(window.location.href))) | ||
setIsSubdomainCheckDone(true) | ||
} | ||
void doWork() | ||
}) | ||
|
||
return (<>First-hit on IPFS hosted service-worker-gateway. Reloading</>) | ||
} | ||
useEffect(() => { | ||
if (subdomainRedirectUrl != null && window.location.href !== subdomainRedirectUrl) { | ||
/** | ||
* We're at a domain with ?helia-sw=, we can reload the page so the service worker will | ||
* capture the request | ||
*/ | ||
window.location.replace(subdomainRedirectUrl) | ||
} | ||
}, [subdomainRedirectUrl]) | ||
|
||
/** | ||
* If you host helia-service-worker-gateway on an IPFS domain, the redirects file will route some requests from | ||
* `<domain>/<wildcard-splat>` to `https://<domain>/?helia-sw=<wildcard-splat>`. | ||
* | ||
* This function will check for "?helia-sw=" in the URL and modify the URL so that it works with the rest of our logic | ||
*/ | ||
function translateIpfsRedirectUrl (urlString: string): URL { | ||
const url = new URL(urlString) | ||
const heliaSw = url.searchParams.get('helia-sw') | ||
if (heliaSw != null) { | ||
url.searchParams.delete('helia-sw') | ||
url.pathname = heliaSw | ||
if (!isSubdomainCheckDone) { | ||
/** | ||
* We're waiting for the subdomain check to complete.. this will look like a FOUC (Flash Of Unstyled Content) when | ||
* the assets are quickly loaded, but are informative to users when loading of assets is slow. | ||
* | ||
* TODO: Better styling. | ||
*/ | ||
return (<>First-hit on IPFS hosted service-worker-gateway. Determining state...</>) | ||
} | ||
return url | ||
|
||
if (subdomainRedirectUrl == null) { | ||
/** | ||
* We now render the redirect page if ?helia-sw is observed and subdomain redirect is not required., but not by | ||
* conflating logic into the actual RedirectPage component. | ||
* | ||
* However, the url in the browser for this scenario will be "<domain>/?helia-sw=/ipfs/blah", and the RedirectPage | ||
* will update that URL when the user clicks "load Content" to "<domain>/ipfs/blah". | ||
*/ | ||
return <RedirectPage /> | ||
} | ||
|
||
/** | ||
* If we redirect to a subdomain, this page will not be rendered again for the requested URL. The `RedirectPage` | ||
* component will render directly. | ||
* | ||
* This page should also not render again for any subsequent unique urls because the SW is registered and would | ||
* trigger the redirect logic, which would then load RedirectPage if a "first-hit" for that subdomain. | ||
* | ||
* TODO: Better styling. | ||
*/ | ||
return <>Waiting for redirect to subdomain...</> | ||
} |
This file contains 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
Oops, something went wrong.