From 472f9e5cdb28552370ac227e2ebd254e3abff22a Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Mon, 14 Oct 2024 10:37:20 +0200 Subject: [PATCH 1/3] feat: remove redirect middleware --- src/middleware.ts | 5 --- src/middlewares/redirectMiddleware.ts | 46 --------------------------- 2 files changed, 51 deletions(-) delete mode 100644 src/middlewares/redirectMiddleware.ts diff --git a/src/middleware.ts b/src/middleware.ts index cd98a0682..30125bc40 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,13 +1,8 @@ import { NextRequest } from "next/server"; import { languageMiddleware } from "./middlewares/languageMiddleware"; -import { redirectMiddleware } from "./middlewares/redirectMiddleware"; export async function middleware(request: NextRequest) { - const response = await redirectMiddleware(request); - if (response !== undefined) { - return response; - } return languageMiddleware(request); } diff --git a/src/middlewares/redirectMiddleware.ts b/src/middlewares/redirectMiddleware.ts deleted file mode 100644 index 5715ca576..000000000 --- a/src/middlewares/redirectMiddleware.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { NextRequest, NextResponse } from "next/server"; - -import { readBaseUrl } from "src/app/env"; -import { HTTP_STATUSES } from "src/utils/http"; -import { RedirectDestinationSlugPage } from "studio/lib/interfaces/redirect"; -import { REDIRECT_BY_SOURCE_SLUG_QUERY } from "studio/lib/queries/redirects"; - -export async function redirectMiddleware( - request: NextRequest, -): Promise { - const baseUrlResult = readBaseUrl(); - if (!baseUrlResult.ok) { - console.error( - "Failed to run redirect middleware, missing base url:", - baseUrlResult.error, - ); - return; - } - const baseUrl = baseUrlResult.value; - const slug = request.nextUrl.pathname; - const slugQueryParam = slug.replace(/^\/+/, ""); - /* - fetching redirect data via API route to avoid token leaking to client - (middleware should run on server, but `experimental_taintUniqueValue` begs to differ...) - */ - const redirectRes = await fetch(new URL("/api/fetchData", baseUrl), { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: REDIRECT_BY_SOURCE_SLUG_QUERY, - params: { slug: slugQueryParam }, - }), - }); - if (redirectRes.ok) { - const redirect: RedirectDestinationSlugPage | null = - await redirectRes.json(); - if (redirect?.destination) { - return NextResponse.redirect( - new URL(redirect.destination, request.url), - HTTP_STATUSES.TEMPORARY_REDIRECT, - ); - } - } -} From 8faed567de3d0fc84b2c97646e79c9a7dfb1ef01 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Mon, 14 Oct 2024 10:39:15 +0200 Subject: [PATCH 2/3] refactor: remove redirect utils --- src/utils/http.ts | 3 --- studio/lib/interfaces/redirect.ts | 3 --- studio/lib/queries/redirects.ts | 10 ---------- 3 files changed, 16 deletions(-) delete mode 100644 src/utils/http.ts delete mode 100644 studio/lib/interfaces/redirect.ts delete mode 100644 studio/lib/queries/redirects.ts diff --git a/src/utils/http.ts b/src/utils/http.ts deleted file mode 100644 index e209a9d4f..000000000 --- a/src/utils/http.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const HTTP_STATUSES = { - TEMPORARY_REDIRECT: 307, -}; diff --git a/studio/lib/interfaces/redirect.ts b/studio/lib/interfaces/redirect.ts deleted file mode 100644 index 51a4e58ff..000000000 --- a/studio/lib/interfaces/redirect.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface RedirectDestinationSlugPage { - destination: string; -} diff --git a/studio/lib/queries/redirects.ts b/studio/lib/queries/redirects.ts deleted file mode 100644 index b54619985..000000000 --- a/studio/lib/queries/redirects.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { groq } from "next-sanity"; - -export const REDIRECT_BY_SOURCE_SLUG_QUERY = groq` - *[_type == "brokenLink" && source.current == $slug][0]{ - "destination": select( - destination.type == "reference" => destination.reference->slug.current, - destination.type == "external" => destination.external - ) - } -`; From b791c4537b2f773ba0c1311841b616be5292ca3c Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Mon, 14 Oct 2024 11:13:35 +0200 Subject: [PATCH 3/3] feat: remove redirects/broken links schema --- studio/deskStructure.ts | 6 - studio/schema.ts | 2 - .../documents/siteSettings/brokenLink.ts | 135 ------------------ studio/schemas/schemaTypes/slug.ts | 3 +- 4 files changed, 1 insertion(+), 145 deletions(-) delete mode 100644 studio/schemas/documents/siteSettings/brokenLink.ts diff --git a/studio/deskStructure.ts b/studio/deskStructure.ts index ac19c749e..16e40a722 100644 --- a/studio/deskStructure.ts +++ b/studio/deskStructure.ts @@ -1,7 +1,6 @@ import { CaseIcon, CogIcon, - DoubleChevronRightIcon, EarthGlobeIcon, HeartIcon, ImagesIcon, @@ -24,7 +23,6 @@ import { compensationsId } from "./schemas/documents/compensations"; import { languageSettingsID } from "./schemas/documents/languageSettings"; import { pageBuilderID } from "./schemas/documents/pageBuilder"; import { brandAssetsID } from "./schemas/documents/siteSettings/brandAssets"; -import { brokenLinkID } from "./schemas/documents/siteSettings/brokenLink"; import { localeID } from "./schemas/documents/siteSettings/locale"; import { soMeLinksID } from "./schemas/documents/siteSettings/socialMediaProfiles"; import { customerCasesPageID } from "./schemas/documents/specialPages/customerCasesPage"; @@ -122,10 +120,6 @@ const siteSettingSection = (S: StructureBuilder) => .documentId(defaultSeoID) .title("Default SEO"), ), - S.listItem() - .title("Broken Links") - .icon(DoubleChevronRightIcon) - .child(S.documentTypeList(brokenLinkID).title("Redirects")), ]), ); diff --git a/studio/schema.ts b/studio/schema.ts index 91cdf055e..099a9e4f6 100644 --- a/studio/schema.ts +++ b/studio/schema.ts @@ -8,7 +8,6 @@ import compensations from "./schemas/documents/compensations"; import languageSettings from "./schemas/documents/languageSettings"; import pageBuilder from "./schemas/documents/pageBuilder"; import brandAssets from "./schemas/documents/siteSettings/brandAssets"; -import brokenLink from "./schemas/documents/siteSettings/brokenLink"; import locale from "./schemas/documents/siteSettings/locale"; import navigationManager from "./schemas/documents/siteSettings/navigationManager"; import socialMediaLinks from "./schemas/documents/siteSettings/socialMediaProfiles"; @@ -34,7 +33,6 @@ export const schema: { types: SchemaTypeDefinition[] } = { legalDocument, compensations, customerCasesPage, - brokenLink, benefitsByLocation, companyLocation, defaultSeo, diff --git a/studio/schemas/documents/siteSettings/brokenLink.ts b/studio/schemas/documents/siteSettings/brokenLink.ts deleted file mode 100644 index 1e24cbfd3..000000000 --- a/studio/schemas/documents/siteSettings/brokenLink.ts +++ /dev/null @@ -1,135 +0,0 @@ -import { SanityDocument, SlugRule } from "@sanity/types"; -import { type Slug, defineField, defineType } from "sanity"; - -import PrefixedSlugInput from "studio/components/slug/PrefixedSlugInput"; -import { compensationsId } from "studio/schemas/documents/compensations"; -import { pageBuilderID } from "studio/schemas/documents/pageBuilder"; - -const slugRequired = (rule: SlugRule) => - rule.required().custom((value: Slug | undefined) => { - if (!value || !value.current) return "Can't be blank"; - return true; - }); - -const requiredIfDestinationType = ( - type: string, - document: SanityDocument | undefined, - value: unknown, -) => { - const destination = document?.destination; - if ( - typeof destination === "object" && - destination !== null && - "type" in destination && - destination.type === type && - value === undefined - ) { - return "Can't be blank"; - } - return true; -}; - -export const brokenLinkID = "brokenLink"; - -const brokenLink = defineType({ - name: brokenLinkID, - title: "Broken Links", - type: "document", - fields: [ - defineField({ - name: "source", - title: "Source", - description: "The url slug to be redirected to the destination", - type: "slug", - validation: (rule) => slugRequired(rule), - components: { - input: (props) => PrefixedSlugInput({ prefix: "/", ...props }), - }, - }), - defineField({ - name: "destination", - title: "Destination", - type: "object", - fields: [ - defineField({ - name: "type", - title: "Type", - type: "string", - initialValue: "reference", - options: { - layout: "radio", - list: [ - { value: "reference", title: "Internal Page" }, - { value: "external", title: "External URL" }, - ], - }, - }), - defineField({ - name: "reference", - title: "Internal Page", - description: "Where should the user be redirected?", - type: "reference", - to: [{ type: pageBuilderID }, { type: compensationsId }], - hidden: ({ parent }) => parent?.type !== "reference", - validation: (rule) => - rule.custom((value, { document }) => - requiredIfDestinationType("reference", document, value), - ), - }), - defineField({ - name: "external", - title: "External URL", - description: "Where should the user be redirected?", - type: "url", - hidden: ({ parent }) => parent?.type !== "external", - validation: (rule) => - rule.custom((value, { document }) => - requiredIfDestinationType("external", document, value), - ), - }), - ], - }), - ], - preview: { - select: { - sourceSlug: "source.current", - destinationType: "destination.type", - destinationReferenceSlug: "destination.reference.slug.current", - destinationExternalURL: "destination.external", - }, - prepare({ - sourceSlug, - destinationType, - destinationReferenceSlug, - destinationExternalURL, - }) { - if ( - typeof sourceSlug !== "string" || - typeof destinationType !== "string" || - (destinationType === "reference" && - typeof destinationReferenceSlug !== "string") || - (destinationType === "external" && - typeof destinationExternalURL !== "string") - ) { - return {}; - } - const destination = { - reference: `/${destinationReferenceSlug}`, - external: destinationExternalURL, - }[destinationType]; - const title = - sourceSlug && destination - ? `/${sourceSlug} → ${destination}` - : undefined; - return { - title, - subtitle: { - reference: "Internal", - external: "External", - }[destinationType], - }; - }, - }, -}); - -export default brokenLink; diff --git a/studio/schemas/schemaTypes/slug.ts b/studio/schemas/schemaTypes/slug.ts index 90d32814f..4a9b99fdf 100644 --- a/studio/schemas/schemaTypes/slug.ts +++ b/studio/schemas/schemaTypes/slug.ts @@ -55,8 +55,7 @@ function createSlugField(source: string) { description: "Enter a unique URL path for the page. This path will be used in the website's address bar. " + "A URL path, also known as a slug, is a URL-friendly version of the page title, used to create " + - "a human-readable and search engine optimized URL for the content. " + - "Note that the slug can not be changed after publication, but alternative slugs can be defined via Redirects.", + "a human-readable and search engine optimized URL for the content. ", options: { source, maxLength: SLUG_MAX_LENGTH,