Skip to content

Commit

Permalink
refactor(core): blog fragment colocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemoya committed May 2, 2024
1 parent dfd5b25 commit 32ad0e8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
63 changes: 63 additions & 0 deletions apps/core/app/[locale]/(default)/blog/[blogId]/page-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { graphql } from 'gql.tada';
import { cache } from 'react';

import { client } from '~/client';
import { revalidate } from '~/client/revalidate-target';

const BlogPostPageQuery = graphql(`
query BlogPostPageQuery($entityId: Int!) {
site {
content {
blog {
isVisibleInNavigation
post(entityId: $entityId) {
author
htmlBody
id
name
publishedDate {
utc
}
tags
thumbnailImage {
altText
url: urlTemplate
}
seo {
metaKeywords
metaDescription
pageTitle
}
}
}
}
settings {
url {
vanityUrl
}
}
}
}
`);

export const getBlogPost = cache(async ({ entityId }: { entityId: number }) => {
const response = await client.fetch({
document: BlogPostPageQuery,
variables: { entityId },
fetchOptions: { next: { revalidate } },
});

const { blog } = response.data.site.content;

if (!blog?.post) {
return null;
}

const { isVisibleInNavigation, post } = blog;

return {
...post,
isVisibleInNavigation,
vanityUrl: response.data.site.settings?.url.vanityUrl,
};
});
8 changes: 5 additions & 3 deletions apps/core/app/[locale]/(default)/blog/[blogId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { getFormatter } from 'next-intl/server';

import { getBlogPost } from '~/client/queries/get-blog-post';
import { BcImage } from '~/components/bc-image';
import { Link } from '~/components/link';
import { SharingLinks } from '~/components/sharing-links';
Expand All @@ -16,6 +15,8 @@ import {
import { Tag, TagContent } from '~/components/ui/tag';
import { LocaleType } from '~/i18n';

import { getBlogPost } from './page-data';

interface Props {
params: {
blogId: string;
Expand All @@ -24,7 +25,7 @@ interface Props {
}

export async function generateMetadata({ params: { blogId } }: Props): Promise<Metadata> {
const blogPost = await getBlogPost(+blogId);
const blogPost = await getBlogPost({ entityId: Number(blogId) });

const title = blogPost?.seo.pageTitle ?? 'Blog';

Expand All @@ -35,7 +36,8 @@ export async function generateMetadata({ params: { blogId } }: Props): Promise<M

export default async function BlogPostPage({ params: { blogId, locale } }: Props) {
const format = await getFormatter({ locale });
const blogPost = await getBlogPost(+blogId);

const blogPost = await getBlogPost({ entityId: Number(blogId) });

if (!blogPost || !blogPost.isVisibleInNavigation) {
return notFound();
Expand Down

0 comments on commit 32ad0e8

Please sign in to comment.