fix(realtime-prompt): React example awaits inside a non-async effect

#51204 · open · 1 comments

View on GitHub ↗

coygeek

## Summary The React Pattern in `examples/prompts/use-realtime.md` uses `await supabase.realtime.setAuth()` directly inside the synchronous callback passed to `useEffect`. The supplied JavaScript fails syntax checking before it can authenticate or subscribe. ## Steps to reproduce 1. Open the [React Pattern](https://github.com/supabase/supabase/blob/4ab54b935919ba3f4bb92f436ae4e757108e4f8a/examples/prompts/use-realtime.md#L137-L165) and copy the fenced JavaScript block into `react-example.js`. 2. Run `node --check react-example.js` with Node.js v22.23.2. No React installation, credentials, or Supabase connection is required for this parse-only check. 3. Observe the error at the `await` statement: ```text SyntaxError: await is only valid in async functions and the top level bodies of modules ``` The relevant fragment is: ```javascript useEffect(() => { // Set auth before subscribing await supabase.realtime.setAuth() }, [roomId]) ``` ## Expected behavior The documented React pattern parses as JavaScript, completes authentication before subscribing, and returns React's synchronous cleanup callback. Asynchronous setup should preserve cleanup if the component unmounts during setup. ## Actual behavior The effect callback is not async, but contains `await`. Node's syntax check rejects the exact fenced block. Simply making the effect callback async would return a Promise instead of the cleanup function React expects. ## Affected area `examples/prompts/use-realtime.md`, React Pattern, lines 142-164 at revision `4ab54b935919ba3f4bb92f436ae4e757108e4f8a`. ## Runtime or environment Node.js v22.23.2, syntax checking only. The same revision was the repository's `master` head when inspected. This report does not claim to have run a React application or contacted a Supabase project. ## Evidence - [Synchronous effect callback and invalid await](https://github.com/supabase/supabase/blob/4ab54b935919ba3f4bb92f436ae4e757108e4f8a/examples/prompts/use-realtime.md#L142-L164). - The exact published block produces the syntax error above with `node --check`. ## Impact Users copying the example and agents using this prompt receive code that cannot parse. The subscription and cleanup logic cannot run. ## Additional context The closure check is that the revised example passes syntax checking and a React verification shows authentication before subscription plus cleanup on unmount, including unmount during asynchronous setup. The report concerns this React pattern only; it is independent of the SQL examples elsewhere in the prompt.

Comments

zoubingwu

I noticed the same Realtime prompt is also maintained separately in [apps/studio/lib/ai/prompts.ts](https://github.com/supabase/supabase/blob/master/apps/studio/lib/ai/prompts.ts) as `REALTIME_PROMPT`, which is consumed by the Studio AI Realtime tool. The React issue reported here still exists in that copy, and the malformed trigger examples from #51205 are duplicated there as well. From the repository history, `examples/prompts/use-realtime.md` was originally added in #38220, while the Studio prompt was added later in #39145, so these appear to be independently maintained copies rather than one being generated from the other. That seems to create a broader maintenance issue: fixes to the public prompt can leave the Studio AI prompt stale. It may be worth either keeping both copies updated as part of these fixes, or adding a follow-up to establish a shared source / sync check so they cannot drift again.