Skip to content

setDoc fails to resolve after page refresh #9038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
builtbyjavi opened this issue May 13, 2025 · 5 comments
Open

setDoc fails to resolve after page refresh #9038

builtbyjavi opened this issue May 13, 2025 · 5 comments

Comments

@builtbyjavi
Copy link

builtbyjavi commented May 13, 2025

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 builtbyjavi added question new A new issue that hasn't be categoirzed as question, bug or feature request labels May 13, 2025
@google-oss-bot
Copy link
Contributor

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

@jbalidiong jbalidiong added Repro Needed api: firestore needs-attention and removed needs-triage new A new issue that hasn't be categoirzed as question, bug or feature request labels May 14, 2025
@cherylEnkidu
Copy link
Contributor

Hi @builtbyjavi ,

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

@google-oss-bot
Copy link
Contributor

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!

@builtbyjavi
Copy link
Author

@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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants