Skip to content

Commit

Permalink
✨ Check archived slugs before fetching azure #1128
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolucchesi committed Jun 17, 2022
1 parent a136a5c commit 5552ed8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 5 additions & 4 deletions studio/schemas/objects/titleAndMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TextArea, Label, Box } from '@sanity/ui'
// eslint-disable-next-line import/no-unresolved
import PatchEvent, { set } from 'part:@sanity/form-builder/patch-event'
import { SchemaType } from '../../types'
import { Rule } from '@sanity/types'
//const createPatchFrom = (value) => PatchEvent.from(value === '' ? unset() : set(value))

type TextAreaWithCharsProps = {
Expand All @@ -21,8 +22,8 @@ const TextAreaWithChars = forwardRef<HTMLTextAreaElement, TextAreaWithCharsProps
const length = value.split('').length

const handleChange = useCallback(
(event) => {
onChange(PatchEvent.from(set(event.target.value)))
(event: React.FormEvent<HTMLTextAreaElement>) => {
onChange(PatchEvent.from(set((event.target as HTMLTextAreaElement).value)))
},
[onChange],
)
Expand All @@ -44,7 +45,7 @@ export default {
title: 'Fields for title and description meta',
name: 'titleAndMeta',
type: 'object',
validation: (Rule: any) => [Rule.required().warning(`Please pay attention to SEO`)],
validation: (Rule: Rule) => [Rule.required().warning('Please pay attention to SEO')],
fields: [
{
name: 'documentTitle',
Expand All @@ -60,7 +61,7 @@ export default {
{
name: 'metaDescription',
title: 'Meta description',
validation: (Rule: any) => [
validation: (Rule: Rule) => [
Rule.required().warning('Meta description is important for SEO'),
Rule.max(160).warning('Google recommends max. 160 chars'),
],
Expand Down
7 changes: 6 additions & 1 deletion web/pages/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NextRequest, NextResponse } from 'next/server'
import { getLocaleFromName } from '../lib/localization'
import { hasArchivedNews, isGlobal } from '../common/helpers/datasetHelpers'
import { getDocumentBySlug } from '../common/helpers/getPaths'
import archivedNews from '../lib/archive/archivedNewsPaths.json'

const PERMANENT_REDIRECT = 301
const TEMPORARY_REDIRECT = 302
Expand Down Expand Up @@ -47,7 +48,11 @@ export async function middleware(request: NextRequest) {
// Redirect external links to news which is now archived if link doesn't exist in Sanity
if (hasArchivedNews && pathname.startsWith('/news') && !pathname.startsWith('/news/archive')) {
const existsInSanity = await pathExistsInSanity(pathname, isPreview)
if (!existsInSanity) return NextResponse.redirect(`${origin}${pathname.replace('news', 'news/archive')}`)
if (!existsInSanity) {
const archivedPath = pathname.replace('news', 'news/archive')
const existsInArchive = archivedNews.some((e) => e.slug === archivedPath)
if (existsInArchive) return NextResponse.redirect(`${origin}${archivedPath}`)
}
}

// Redirect to the same url lowercased if necessary
Expand Down
4 changes: 4 additions & 0 deletions web/pages/news/archive/[...pagePath].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { SkipNavContent } from '@reach/skip-nav'
import { hasArchivedNews, isGlobal } from '../../../common/helpers/datasetHelpers'
import { getFullUrl } from '../../../common/helpers/getFullUrl'
import { filterDataToSingleItem } from '../../../lib/filterDataToSingleItem'
import archivedNews from '../../../lib/archive/archivedNewsPaths.json'

import type { MenuData, SimpleMenuData } from '../../../types/types'
import { FormattedMessage } from 'react-intl'
Expand Down Expand Up @@ -176,6 +177,9 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, params,
const pagePathArray = params?.pagePath as string[]
const pagePath = pagePathArray.join('/')

const existsInArchive = archivedNews.some((e) => e.slug === `/news/archive/${pagePath}`)
if (!existsInArchive) return { notFound: true }

const response = await fetchArchiveData(pagePathArray, pagePath, locale)
const pageData = response ? await parseResponse(response) : false

Expand Down

0 comments on commit 5552ed8

Please sign in to comment.