surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
272 stars 45 forks source link

Bug: CF Pages SSR with SurrealJS fails #214

Open itsezc opened 5 months ago

itsezc commented 5 months ago

Describe the bug

Using surrealdb.js on Cloudflare Pages platform with SSR just fails on SvelteKit 2, I have to pass the following to the root layout:

export const csr = true;
export const ssr = false;

for it to work, but this is not ideal as it disables SSR and ISR.

Steps to reproduce

Setup SvelteKit with surrealdb.js and deploy as an SSR site

Expected behaviour

Have SSR working

SurrealDB version

v1.1.1

SurrealDB.js version

0.11.0

Contact Details

No response

Is there an existing issue for this?

Code of Conduct

itsezc commented 3 months ago

While running into the issue with DevTools, heres what you get:

A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
Uncaught (in response) Error: The script will never generate a response.

Upon debugging further:

Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance.
    at _SurrealSocket.send (SurrealSocket.ts:133:12)
#options.hooks.handleError @ index.js:2903
handle_error_and_jsonify @ index.js:174
render_page @ index.js:2090
raymonddaikon commented 2 months ago

I'm having the same issue with CloudFlare workers (via Partykit). Making a db call using the client library hangs indefinitely. I'm not sure whether or not it's an issue with serialization or the ws protocol. A reproduction can be created with a starter template and adding an onStart surrealdb.js call function

  async onStart() {
    this.surrealClient = new Surreal()
    await this.surrealClient.connect(this.room.env.SURREALDB_URL as string, {
      auth: {
        namespace: 'ns',
        database: 'db',
        username: 'root'
        password: 'root',
        scope: undefined,
      },
    })
    await this.surrealClient.query()
  }

anywhere in /party/chatrooms.ts