forked from Mirrors/elk
refactor: use runtimeConfig
This commit is contained in:
parent
5e717cac03
commit
e88d255c07
2 changed files with 6 additions and 4 deletions
|
@ -61,6 +61,7 @@ export default defineNuxtConfig({
|
||||||
namespaceId: '',
|
namespaceId: '',
|
||||||
apiToken: '',
|
apiToken: '',
|
||||||
},
|
},
|
||||||
|
opengraphApi: '',
|
||||||
public: {
|
public: {
|
||||||
env: isCI ? isPreview ? 'staging' : 'production' : 'local',
|
env: isCI ? isPreview ? 'staging' : 'production' : 'local',
|
||||||
translateApi: '',
|
translateApi: '',
|
||||||
|
|
|
@ -7,12 +7,12 @@ type OpenGraphClient = ReturnType<typeof opengraph>
|
||||||
let openGraphClient: OpenGraphClient
|
let openGraphClient: OpenGraphClient
|
||||||
|
|
||||||
function getOpenGraphClient(): OpenGraphClient {
|
function getOpenGraphClient(): OpenGraphClient {
|
||||||
const NUXT_OPENGRAPH_API = process.env.NUXT_OPENGRAPH_API
|
const appId = useRuntimeConfig().opengraphApi
|
||||||
if (typeof NUXT_OPENGRAPH_API !== 'string')
|
if (typeof appId !== 'string')
|
||||||
throw new Error('Missing NUXT_OPENGRAPH_API environment variable.')
|
throw new Error('Missing NUXT_OPENGRAPH_API environment variable.')
|
||||||
|
|
||||||
if (!openGraphClient)
|
if (!openGraphClient)
|
||||||
openGraphClient = opengraph({ appId: NUXT_OPENGRAPH_API, fullRender: true })!
|
openGraphClient = opengraph({ appId, fullRender: true })!
|
||||||
|
|
||||||
return openGraphClient
|
return openGraphClient
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ async function resolveOgImageUrlManually(cardUrl: string): Promise<string> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
|
const config = useRuntimeConfig()
|
||||||
const { url } = getRouterParams(event)
|
const { url } = getRouterParams(event)
|
||||||
const { fallbackUrl } = getQuery(event)
|
const { fallbackUrl } = getQuery(event)
|
||||||
|
|
||||||
|
@ -64,7 +65,7 @@ export default defineEventHandler(async (event) => {
|
||||||
'',
|
'',
|
||||||
)
|
)
|
||||||
|
|
||||||
if (process.env.NUXT_OPENGRAPH_API) {
|
if (config.opengraphApi) {
|
||||||
// If no og:image was found, try to get it from opengraph.io
|
// If no og:image was found, try to get it from opengraph.io
|
||||||
if (!ogImageUrl) {
|
if (!ogImageUrl) {
|
||||||
const response = await getOpenGraphClient().getSiteInfo(cardUrl).catch(() =>
|
const response = await getOpenGraphClient().getSiteInfo(cardUrl).catch(() =>
|
||||||
|
|
Loading…
Reference in a new issue