Skip to content

Commit

Permalink
add global modules to index and /*, update GROQ
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchuman committed Feb 8, 2025
1 parent 822a691 commit d188d51
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
19 changes: 16 additions & 3 deletions src/app/(frontend)/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Modules modules={page?.modules} page={page} />
return <Modules modules={page.modules} page={page} />
}

export async function generateMetadata({ params }: Props) {
Expand All @@ -31,20 +31,33 @@ export async function generateStaticParams() {
}

async function getPage(params: { slug?: string[] }) {
const slug = params.slug?.join('/')

return await fetchSanityLive<Sanity.Page>({
query: groq`*[
_type == 'page' &&
metadata.slug.current == $slug &&
!(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 },
})
}

Expand Down
13 changes: 9 additions & 4 deletions src/app/(frontend)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ export async function generateMetadata() {
}

async function getPage() {
const data = await fetchSanityLive<Sanity.Page>({
const page = await fetchSanityLive<Sanity.Page>({
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',
}
}`,
})

if (!data)
if (!page)
throw Error('No `page` document with slug "index" found in the Studio')

return data
return page
}
5 changes: 3 additions & 2 deletions src/sanity/schemaTypes/documents/global-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}`,
}),
},
})
5 changes: 5 additions & 0 deletions src/types/Sanity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d188d51

Please sign in to comment.