-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Sanity-controlled redirects with Next.js middleware
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { client } from "../studio/lib/client"; | ||
import { RedirectSparsePage } from '../studio/lib/payloads/redirect'; | ||
|
||
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 | ||
}` | ||
); | ||
if (redirect !== null) { | ||
return NextResponse.redirect( | ||
new URL(redirect.destination, request.url), | ||
redirect.permanent ? 308 : 307, | ||
); | ||
} | ||
} | ||
|
||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - api (API routes) | ||
* - _next/static (static files) | ||
* - _next/image (image optimization files) | ||
* - favicon.ico, sitemap.xml, robots.txt (metadata files) | ||
*/ | ||
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)', | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const RedirectThumbnail = ({ permanent }: { permanent: boolean }) => { | ||
return <span style={{ fontSize: "1.5rem" }}>{permanent ? "⮕" : "⇢"}</span>; | ||
}; | ||
|
||
export default RedirectThumbnail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface RedirectPage extends RedirectSparsePage { | ||
source: string; | ||
} | ||
|
||
export interface RedirectSparsePage { | ||
destination: string; | ||
permanent: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { defineField, defineType, type Slug } from "sanity"; | ||
import { SlugRule } from "@sanity/types"; | ||
import RedirectThumbnail from "../../components/RedirectThumbnail"; | ||
|
||
const slugValidator = (rule: SlugRule) => | ||
rule.required().custom((value: Slug | undefined) => { | ||
if (!value || !value.current) return "Can't be blank"; | ||
if (!value.current.startsWith("/")) { | ||
return "The path must start with a forward slash ('/')"; | ||
} | ||
return true; | ||
}); | ||
|
||
export const redirectId = "redirect"; | ||
|
||
export 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", | ||
description: "Which url should this redirect apply for", | ||
type: "slug", | ||
validation: slugValidator, | ||
}), | ||
defineField({ | ||
name: "destination", | ||
description: "Where should the user be redirected?", | ||
type: "slug", | ||
validation: slugValidator, | ||
options: { | ||
isUnique: () => { | ||
/* | ||
does not need to be unique since multiple source paths | ||
can point to the same destination path | ||
*/ | ||
return true; | ||
}, | ||
}, | ||
}), | ||
defineField({ | ||
name: "permanent", | ||
description: | ||
"Will this redirect exist throughout the foreseeable future?", | ||
type: "boolean", | ||
}), | ||
], | ||
initialValue: { | ||
permanent: false, | ||
}, | ||
preview: { | ||
select: { | ||
source: "source", | ||
destination: "destination", | ||
permanent: "permanent", | ||
}, | ||
prepare({ source, destination, permanent }) { | ||
const title = | ||
source && destination | ||
? `${source.current} → ${destination.current}` | ||
: undefined; | ||
return { | ||
title, | ||
subtitle: permanent ? "Permanent" : "Temporary", | ||
media: RedirectThumbnail({ permanent }), | ||
}; | ||
}, | ||
}, | ||
}); |