NuxtN
Nuxt12mo ago
Solid

prevent useAsyncData from running in the browser

I am using useAsyncData to see if the user has permission to view the page. I have created a helper function that assists in doing so. It simply returns a promise evaluating to a boolean if the user has permission or not. This logic uses sequelize to make a database request in order to see if the user has permission.

I have noticed that useAsyncData also executes on the client. This results in the client calling sequelize functions which relies on nodejs which makes the browser spit an error.
500 buffer not defined
I have tried using import.meta to prevent the code from executing on the client but o import.meta variables would work and it would execute in the browser anyway.

This is the current code
<script setup>
    import { getSessionAllowedView } from '~/server/utils/session';
    const config = useRuntimeConfig()
    const session = useCookie('session', {sameSite: 'strict',domain:config.public.HOST_DOMAIN,secure:false})

    const {data, status} = await useAsyncData('protectedPage', async ()=>{
        if(import.meta.nitro||import.meta.dev||import.meta.server){
            return getSessionAllowedView(session.value,'pages_protected_view',1).then((res)=>{console.log(res);return res})
        }
    })
</script>
Was this page helpful?