Skip to content

Commit

Permalink
feat(redirect): add external redirect type
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 10, 2024
1 parent e458c10 commit 5c73e10
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
2 changes: 1 addition & 1 deletion studio/lib/payloads/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface RedirectDestinationSlugPage {
destinationSlug: string;
destination: string;
}
5 changes: 3 additions & 2 deletions studio/lib/queries/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
`;
35 changes: 27 additions & 8 deletions studio/schemas/documents/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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" },
],
},
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5c73e10

Please sign in to comment.