@@ -51,7 +51,7 @@ const draftQuery = gql`
5151 }
5252` ;
5353
54- const postQuery = gql `
54+ const postBySlugQuery = gql `
5555 query GetPost($host: String!, $slug: String!) {
5656 publication(host: $host) {
5757 post(slug: $slug) {
@@ -67,19 +67,44 @@ const postQuery = gql`
6767 }
6868` ;
6969
70+ const postByIdQuery = gql `
71+ query GetPostById($id: ID!) {
72+ post(id: $id) {
73+ ${ commonFields }
74+ tags {
75+ id
76+ name
77+ slug
78+ }
79+ publishedAt
80+ }
81+ }
82+ ` ;
83+
7084export async function fetchContent ( idOrSlug ) {
7185 const isValidObjectId =
7286 idOrSlug . length === 24 && / ^ [ 0 - 9 a - f A - F ] { 24 } $ / . test ( idOrSlug ) ;
7387 const HASHNODE_HOST = process . env . HASHNODE_HOST || 'handle.hashnode.dev' ;
7488
7589 try {
76- // Fetch if it's a draft from the drafts endpoint
7790 if ( isValidObjectId ) {
78- const response = await request ( endpoint , draftQuery , { id : idOrSlug } ) ;
79- return response . draft ;
80- // Fetch if it's a post from the publication endpoint
91+ // Try draft first, then fall back to post-by-id (published posts also
92+ // have 24-char hex cuid values)
93+ try {
94+ const response = await request ( endpoint , draftQuery , {
95+ id : idOrSlug
96+ } ) ;
97+ if ( response . draft ) return response . draft ;
98+ } catch {
99+ // Draft not found — try as a published post id
100+ }
101+
102+ const response = await request ( endpoint , postByIdQuery , {
103+ id : idOrSlug
104+ } ) ;
105+ return response . post ;
81106 } else {
82- const response = await request ( endpoint , postQuery , {
107+ const response = await request ( endpoint , postBySlugQuery , {
83108 host : HASHNODE_HOST ,
84109 slug : idOrSlug
85110 } ) ;
0 commit comments