Skip to content

Commit

Permalink
feat(redirect): remove support for permanent redirects
Browse files Browse the repository at this point in the history
can be introduced later if needed, but omitted now because they are hard to undo later
  • Loading branch information
mathiazom committed Sep 10, 2024
1 parent eb3103c commit dc77321
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 32 deletions.
5 changes: 2 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ export async function middleware(request: NextRequest) {
const slug = request.nextUrl.pathname;
const redirect = await client.fetch<RedirectSparsePage | null>(
`*[_type == "redirect" && source.current == "${slug}"][0]{
"destination":destination.current,
permanent
"destination":destination.current
}`,
);
if (redirect !== null) {
return NextResponse.redirect(
new URL(redirect.destination, request.url),
redirect.permanent ? 308 : 307,
307,
);
}
}
Expand Down
5 changes: 0 additions & 5 deletions studio/components/RedirectThumbnail.tsx

This file was deleted.

1 change: 0 additions & 1 deletion studio/lib/payloads/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export interface RedirectPage extends RedirectSparsePage {

export interface RedirectSparsePage {
destination: string;
permanent: boolean;
}
24 changes: 1 addition & 23 deletions studio/schemas/documents/redirect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineField, defineType, type Slug } from "sanity";
import { SlugRule } from "@sanity/types";
import RedirectThumbnail from "../../components/RedirectThumbnail";
import { pageBuilderID } from "../builders/pageBuilder";
import { blogId } from "./blog";
import { compensationsId } from "./compensations";
Expand All @@ -18,16 +17,6 @@ const redirect = defineType({
name: redirectId,
title: "Redirect",
type: "document",
readOnly: (ctx) => {
/*
make permanent redirects read-only after initial publish
this is a soft guardrail that is possible to bypass
*/
return (
(ctx.document?.permanent ?? false) &&
!ctx.document?._id.startsWith("drafts.")
);
},
fields: [
defineField({
name: "source",
Expand Down Expand Up @@ -92,29 +81,19 @@ const redirect = defineType({
}),
],
}),
defineField({
name: "permanent",
title: "Permanent",
description:
"Will this redirect exist throughout the foreseeable future?",
type: "boolean",
initialValue: false,
}),
],
preview: {
select: {
source: "source",
destinationType: "destination.type",
destinationSlug: "destination.slug.current",
destinationReferenceSlug: "destination.reference.slug.current",
permanent: "permanent",
},
prepare({
source,
destinationType,
destinationSlug,
destinationReferenceSlug,
permanent,
}) {
const destination =
destinationType === "slug" ? destinationSlug : destinationReferenceSlug;
Expand All @@ -124,8 +103,7 @@ const redirect = defineType({
: undefined;
return {
title,
subtitle: permanent ? "Permanent" : "Temporary",
media: RedirectThumbnail({ permanent }),
subtitle: "type: " + destinationType,
};
},
},
Expand Down

0 comments on commit dc77321

Please sign in to comment.