diff --git a/src/app/(frontend)/[...slug]/page.tsx b/src/app/(frontend)/[...slug]/page.tsx index 69e097f..9fa23e5 100644 --- a/src/app/(frontend)/[...slug]/page.tsx +++ b/src/app/(frontend)/[...slug]/page.tsx @@ -9,7 +9,7 @@ import processMetadata from '@/lib/processMetadata' export default async function Page({ params }: Props) { const page = await getPage(await params) if (!page) notFound() - return + return } export async function generateMetadata({ params }: Props) { @@ -31,6 +31,8 @@ export async function generateStaticParams() { } async function getPage(params: { slug?: string[] }) { + const slug = params.slug?.join('/') + return await fetchSanityLive({ query: groq`*[ _type == 'page' && @@ -38,13 +40,24 @@ async function getPage(params: { slug?: string[] }) { !(metadata.slug.current in ['index', 'blog/*']) ][0]{ ..., - modules[]{ ${MODULES_QUERY} }, + 'modules': ( + // page modules + modules[]{ ${MODULES_QUERY} } + // path modules + + *[_type == 'global-module' && path.current != '*' && ($slug + '/*' != path.current && $slug match path.current)].modules[]{ + ${MODULES_QUERY} + } + // global modules + + *[_type == 'global-module' && path.current == '*'].modules[]{ + ${MODULES_QUERY} + } + ), metadata { ..., 'ogimage': image.asset->url + '?w=1200' } }`, - params: { slug: params.slug?.join('/') }, + params: { slug }, }) } diff --git a/src/app/(frontend)/page.tsx b/src/app/(frontend)/page.tsx index 5f17e28..bc4ff71 100644 --- a/src/app/(frontend)/page.tsx +++ b/src/app/(frontend)/page.tsx @@ -15,10 +15,15 @@ export async function generateMetadata() { } async function getPage() { - const data = await fetchSanityLive({ + const page = await fetchSanityLive({ query: groq`*[_type == 'page' && metadata.slug.current == 'index'][0]{ ..., - modules[]{ ${MODULES_QUERY} }, + 'modules': ( + // page modules + modules[]{ ${MODULES_QUERY} } + // global modules + + *[_type == 'global-module' && path.current == '*'].modules[]{ ${MODULES_QUERY} } + ), metadata { ..., 'ogimage': image.asset->url + '?w=1200', @@ -26,8 +31,8 @@ async function getPage() { }`, }) - if (!data) + if (!page) throw Error('No `page` document with slug "index" found in the Studio') - return data + return page } diff --git a/src/sanity/schemaTypes/documents/global-module.ts b/src/sanity/schemaTypes/documents/global-module.ts index 02272f4..e1e53b9 100644 --- a/src/sanity/schemaTypes/documents/global-module.ts +++ b/src/sanity/schemaTypes/documents/global-module.ts @@ -12,7 +12,8 @@ export default defineType({ defineField({ name: 'path', type: 'slug', - description: 'The path to add modules. Leave blank for all pages.', + description: 'The path to add modules. eg. * (all pages), docs/*, etc.', + validation: (Rule) => Rule.required(), }), defineField({ ...modules, @@ -26,7 +27,7 @@ export default defineType({ }, prepare: ({ path, modules }) => ({ title: count(modules, 'module'), - subtitle: path ? `/${path}` : 'All pages', + subtitle: path === '*' ? 'All pages' : path && `/${path}`, }), }, }) diff --git a/src/types/Sanity.d.ts b/src/types/Sanity.d.ts index 4ac0863..be41c46 100644 --- a/src/types/Sanity.d.ts +++ b/src/types/Sanity.d.ts @@ -45,6 +45,11 @@ declare global { modules?: Module[] } + interface GlobalModule extends SanityDocument { + path?: { current: string } + modules?: Module[] + } + interface BlogPost extends PageBase { readonly _type: 'blog.post' body: any