chore: replace with og endpoint (#5571)
* chore: replace with og endpoint * fix: update og routespull/5579/head
parent
d441c9ac58
commit
b7a858823c
12 changed files with 121 additions and 25 deletions
@ -1,13 +1,26 @@ |
|||||||
type RoadmapOpenGraphQuery = { |
type RoadmapOpenGraphQuery = { |
||||||
group: 'roadmaps' | 'guides' | 'best-practices'; |
group: 'roadmap' | 'guide' | 'best-practice'; |
||||||
resourceId: string; |
resourceId: string; |
||||||
}; |
}; |
||||||
|
|
||||||
export function getOpenGraphImageUrl(params: RoadmapOpenGraphQuery) { |
export function getOpenGraphImageUrl(params: RoadmapOpenGraphQuery) { |
||||||
return `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/og-images/${params.group}/${params.resourceId}.png`; |
return `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/og/${params.group}/${params.resourceId}`; |
||||||
} |
} |
||||||
|
|
||||||
export async function getDefaultOpenGraphImageBuffer() { |
export async function getDefaultOpenGraphImageBuffer() { |
||||||
const defaultImageUrl = `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/images/og-img.png`; |
const defaultImageUrl = `${import.meta.env.DEV ? 'http://localhost:3000' : 'https://roadmap.sh'}/images/og-img.png`; |
||||||
return fetch(defaultImageUrl).then((response) => response.arrayBuffer()); |
return fetch(defaultImageUrl).then((response) => response.arrayBuffer()); |
||||||
} |
} |
||||||
|
|
||||||
|
export async function getResourceOpenGraph( |
||||||
|
type: 'roadmap' | 'guide' | 'best-practice', |
||||||
|
resourceId: string, |
||||||
|
) { |
||||||
|
const url = new URL(`${import.meta.env.PUBLIC_API_URL}/v1-open-graph`); |
||||||
|
url.searchParams.set('type', type); |
||||||
|
url.searchParams.set('resourceId', resourceId); |
||||||
|
url.searchParams.set('variant', 'image'); |
||||||
|
const response = await fetch(url.toString()); |
||||||
|
|
||||||
|
return response.text(); |
||||||
|
} |
||||||
|
@ -0,0 +1,31 @@ |
|||||||
|
import type { APIRoute } from 'astro'; |
||||||
|
import { |
||||||
|
getDefaultOpenGraphImageBuffer, |
||||||
|
getResourceOpenGraph, |
||||||
|
} from '../../../lib/open-graph'; |
||||||
|
|
||||||
|
export const prerender = false; |
||||||
|
|
||||||
|
type Params = { |
||||||
|
slug: string; |
||||||
|
}; |
||||||
|
|
||||||
|
export const GET: APIRoute<any, Params> = async (context) => { |
||||||
|
const { slug } = context.params; |
||||||
|
|
||||||
|
if (!slug) { |
||||||
|
const buffer = await getDefaultOpenGraphImageBuffer(); |
||||||
|
return new Response(buffer, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/png', |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
const svg = await getResourceOpenGraph('best-practice', slug); |
||||||
|
return new Response(svg, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/svg+xml', |
||||||
|
}, |
||||||
|
}); |
||||||
|
}; |
@ -0,0 +1,31 @@ |
|||||||
|
import type { APIRoute } from 'astro'; |
||||||
|
import { |
||||||
|
getDefaultOpenGraphImageBuffer, |
||||||
|
getResourceOpenGraph, |
||||||
|
} from '../../../lib/open-graph'; |
||||||
|
|
||||||
|
export const prerender = false; |
||||||
|
|
||||||
|
type Params = { |
||||||
|
slug: string; |
||||||
|
}; |
||||||
|
|
||||||
|
export const GET: APIRoute<any, Params> = async (context) => { |
||||||
|
const { slug } = context.params; |
||||||
|
|
||||||
|
if (!slug) { |
||||||
|
const buffer = await getDefaultOpenGraphImageBuffer(); |
||||||
|
return new Response(buffer, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/png', |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
const svg = await getResourceOpenGraph('guide', slug); |
||||||
|
return new Response(svg, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/svg+xml', |
||||||
|
}, |
||||||
|
}); |
||||||
|
}; |
@ -0,0 +1,31 @@ |
|||||||
|
import type { APIRoute } from 'astro'; |
||||||
|
import { |
||||||
|
getDefaultOpenGraphImageBuffer, |
||||||
|
getResourceOpenGraph, |
||||||
|
} from '../../../lib/open-graph'; |
||||||
|
|
||||||
|
export const prerender = false; |
||||||
|
|
||||||
|
type Params = { |
||||||
|
slug: string; |
||||||
|
}; |
||||||
|
|
||||||
|
export const GET: APIRoute<any, Params> = async (context) => { |
||||||
|
const { slug } = context.params; |
||||||
|
|
||||||
|
if (!slug) { |
||||||
|
const buffer = await getDefaultOpenGraphImageBuffer(); |
||||||
|
return new Response(buffer, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/png', |
||||||
|
}, |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
const svg = await getResourceOpenGraph('roadmap', slug); |
||||||
|
return new Response(svg, { |
||||||
|
headers: { |
||||||
|
'Content-Type': 'image/svg+xml', |
||||||
|
}, |
||||||
|
}); |
||||||
|
}; |
@ -1,26 +1,16 @@ |
|||||||
import type { APIRoute } from 'astro'; |
import type { APIRoute } from 'astro'; |
||||||
import { getDefaultOpenGraphImageBuffer } from '../../lib/open-graph'; |
import { getDefaultOpenGraphImageBuffer } from '../../../lib/open-graph'; |
||||||
|
|
||||||
export const prerender = false; |
export const prerender = false; |
||||||
|
|
||||||
type Params = { |
type Params = { |
||||||
slug: string; |
username: string; |
||||||
}; |
}; |
||||||
|
|
||||||
export const GET: APIRoute<any, Params> = async (context) => { |
export const GET: APIRoute<any, Params> = async (context) => { |
||||||
const { slug } = context.params; |
const { username } = context.params; |
||||||
|
|
||||||
if (!slug.startsWith('user-')) { |
if (!username || !/^[a-zA-Z0-9]*?[a-zA-Z]+?[a-zA-Z0-9]*?$/.test(username)) { |
||||||
const buffer = await getDefaultOpenGraphImageBuffer(); |
|
||||||
return new Response(buffer, { |
|
||||||
headers: { |
|
||||||
'Content-Type': 'image/png', |
|
||||||
}, |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
const username = slug.replace('user-', ''); |
|
||||||
if (!username) { |
|
||||||
const buffer = await getDefaultOpenGraphImageBuffer(); |
const buffer = await getDefaultOpenGraphImageBuffer(); |
||||||
return new Response(buffer, { |
return new Response(buffer, { |
||||||
headers: { |
headers: { |
Loading…
Reference in new issue