Skip to content

Fix TypeRef resolution errors in production mode #3760

@webberwang

Description

@webberwang

Problem

Production mode encounters TypeRef resolution errors when rendering pages:

Error: a reference of type '@codelab/TypeRef' could not resolve an object with id '016048df-df67-4371-9979-6a4570f3ebb4'

This happens because:

  1. Production queries (AtomProduction fragment) don't include the api field to reduce payload size
  2. The renderer code accesses atom API types using .current which throws when the reference can't be resolved
  3. MobX initializes all computed properties even if they're not used, triggering TypeRef access

Root Causes Found

1. propsHaveErrors getter (line 177)

const schema = this.element.current.renderType.current.api.current.toJsonSchema({})
  • Only used in builder UI but MobX initializes it anyway
  • Fixed by using .maybeCurrent instead of .current

2. renderedChildrenProp getter (line 176)

const atomApi = isAtom(this.element.renderType.current)
  ? this.element.renderType.current.api.current  // ❌ Throws error
  : undefined
  • Used to determine if atoms accept children props
  • Fixed by using .api.maybeCurrent instead of .api.current

Solution Implemented

Changed all .current accesses to .maybeCurrent when accessing atom API types. This returns undefined instead of throwing when the reference isn't resolved.

Files Modified

  • libs/frontend/application/renderer/src/store/runtime-element.model.ts

    • Fixed propsHaveErrors getter
  • libs/frontend/application/renderer/src/store/runtime-element-prop.model.ts

    • Fixed renderedChildrenProp getter

Long-term Recommendations

  1. Separate builder-specific logic from core runtime models
  2. Use .maybeCurrent consistently for optional references
  3. Consider loading minimal type data in production (just field names for children detection)
  4. Add ESLint rule to warn about .current usage on type references

Related Discussions

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions