Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help: Get access to generated OgImage path #264

Open
julbd opened this issue Sep 27, 2024 · 2 comments
Open

help: Get access to generated OgImage path #264

julbd opened this issue Sep 27, 2024 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@julbd
Copy link

julbd commented Sep 27, 2024

📚 What are you trying to do?

Hello,

I'm looking for a way to get the path to an ogImage without actually defining it as the ogImage of the current page. The objective is to provide the user a "screenshot" of a component in full static.

Something like (pseudo-code) :

<template>
    <img src="{{ogImagePath}}" />
</template>

<script setup>
    const ogImagePath = useOgImageComponent('NuxtSeo');
</script>

Thanks. 🤗

🔍 What have you tried?

No response

ℹ️ Additional context

No response

@julbd julbd added the help wanted Extra attention is needed label Sep 27, 2024
@harlan-zw
Copy link
Collaborator

Hi, thanks for the issue.

I may look to add a composable as you've described or alternatively just return the meta data from the defineOgImage composable if more people need it.

For the time being you can use this code.

export function getOgImagePath(pagePath?: string, _options?: Partial<Pick<OgImageOptions, 'extension'>>) {
  const runtimeConfig = useRuntimeConfig()
  const { baseURL } = runtimeConfig.app
  const route = useRoute()
  const options = defu(_options, runtimeConfig['nuxt-og-image'].defaults)
  return joinURL('/', baseURL, `__og-image__/${import.meta.prerender ? 'static' : 'image'}`, pagePath || route.path, `og.${options.extension}`)
}
<template>
    <img src="{{ogImagePath}}" />
</template>

<script setup>
    defineOgImageComponent('NuxtSeo', { /* ... */ })
    const ogImagePath = getOgImagePath();
</script>

@martinadamsdev
Copy link

export function getOgImagePath(pagePath?: string, _options?: Partial<Pick<OgImageOptions, 'extension'>>) {
  const runtimeConfig = useRuntimeConfig()
  const { baseURL } = runtimeConfig.app
  const route = useRoute()
  const options = defu(_options, runtimeConfig['nuxt-og-image'].defaults)
  return joinURL('/', baseURL, `__og-image__/${import.meta.prerender ? 'static' : 'image'}`, pagePath || route.path, `og.${options.extension}`)
}

@harlan-zw

Your reply is very helpful to me. Thank you very much.
I have a website that has many paths that need a unified style.
How can I change the __og-image__ path?
eg:

`/${import.meta.prerender ? 'static' : 'image'}`, pagePath || route.path, `og.${options.extension}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants