Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - manual Sanity-controlled redirects #562

Merged
merged 12 commits into from
Sep 10, 2024
Merged
Prev Previous commit
Next Next commit
feat(redirect): remove support for permanent redirects
can be introduced later if needed, but omitted now because they are hard to undo later
mathiazom committed Sep 10, 2024

Verified

This commit was signed with the committer’s verified signature.
mathiazom Mathias Oterhals Myklebust
commit dc773219c134a99b510fdc323c700980704c3be9
5 changes: 2 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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]{
mathiazom marked this conversation as resolved.
Show resolved Hide resolved
"destination":destination.current,
permanent
"destination":destination.current
}`,
);
if (redirect !== null) {
return NextResponse.redirect(
new URL(redirect.destination, request.url),
redirect.permanent ? 308 : 307,
307,
);
}
}
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
@@ -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";
@@ -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",
@@ -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;
@@ -124,8 +103,7 @@ const redirect = defineType({
: undefined;
return {
title,
subtitle: permanent ? "Permanent" : "Temporary",
media: RedirectThumbnail({ permanent }),
subtitle: "type: " + destinationType,
};
},
},