Skip to content

How to get Typename from preview with Nuxt ? #1703

@mathieuCatapulpe

Description

@mathieuCatapulpe

I followed this very well done tutorial for faust+Nuxt https://wpengine.com/builders/headless-wordpress-post-previews-in-nuxt-3/

But it gives no real clues to setup previews for multiple content types.
After looking at the faustjs docs I saw some hooks that can help you get the typename using Next but none for Nuxt as the doc doesn't seem to exist yet.

Is there a way to retrieve the typename in Nuxt ?

I thought about storing the value we get from the middleware when wordpress redirects us to a path like : http://localhost:3000/?p=225&preview=true&previewPathname=%2F%3Fp%3D225&typeName=Post

But it doesn't seem to work as it appears the middleware is never called when I click on the preview button in wordpress admin

export default  defineNuxtRouteMiddleware(async (to, from) => {
    if (to.query.preview === 'true') {
        const appStore = useAppStore()
        const config = useRuntimeConfig();
        const previewId = to.query.p;
        const typeName = to.query.typeName;

        console.log('query middleware : ' , to.query) // never triggers
        console.log('typename middleware : ' , typeName) // never triggers
        appStore.setPreviewType(typeName)



        return navigateTo(`${config.public.wordpressUrl}/generate?redirect_uri=${config.public.frontendSiteUrl}/preview?preview_id=${previewId}`, {external: true})
    }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ✅ Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions