firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

setDoc fails to resolve after page refresh

Open builtbyjavi opened this issue 7 months ago • 7 comments

Operating System

Windows 11

Environment (if applicable)

node: 22, next: 12.3.4

Firebase SDK Version

11.7.1

Firebase SDK Product(s)

Firestore

Project Tooling

React / Typescript

Detailed Problem Description

I am trying to set data in Firestore based on a unique identifier in the url. I have a React hook that listens to Next.js Router.events and calls an update callback function on routeChangeComplete. When I navigate through my app, the related data I need is correctly added to Firestore; however, when I refresh the page the setDoc function hangs (i.e. the following line is never executed). I currently have a hacky workaround by introducing a small timeout around setDoc and I am really not sure why this works? Emphasis on this only happening when I refresh the page.

Steps and code to reproduce issue

useEffect(() => {
    syncData(window.location.href);

    Router.events.on('routeChangeComplete', syncData);

    return () => {
      Router.events.off('routeChangeComplete', syncData);
    };
}, []);


//NOT-WORKING (but should)
const syncData = useCallback(
    async (data) => {
        if (!documentReference) {
          return;
        }

       const firebaseFirestoreModule = await import('firebase/firestore');

        const { setDoc } = firebaseFirestoreModule;

        // we make it to here with the correct data
        await setDoc(documentReference, data, { merge: true });
        
        console.log('This is never reached and firestore document is never written to.');
    },
    [documentReference],
 );

//WORKING (but ugly)
const syncData= useCallback(
   async (data) => {
      return new Promise<void>((resolve) => {
        if (!documentReference) {
          resolve();
          return;
        }

       const firebaseFirestoreModule = await import('firebase/firestore');

        const { setDoc } = firebaseFirestoreModule;

        setTimeout(async () => {
          await setDoc(documentReference, data, { merge: true });
          resolve();
        }, 25);
      });
    },
    [documentReference],
  );

builtbyjavi avatar May 13 '25 23:05 builtbyjavi

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar May 13 '25 23:05 google-oss-bot

Hi @builtbyjavi ,

Could you please provide the debug log when the setData hangs?

cherylEnkidu avatar May 16 '25 17:05 cherylEnkidu

Hey @builtbyjavi. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar May 23 '25 01:05 google-oss-bot

@cherylEnkidu here are the logs as requested. The relevant logs in this case are those regarding SessionStorage this is the document that is being written to.

This is the log file without the workaround that fails to write data to the SessionStorage document. setDoc-debug.log

This is the log file with the workaround timeout that successfully writes data to the SessionStorage document. setDoc-debug-workaround.log

builtbyjavi avatar May 23 '25 18:05 builtbyjavi

hi @cherylEnkidu just checking in, any updates on this?

builtbyjavi avatar May 28 '25 02:05 builtbyjavi

The team is taking a look at this ticket, we will update the ticket once we make any progress

cherylEnkidu avatar Jun 12 '25 18:06 cherylEnkidu

@cherylEnkidu thanks! For a bit more context it seemed that auth.currentUser was not defined. I was calling setDoc "too soon" after refreshing the page? This would have been a lot easier to diagnose if an error was thrown instead of the current behavior where nothing happens.

builtbyjavi avatar Jun 13 '25 00:06 builtbyjavi

Hi @builtbyjavi ,

I tried to repo it without using the React framework but it went through. When I put your code into a React App, it failed with different error related to data format. Can you provide a minimum repo app for us to continue the investigation?

cherylEnkidu avatar Jun 17 '25 03:06 cherylEnkidu

It would also be helpful if you can provide a short video for refreshing the page.

cherylEnkidu avatar Jun 17 '25 03:06 cherylEnkidu

Hey @builtbyjavi. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

google-oss-bot avatar Jun 24 '25 01:06 google-oss-bot

Since there haven't been any recent updates here, I am going to close this issue.

@builtbyjavi if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

google-oss-bot avatar Jul 01 '25 01:07 google-oss-bot