From 5c73e102d7c2ad3dd32ef2dd7321f5ff23720b27 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Tue, 10 Sep 2024 14:54:02 +0200 Subject: [PATCH] feat(redirect): add external redirect type --- src/middleware.ts | 4 ++-- studio/lib/payloads/redirect.ts | 2 +- studio/lib/queries/redirects.ts | 5 ++-- studio/schemas/documents/redirect.ts | 35 +++++++++++++++++++++------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 4ccf01da1..f3f973917 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -26,9 +26,9 @@ export async function middleware(request: NextRequest) { if (redirectRes.ok) { const redirect: RedirectDestinationSlugPage | null = await redirectRes.json(); - if (redirect?.destinationSlug) { + if (redirect?.destination) { return NextResponse.redirect( - new URL(redirect.destinationSlug, request.url), + new URL(redirect.destination, request.url), HTTP_STATUSES.TEMPORARY_REDIRECT, ); } diff --git a/studio/lib/payloads/redirect.ts b/studio/lib/payloads/redirect.ts index e54442eab..51a4e58ff 100644 --- a/studio/lib/payloads/redirect.ts +++ b/studio/lib/payloads/redirect.ts @@ -1,3 +1,3 @@ export interface RedirectDestinationSlugPage { - destinationSlug: string; + destination: string; } diff --git a/studio/lib/queries/redirects.ts b/studio/lib/queries/redirects.ts index ecc0362f1..da54e49f6 100644 --- a/studio/lib/queries/redirects.ts +++ b/studio/lib/queries/redirects.ts @@ -2,9 +2,10 @@ import { groq } from "next-sanity"; export const REDIRECT_BY_SOURCE_SLUG_QUERY = groq` *[_type == "redirect" && source.current == $slug][0]{ - "destinationSlug": select( + "destination": select( destination.type == "slug" => destination.slug.current, - destination.type == "reference" => destination.reference->slug.current + destination.type == "reference" => destination.reference->slug.current, + destination.type == "external" => destination.external ) } `; diff --git a/studio/schemas/documents/redirect.ts b/studio/schemas/documents/redirect.ts index e177d2e1e..1dbd6bc56 100644 --- a/studio/schemas/documents/redirect.ts +++ b/studio/schemas/documents/redirect.ts @@ -39,7 +39,7 @@ const redirect = defineType({ defineField({ name: "source", title: "Source", - description: "Which url should this redirect apply for", + description: "The url slug to be redirected to the destination", type: "slug", validation: (rule) => slugRequired(rule), components: { @@ -54,13 +54,13 @@ const redirect = defineType({ defineField({ name: "type", title: "Type", - description: "Choose between an internal page or a static slug", type: "string", initialValue: "reference", options: { layout: "radio", list: [ { value: "reference", title: "Internal Page" }, + { value: "external", title: "External URL" }, { value: "slug", title: "Slug" }, ], }, @@ -81,6 +81,13 @@ const redirect = defineType({ requiredIfDestinationType("reference", document, value), ), }), + defineField({ + name: "external", + title: "External URL", + description: "Where should the user be redirected?", + type: "url", + hidden: ({ parent }) => parent?.type !== "external", + }), defineField({ name: "slug", title: "Slug", @@ -109,22 +116,34 @@ const redirect = defineType({ ], preview: { select: { - source: "source", + sourceSlug: "source.current", destinationType: "destination.type", destinationSlug: "destination.slug.current", destinationReferenceSlug: "destination.reference.slug.current", + destinationExternalURL: "destination.external", }, prepare({ - source, + sourceSlug, destinationType, destinationSlug, destinationReferenceSlug, + destinationExternalURL, + }: { + sourceSlug: string; + destinationType: string; + destinationSlug: string; + destinationReferenceSlug: string; + destinationExternalURL: string; }) { - const destination = - destinationType === "slug" ? destinationSlug : destinationReferenceSlug; + const destination = { + slug: `/${destinationSlug}`, + reference: `/${destinationReferenceSlug}`, + external: destinationExternalURL, + }[destinationType]; + console.log(destination); const title = - source && destination - ? `/${source.current} → /${destination}` + sourceSlug && destination + ? `/${sourceSlug} → ${destination}` : undefined; return { title,