Next.js Discord

Discord Forum

Correctly fetch data from public dir in server component and then client component

Unanswered
Ovenbird posted this in #help-forum
Open in Discord
OvenbirdOP
Helle people, I am try to find the way to correctly fetch data in server component and then client component from the client component. Actually I am doing that.

Some code:
In the server component:
export default async function ServerComponentParent() {
  await getDataOnServer('/data/data1.bin');
  await getDataOnServer('/data/data2.bin');

  return (
    <AnotherClientComponent />
  );
}


Then in the client component I fetch again to use the cache system.
export default function TheClientComponent {
  const data1 = useData('/data/data1.bin');
  const data2 = useData('/data/data2.bin');
  
  return (...);
}


useData() is a hook to fetch data

export function useData(url: string) {
  const [data, setData] = useState<ArrayBuffer | null>();

  useEffect(() => {
    let ignore = false;

    async function fetchData() {
      const result = await getData(url);
      if (!ignore) {
        setData(result);
      }
    }

    fetchData();

    return () => {
      ignore = true;
    }
  }, []);

  return data;
}


getData() is the fetch function

export const getData = async (url: string) => {
  try {
    const data = await fetch(url, { next: { revalidate: false } });
    const arrayBuffer = await data.arrayBuffer();
    return arrayBuffer;
  } catch (error) {
    console.error('Error fetching static data:', error);
    throw error;
  }
};


Some details:
1. Data are static and immutable, so I do not need to revalidate them.
2. I do not want to include data in the initial bundle, that's why I am fetching them.
3. I do not need data in the server component and nowhere else but just in the client component (only one).
4. Actually the client component that consume data it is not a direct children of the server component, so I want to avoid props drilling.
5. The server component where I fetching data is the closer to the client component.
6. I am using nextjs 14.2.7 with app routing.

Continue..

2 Replies

OvenbirdOP
Questions:
1. is it okay to fetch from the public dir or in general is not recommended? if it is discouraged, what is the alternative?
2. doing like that I am using the ability offer by next to use the cache and not a new re-fetch?
3. My initial idea was to read the file on the server component the file, but then the only way to get the data in the client component is with props drilling (or a global state). In additional data are included in the initial bundle if I read them?
4. I I use fetch in the server component and SWR in the client component, do both use the same cache? I think not but wanna be sure.
OvenbirdOP
I tried reading in the server component the file and pass down to the client compent (actualy to avoid prop drilling with jotai), but both ArrayBuffer and Float32Array are not serializable, so I do not know what is the best approach.
1. Fetching in the client component the ArrayBuffer and create the Float32Array.
2. Read the file in server component, encode to base64 and pass down to the client component. In th client component decode from base64 to ArrayBuffer and create the Float32Array.
3. Read the ArrayBuffer in the server component, create a simple Array and then pass down the client component. In the client component create the Float32Array (typed array) from the simple array.

What could be the best approach?

Files are 61kb each one.