Only use +page.server.svelte
on server side render
#8178
-
Hi all. First off congrats on the v1 release! I've recently migrated an app (https://github.com/jloh/geojs-app) over to v1 and am having trouble migrating after the removal of For my app to correctly render server side (which I'd like!) I need to get the end users IP address. Previously I could use /** @type {import('./$types').PageServerLoad} */
export async function load(event) {
return {
clientIP: event.getClientAddress()
};
} Then my /** @type {import('./$types').PageLoad} */
import { browser } from '$app/environment';
export const prerender = false;
export async function load({ fetch, data }) {
const { clientIP } = data;
var geo, ptr;
if (browser) {
geo = 'https://get.geojs.io/v1/ip/geo.json';
ptr = 'https://get.geojs.io/v1/dns/ptr.json';
} else {
geo = `https://get.geojs.io/v1/ip/geo/${clientIP}.json`;
ptr = `https://get.geojs.io/v1/dns/ptr/${clientIP}.json`;
}
const geoResponse = await fetch(geo);
const ptrResponse = await fetch(ptr);
return {
geoLookup: geoResponse.ok && (await geoResponse.json()),
ptrLookup: ptrResponse.ok && ptrResponse.json()
};
} This works and when running server side I correctly request the requesting users IP. The problem is if someone navigates away then back to the homepage What I'm wondering is can I get the clients address in a normal Hopefully this makes sense and thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think the problem is that the page load function runs everytime the page is loaded. Regardless of CSR or SSR. The load function would always be called. Why dont you instead send these requests from the server and return the responses to the client to be handled or just use a hook? |
Beta Was this translation helpful? Give feedback.
-
I think this is similar to a request I made a while ago #6566, I think there's definitely a desire and use case for this, lower level control over how the load functions work. |
Beta Was this translation helpful? Give feedback.
I think the problem is that the page load function runs everytime the page is loaded. Regardless of CSR or SSR. The load function would always be called. Why dont you instead send these requests from the server and return the responses to the client to be handled or just use a hook?