Skip to content

Commit

Permalink
Merge branch 'main' into fix/preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcastres authored Jan 27, 2025
2 parents 8ef9d06 + 516011b commit 8a6784f
Show file tree
Hide file tree
Showing 17 changed files with 450 additions and 187 deletions.
24 changes: 17 additions & 7 deletions next/app/[locale]/(marketing)/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Metadata } from 'next';

import PageContent from '@/lib/shared/PageContent';
import fetchContentType from '@/lib/strapi/fetchContentType';
import { generateMetadataObject } from '@/lib/shared/metadata';
Expand All @@ -11,9 +10,15 @@ export async function generateMetadata({
params: { locale: string; slug: string };
}): Promise<Metadata> {
const pageData = await fetchContentType(
'pages',
`filters[slug][$eq]=${params.slug}&filters[locale][$eq]=${params.locale}&populate=seo.metaImage`,
true
"pages",
{
filters: {
slug: params.slug,
locale: params.locale,
},
populate: "seo.metaImage",
},
true,
);

const seo = pageData?.seo;
Expand All @@ -23,9 +28,14 @@ export async function generateMetadata({

export default async function Page({ params }: { params: { locale: string, slug: string } }) {
const pageData = await fetchContentType(
'pages',
`filters[slug][$eq]=${params.slug}&filters[locale][$eq]=${params.locale}`,
true
"pages",
{
filters: {
slug: params.slug,
locale: params.locale,
},
},
true,
);

const localizedSlugs = pageData.localizations?.reduce(
Expand Down
9 changes: 7 additions & 2 deletions next/app/[locale]/(marketing)/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ export default async function SingleArticlePage({
}) {
const article = await fetchContentType(
"articles",
`filters[slug]=${params?.slug}&filters[locale][$eq]=${params.locale}`,
true
{
filters: {
slug: params.slug,
locale: params.locale,
}
},
true,
);

if (!article) {
Expand Down
16 changes: 12 additions & 4 deletions next/app/[locale]/(marketing)/blog/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Metadata } from "next";

import { Container } from "@/components/container";
import { Heading } from "@/components/elements/heading";
import { Subheading } from "@/components/elements/subheading";
Expand All @@ -18,7 +19,10 @@ export async function generateMetadata({
}: {
params: { locale: string };
}): Promise<Metadata> {
const pageData = await fetchContentType('blog-page', `filters[locale]=${params.locale}&populate=seo.metaImage`, true)
const pageData = await fetchContentType('blog-page', {
filters: { locale: params.locale },
populate: "seo.metaImage",
}, true)

const seo = pageData?.seo;
const metadata = generateMetadataObject(seo);
Expand All @@ -30,8 +34,12 @@ export default async function Blog({
}: {
params: { locale: string, slug: string };
}) {
const blogPage = await fetchContentType('blog-page', `filters[locale]=${params.locale}`, true)
const articles = await fetchContentType('articles', `filters[locale]=${params.locale}`)
const blogPage = await fetchContentType('blog-page', {
filters: { locale: params.locale },
}, true)
const articles = await fetchContentType('articles', {
filters: { locale: params.locale },
}, false)

const localizedSlugs = blogPage.localizations?.reduce(
(acc: Record<string, string>, localization: any) => {
Expand All @@ -43,7 +51,7 @@ export default async function Blog({

return (
<div className="relative overflow-hidden py-20 md:py-0">
<ClientSlugHandler localizedSlugs={localizedSlugs} />
<ClientSlugHandler localizedSlugs={localizedSlugs} />
<AmbientColor />
<Container className="flex flex-col items-center justify-between pb-20">
<div className="relative z-20 py-10 md:pt-40">
Expand Down
17 changes: 15 additions & 2 deletions next/app/[locale]/(marketing)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ export async function generateMetadata({
}: {
params: { locale: string };
}): Promise<Metadata> {

const pageData = await fetchContentType(
'pages',
`filters[slug][$eq]=homepage&filters[locale][$eq]=${params.locale}&populate=seo.metaImage`,
{
filters: {
slug: "homepage",
locale: params.locale,
},
populate: "seo.metaImage",
},
true
);

Expand All @@ -22,9 +29,15 @@ export async function generateMetadata({
}

export default async function HomePage({ params }: { params: { locale: string } }) {

const pageData = await fetchContentType(
'pages',
`filters[slug][$eq]=homepage&filters[locale][$eq]=${params.locale}`,
{
filters: {
slug: "homepage",
locale: params.locale,
},
},
true
);

Expand Down
12 changes: 10 additions & 2 deletions next/app/[locale]/(marketing)/products/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Metadata } from "next";

import { redirect } from "next/navigation";
import { Container } from "@/components/container";
import { AmbientColor } from "@/components/decorations/ambient-color";
Expand All @@ -13,7 +14,11 @@ export async function generateMetadata({
}: {
params: { locale: string, slug: string };
}): Promise<Metadata> {
const pageData = await fetchContentType("products", `filters[slug]=${params?.slug}&populate=seo.metaImage`, true)

const pageData = await fetchContentType("products", {
filters: { slug: params.slug },
populate: "seo.metaImage",
}, true)

const seo = pageData?.seo;
const metadata = generateMetadataObject(seo);
Expand All @@ -25,7 +30,10 @@ export default async function SingleProductPage({
}: {
params: { slug: string, locale: string };
}) {
const product = await fetchContentType("products", `filters[slug]=${params?.slug}`, true)

const product = await fetchContentType("products", {
filters: { slug: params.slug },
}, true)

if (!product) {
redirect("/products");
Expand Down
17 changes: 14 additions & 3 deletions next/app/[locale]/(marketing)/products/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export async function generateMetadata({
}: {
params: { locale: string };
}): Promise<Metadata> {
const pageData = await fetchContentType("product-page", `filters[locale][$eq]=${params.locale}&populate=seo.metaImage`, true)

const pageData = await fetchContentType("product-page", {
filters: {
locale: params.locale,
},
populate: "seo.metaImage",
}, true)

const seo = pageData?.seo;
const metadata = generateMetadataObject(seo);
Expand All @@ -30,9 +36,14 @@ export default async function Products({
}: {
params: { locale: string };
}) {

// Fetch the product-page and products data
const productPage = await fetchContentType('product-page', `filters[locale]=${params.locale}`, true);
const products = await fetchContentType('products', ``);
const productPage = await fetchContentType('product-page', {
filters: {
locale: params.locale,
},
}, true);
const products = await fetchContentType('products');

const localizedSlugs = productPage.localizations?.reduce(
(acc: Record<string, string>, localization: any) => {
Expand Down
7 changes: 5 additions & 2 deletions next/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export async function generateMetadata({
}): Promise<Metadata> {
const pageData = await fetchContentType(
'global',
`&filters[locale][$eq]=${params.locale}&populate=seo.metaImage`,
{
filters: { locale: params.locale },
populate: "seo.metaImage",
},
true
);

Expand All @@ -42,7 +45,7 @@ export default async function LocaleLayout({
params: { locale: string };
}) {

const pageData = await fetchContentType('global', `filters[locale][$eq]=${locale}`, true);
const pageData = await fetchContentType('global', { filters: { locale } }, true);
return (
<html lang={locale}>
<ViewTransitions>
Expand Down
43 changes: 43 additions & 0 deletions next/app/api/preview/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'

export const GET = async (request: Request) => {

const { searchParams } = new URL(request.url)
const secret = searchParams.get('secret')
const slug = searchParams.get('slug')
const locale = searchParams.get('locale')
const uid = searchParams.get('uid')
const status = searchParams.get('status');

if (
secret !== process.env.PREVIEW_SECRET
) {
return new Response('Invalid token', { status: 401 })
}

const contentType = uid?.split(".").pop();

// Specific for the application
let slugToReturn = `/${locale}/${contentType}`;

if (contentType === 'page' || contentType === 'global') {
if (slug && slug !== 'homepage') {
slugToReturn = `/${locale}/${slug}`;
} else {
slugToReturn = `/${locale}`;
}
} else if (contentType === 'article' || contentType?.includes('blog')) {
slugToReturn = `/${locale}/blog${slug ? `/${slug}` : ''}`;
} else if (contentType?.includes('product')) {
slugToReturn = `/en/products${slug ? `/${slug}` : ''}`;
}

const draft = await draftMode()
if (status === 'draft') {
draft.enable()
} else {
draft.disable()
}
redirect(slugToReturn)
};
4 changes: 2 additions & 2 deletions next/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default function RootLayout({
params: { lang: Locale }
}) {
return (
<html lang={params.lang}>
<body>
<html lang={params.lang} suppressHydrationWarning>
<body suppressHydrationWarning>
<SlugProvider>
{children}
</SlugProvider>
Expand Down
2 changes: 1 addition & 1 deletion next/components/navbar/mobile-navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const MobileNavbar = ({ leftNavbarItems, rightNavbarItems, logo, locale }
<div className="flex items-center justify-between w-full px-5">
<Logo locale={locale} image={logo?.image} />
<div className="flex items-center space-x-2">
<LocaleSwitcher />
<LocaleSwitcher currentLocale={locale} />
<IoIosClose
className="h-8 w-8 text-white"
onClick={() => setOpen(!open)}
Expand Down
17 changes: 14 additions & 3 deletions next/lib/strapi/fetchContentType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { draftMode } from "next/headers";
import qs from "qs";
/**
* Fetches data for a specified Strapi content type.
*
Expand Down Expand Up @@ -27,15 +29,24 @@ export function spreadStrapiData(data: StrapiResponse): StrapiData | null {

export default async function fetchContentType(
contentType: string,
params: string,
spreadData?: boolean
params: Record<string, unknown> = {},
spreadData?: boolean,
): Promise<any> {
const { isEnabled } = await draftMode()

try {

const queryParams = { ...params };

if (isEnabled) {
queryParams.status = "draft";
}

// Construct the full URL for the API request
const url = new URL(`api/${contentType}`, process.env.NEXT_PUBLIC_API_URL);

// Perform the fetch request with the provided query parameters
const response = await fetch(`${url.href}?${params}`, {
const response = await fetch(`${url.href}?${qs.stringify(queryParams)}`, {
method: 'GET',
cache: 'no-store',
});
Expand Down
2 changes: 2 additions & 0 deletions next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"negotiator": "^0.6.3",
"next": "14.2.5",
"next-view-transitions": "^0.2.0",
"qs": "^6.14.0",
"react": "^18",
"react-dom": "^18",
"react-fast-marquee": "^1.6.5",
Expand All @@ -46,6 +47,7 @@
"devDependencies": {
"@types/negotiator": "^0.6.3",
"@types/node": "^20",
"@types/qs": "^6.9.16",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/three": "^0.167.1",
Expand Down
Loading

0 comments on commit 8a6784f

Please sign in to comment.