Skip to content

Caching when using Firebase Admin SDK #52267

Answered by imhardikdesai
HStromfelt asked this question in Help
Discussion options

You must be logged in to vote

@HStromfelt
Next.js offers an experiential cache function designed to write to the Next.js Data Cache, functioning similarly to the extended fetch API.

import { unstable_cache as cache } from "next/cache";

const getUserDetailsByUsernameImpl = async (username: string): Promise<{ userDetails: any; message?: string }> => {
  const q = DB.collection('users').where('username', '==', username)
  const querySnapshot = await q.get()
  if (querySnapshot.empty) {
    return { userDetails: null, message: 'User not found' }
  }

  const userDetails = querySnapshot.docs[0].data()
  return { userDetails }
}

const getUserDetailsByUsername = cache(
  /* fetch function */ getUserDetailsByUsernameImpl,
  /…

Replies: 4 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@HStromfelt
Comment options

@ben519
Comment options

@HStromfelt
Comment options

@ben519
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@HStromfelt
Comment options

Answer selected by HStromfelt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
5 participants