Get clientIp during build in sveltekit is returing an error "can't get clientip during prerendering"

57 views Asked by At

I'm attempting to get the clientip inside hooks.server.js in sveltekit app for the first time. I get an error "Cannot read clientAddress during prerendering "

My hooks.server.js file is basic :

import { sequence } from '@sveltejs/kit/hooks'; 
export const prerender = false;

export const runauthjs = SvelteKitAuth({
    providers: [
        EmailProvider({" config "}),
        AppleProvider({clientId : "", clientSecret : ""})
    ],
});

export const getip = async ({ event, resolve }) => {
    
    let requestIp = event.getClientAddress(); // IP from Client Request
    console.log('IP Address from Client Request: ', requestIp);
  
    const response = await resolve(event);
    return response;

  };


  export async function handleError({ error, event, status, message }) {

    console.log("error : ", error )

  }

  export const handle = sequence(runauthjs, getip);

When I use : npm run build I get an error : error : Error: Cannot read clientAddress during prerendering

I moved the get ip function to layout.server.js, same message.

In dev, everything is working as expected. But I'm unable to get the build to complete. It throw the aforementioned error.

I need the ip to lookup user location.

How do I get the clientip and get the app to build?

0

There are 0 answers