Getting previous data in firebase 9.0.0 with onSnapshot trigger

I am using firebase and I am trying to get the previous value of the updated data using ‘onSnapshot’ method but I couldn’t find a way. I know that before version 9 of firebase trigger functions like ‘onUpdate’ were returning a change object and we were able to reach to both old and new data from that change object but I couldnt figure out a way in the SDK 9 of firebase. I would appreciate if anyone could help me out. Here is my code snippet:

import { getFirestore, collection, doc, onSnapshot, query } from 'firebase/firestore'
import { initializeApp } from 'firebase/app';

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: ""
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);


/**
 * Trigger function that fires on update of any document on cafe collection
 */
export const onUpdate = async () => {
  
  const cafeCollection = collection(db, 'cafes');
  const q = query(cafeCollection);
  onSnapshot(q, (snapshot) => {
    let changesArr = snapshot.docChanges();
    if(changesArr.length == 1){
      let changes = changesArr[0];
      console.log(changes.type); // added-modified-removed
      console.log(changes.doc.id); // id of the updated doc
      console.log(changes.doc.data()); // newly updated data
    }
    
  })
}

Answer

I know that before version 9 of firebase trigger functions like ‘onUpdate’ were returning a change object

A change object is only available in Cloud Functions, as shown in the documentation on Writing Cloud Firestore-triggered functions. It is not (nor ever was) available in the client-side SDKs.

If you want to compare the current and previous snapshot, you will have to introduce a variable where you keep the previous snapshot yourself.