Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/blog-starter-kit/themes/personal/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ const config = {
source: '/api/analytics',
destination: `${HASHNODE_ADVANCED_ANALYTICS_URL}/api/analytics`,
},
{
source: '/feed.xml',
destination: '/rss.xml',
},
];
},
async redirects() {
Expand Down
52 changes: 31 additions & 21 deletions packages/blog-starter-kit/themes/personal/pages/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,48 @@ export const getStaticProps: GetStaticProps<Props, Params> = async ({ params })
const host = process.env.NEXT_PUBLIC_HASHNODE_PUBLICATION_HOST;
const slug = params.slug;

const postData = await request(endpoint, SinglePostByPublicationDocument, { host, slug });
try {
const postData = await request(endpoint, SinglePostByPublicationDocument, { host, slug });

if (postData.publication?.post) {
return {
props: {
type: 'post',
post: postData.publication.post,
publication: postData.publication,
},
revalidate: 1,
};
}

const pageData = await request(endpoint, PageByPublicationDocument, { host, slug });

if (pageData.publication?.staticPage) {
return {
props: {
type: 'page',
page: pageData.publication.staticPage,
publication: pageData.publication,
},
revalidate: 1,
};
}

if (postData.publication?.post) {
return {
props: {
type: 'post',
post: postData.publication.post,
publication: postData.publication,
},
notFound: true,
revalidate: 1,
};
}

const pageData = await request(endpoint, PageByPublicationDocument, { host, slug });
} catch (error) {
console.error('Error fetching data:', error);

if (pageData.publication?.staticPage) {
return {
props: {
type: 'page',
page: pageData.publication.staticPage,
publication: pageData.publication,
},
notFound: true,
revalidate: 1,
};
}

return {
notFound: true,
revalidate: 1,
};
};


export async function getStaticPaths() {
const data = await request(
process.env.NEXT_PUBLIC_HASHNODE_GQL_ENDPOINT,
Expand Down
39 changes: 39 additions & 0 deletions packages/blog-starter-kit/themes/personal/pages/favicon.ico.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import request from 'graphql-request';
import { type GetServerSideProps } from 'next';
import {
PublicationByHostDocument,
PublicationByHostQuery,
PublicationByHostQueryVariables,
} from '../generated/graphql';

const GQL_ENDPOINT = process.env.NEXT_PUBLIC_HASHNODE_GQL_ENDPOINT;
const FaviconIco = () => null;

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const { res } = ctx;
const host = process.env.NEXT_PUBLIC_HASHNODE_PUBLICATION_HOST;
if (!host) {
throw new Error('Could not determine host');
}

const data = await request<PublicationByHostQuery, PublicationByHostQueryVariables>(
GQL_ENDPOINT,
PublicationByHostDocument,
{
host: process.env.NEXT_PUBLIC_HASHNODE_PUBLICATION_HOST,
},
);
if (!data.publication?.favicon) throw new Error('Favicon could not be found');

const faviconResponse = await fetch(data.publication.favicon);
const faviconBuffer = await faviconResponse.arrayBuffer();

res.setHeader('Cache-Control', 's-maxage=86400, stale-while-revalidate');
res.setHeader('content-type', 'image/x-icon');
res.write(Buffer.from(faviconBuffer));
res.end();

return { props: {} };
};

export default FaviconIco;