Skip to content

Instantly share code, notes, and snippets.

@benjaminsehl
Last active June 8, 2023 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminsehl/1c169edcce5b8ef6144156968a2edf16 to your computer and use it in GitHub Desktop.
Save benjaminsehl/1c169edcce5b8ef6144156968a2edf16 to your computer and use it in GitHub Desktop.
Hydrogen Admin Redirects
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