Skip to content
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

Bug: If project sources are minified, the wrong table/reducer class name is registered #31

Open
ewgenius opened this issue Dec 31, 2023 · 1 comment

Comments

@ewgenius
Copy link

ewgenius commented Dec 31, 2023

The issue is that the registerTables and registerReducers methods rely on the .name attribute of the generated classes in the bindings. I'd recommend to use .tableName (I'm not sure though if that suitable) or add a separate static field .className instead of relying on JavaScript sources that may change during bundling and minification.

Ib my case, I'm configuring the Spacetime SDK in a Next.js project which has code minification enabled, and the class names in the bindings are mangled which causes missing class error:

Uncaught (in promise) Could not find class "World", you need to register it with SpacetimeDBClient.registerTable() first

CleanShot 2023-12-31 at 14 48 13@2x

CleanShot 2023-12-31 at 14 50 09@2x

@ewgenius
Copy link
Author

I didn't want to disable project minification. Instead, I managed to work around it by extending the client with additional methods in my code:

import {
  type DatabaseTableClass,
  type ReducerClass,
  SpacetimeDBClient as SpacetimeDBClientBase,
} from "@clockworklabs/spacetimedb-sdk";

export class SpacetimeDBClient extends SpacetimeDBClientBase {
  static registerNamedTable(name: string, table: DatabaseTableClass) {
    (this as any).tableClasses.set(name, table);
  }

  static registerNamedTables(tables: Record<string, DatabaseTableClass>) {
    for (const [name, table] of Object.entries(tables)) {
      (this as any).tableClasses.set(name, table);
    }
  }

  static registerNamedReducer(name: string, reducer: ReducerClass) {
    (this as any).reducerClasses.set(name, reducer);
  }

  static registerNamedReducers(reducers: Record<string, ReducerClass>) {
    for (const [name, table] of Object.entries(reducers)) {
      (this as any).reducerClasses.set(name, table);
    }
  }
}

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

No branches or pull requests

1 participant