diff --git a/app/[locale]/[[...slug]]/page.tsx b/app/[locale]/[[...slug]]/page.tsx index 35dcf0f51..4facab3d3 100644 --- a/app/[locale]/[[...slug]]/page.tsx +++ b/app/[locale]/[[...slug]]/page.tsx @@ -1,9 +1,12 @@ +// Next +import type { Metadata } from 'next'; +import { draftMode } from 'next/headers'; +// Sanity import PreviewSuspense from 'components/preview-suspense'; import getQueryFromSlug from 'helpers/getQueryFromSlug'; import { docQuery } from 'lib/sanity/queries'; -import { client } from 'lib/sanity/sanity.client'; -import type { Metadata } from 'next'; -import { draftMode } from 'next/headers'; +import { clientFetch } from 'lib/sanity/sanity.client'; +// Pages. import CategoryPage from './category-page'; import CategoryPagePreview from './category-page-preview'; import HomePage from './home-page'; @@ -16,40 +19,28 @@ import SinglePagePreview from './single-page-preview'; /** * Render pages depending on type. */ -export default async function Page({ - params, -}: { - params: { slug: string[], locale: string }; -}) { +export default async function Page({ params }: { params: { slug: string[]; locale: string } }) { const { isEnabled } = draftMode(); const { slug, locale } = params; - - const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale) - const pageData = await client.fetch(query, queryParams) + const { query = '', queryParams, docType } = getQueryFromSlug(slug, locale); - const data = filterDataToSingleItem(pageData, isEnabled) + const pageData = await clientFetch(query, queryParams); + + const data = filterDataToSingleItem(pageData, isEnabled); if (isEnabled) { return ( - {docType === 'home' && ( - - )} - {docType === 'page' && ( - - )} - {docType === 'product' && ( - - )} - {docType === 'category' && ( - - )} + {docType === 'home' && } + {docType === 'page' && } + {docType === 'product' && } + {docType === 'category' && } - ) + ); } - + return ( <> {docType === 'home' && } @@ -57,7 +48,7 @@ export default async function Page({ {docType === 'category' && } {docType === 'page' && } - ) + ); } // Background revalidate once every day. @@ -67,15 +58,12 @@ export const revalidate = 86400; * Get paths for each page. */ export async function generateStaticParams() { - const paths = await client.fetch(docQuery) + const paths = await clientFetch(docQuery); - return paths.map((path: { - slug: string, - locale: string - }) => ({ + return paths.map((path: { slug: string; locale: string }) => ({ slug: path.slug.split('/').filter((p) => p), locale: path.locale - })) + })); } /** @@ -83,54 +71,51 @@ export async function generateStaticParams() { * If we're in "preview mode" and have multiple documents, return the draft */ function filterDataToSingleItem(data: any, preview = false) { - if (!Array.isArray(data)) { - return data + return data; } if (data.length === 1) { - return data[0] + return data[0]; } if (preview) { - return data.find((item) => item._id.startsWith(`drafts.`)) || data[0] + return data.find((item) => item._id.startsWith(`drafts.`)) || data[0]; } - return data[0] + return data[0]; } /** * Generate metadata for each page. */ -export async function generateMetadata({ params }: {params: { slug: string[], locale: string }}): Promise { - const { slug, locale } = params +export async function generateMetadata({ + params +}: { + params: { slug: string[]; locale: string }; +}): Promise { + const { slug, locale } = params; - const { query = '', queryParams } = getQueryFromSlug(slug, locale) + const { query = '', queryParams } = getQueryFromSlug(slug, locale); - const pageData = await client.fetch(query, queryParams) + const pageData = await clientFetch(query, queryParams); - const data = filterDataToSingleItem(pageData, false) + const data = filterDataToSingleItem(pageData, false); const { seo } = data ?? {}; return { title: seo?.title ? seo?.title : data?.title, - description: seo?.description - ? seo.description - : 'Webb och digitalbyrå från Göteborg', + description: seo?.description ? seo.description : 'Webb och digitalbyrå från Göteborg', openGraph: { images: [ { - url: seo?.image?.asset?.url - ? seo.image.asset.url - : '/og-image.jpg', + url: seo?.image?.asset?.url ? seo.image.asset.url : '/og-image.jpg', width: 1200, height: 630, - alt: seo?.coverImage?.alt - ? seo.coverImage.alt - : 'Kodamera AB', - }, - ], - }, - } -} \ No newline at end of file + alt: seo?.coverImage?.alt ? seo.coverImage.alt : 'Kodamera AB' + } + ] + } + }; +} diff --git a/components/ui/text/text.tsx b/components/ui/text/text.tsx index 2e7db83f1..66ad02881 100644 --- a/components/ui/text/text.tsx +++ b/components/ui/text/text.tsx @@ -1,19 +1,15 @@ -'use client' +'use client'; -import { cn } from 'lib/utils' -import React, { - CSSProperties, - FunctionComponent, - JSXElementConstructor, -} from 'react' +import { cn } from 'lib/utils'; +import React, { CSSProperties, FunctionComponent, JSXElementConstructor } from 'react'; interface TextProps { - variant?: Variant - className?: string - style?: CSSProperties - children?: React.ReactNode | any - html?: string - onClick?: () => any + variant?: Variant; + className?: string; + style?: CSSProperties; + children?: React.ReactNode | any; + html?: string; + onClick?: () => any; } type Variant = @@ -24,7 +20,7 @@ type Variant = | 'sectionHeading' | 'label' | 'paragraph' - | 'listChildHeading' + | 'listChildHeading'; const Text: FunctionComponent = ({ style, @@ -32,10 +28,10 @@ const Text: FunctionComponent = ({ variant = 'body', children, html, - onClick, + onClick }) => { const componentsMap: { - [P in Variant]: React.ComponentType | string + [P in Variant]: React.ComponentType | string; } = { body: 'div', heading: 'h1', @@ -44,39 +40,38 @@ const Text: FunctionComponent = ({ sectionHeading: 'h2', listChildHeading: 'h3', label: 'div', - paragraph: 'p', - } + paragraph: 'p' + }; const Component: | JSXElementConstructor | React.ReactElement | React.ComponentType - | string = componentsMap![variant!] + | string = componentsMap![variant!]; const htmlContentProps = html ? { - dangerouslySetInnerHTML: { __html: html }, + dangerouslySetInnerHTML: { __html: html } } - : {} + : {}; return ( = ({ > {children} - ) -} + ); +}; -export default Text +export default Text; diff --git a/lib/sanity/sanity.client.ts b/lib/sanity/sanity.client.ts index 197f019d1..b7bd07fb9 100644 --- a/lib/sanity/sanity.client.ts +++ b/lib/sanity/sanity.client.ts @@ -1,4 +1,5 @@ import { createClient } from "next-sanity"; +import { cache } from 'react'; export const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID!; export const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET!; @@ -9,4 +10,6 @@ export const client = createClient({ dataset, apiVersion, useCdn: true, -}); \ No newline at end of file +}); + +export const clientFetch = cache(client.fetch.bind(client)); \ No newline at end of file