Skip to content

Can I use this to cache client loader data too? #6

Description

@kiastorm

This is working great for server data - but in my usecase I am fetching data in the client loader that I also want to cache. Below is some rough code of what I'm trying to do - is this possible?

export async function clientLoader(args: ClientLoaderFunctionArgs) {
	const cache = await cacheClientLoader(args, {
		type: 'swr',
	})

	if (cache.whiteboards) {
		console.log(cache)
		return cache
	}

	const { serverLoader } = args
	const server = (await serverLoader()) as {}

	const whiteboards = await getAllLocalWhiteboards()

	return {
		...server,
		whiteboards,
	}
}

Activity

  1. AlemTuzlak commented on Sep 17, 2024

    @AlemTuzlak
    Contributor

    yes it is possible, there are concepts of adapters which you can use to cache the data, for example the library exports a cache export which is used by default and allows you to setItem in it, for example you could do:

    export async function clientLoader(args: ClientLoaderFunctionArgs) {
           // cache from the server 	loader
           const server = await cacheClientLoader(args, {
    		type: 'swr',
    	})
            // your code that is caching and fetching something on the client
    	const whiteboards = cache.getItem("whiteboards") ? cache.getItem("whiteboards") :  await getAllLocalWhiteboards() 
    	 
    	return {
    		...server,
    		whiteboards,
    	}
    }
  2. locked and limited conversation to collaborators on Sep 17, 2024
  3. converted this issue into a discussion #12 on Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions