Last active
June 8, 2023 16:52
-
-
Save benjaminsehl/1c169edcce5b8ef6144156968a2edf16 to your computer and use it in GitHub Desktop.
Hydrogen Admin Redirects
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
import {useUrl, useShopQuery} from '@shopify/hydrogen'; | |
export default function AdminRedirect({response}} { | |
const url = useUrl(); | |
if (response && response.writeHead) { | |
response.doNotStream(); | |
} | |
const {data} = useShopQuery({ | |
query: gql` | |
query redirect($pathname: String) { | |
urlRedirects(first: 1, query: $pathname) { | |
nodes { | |
target | |
} | |
} | |
} | |
`, | |
variables: { | |
pathname: url.pathname | |
}, | |
cache: CacheLong(), | |
preload: true, | |
}); | |
const {target} = data?.urlRedirects?.nodes?[0] | |
if (target) { | |
response.writeHead({status: 301}); | |
return response.redirect(target) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment