Skip to content

Passing $state wrapped variables to native code bridge causes DataCloneError: The object can not be cloned #17185

@jamestinman

Description

@jamestinman

Describe the bug

This took me a while to figure out this morning (LLMs also have no idea)

When passing Svelte 5 $state variables to native bridged code (via Capacitor in this instance) you can get a "DataCloneError: The object can not be cloned." error on iOS

Reproduction

import { AdMob, BannerAdSize } from '@capacitor-community/admob';
bannerOptions = $state({
        adId: unitId,
        adSize: BannerAdSize.ADAPTIVE_BANNER,
        position: BannerAdPosition.BOTTOM_CENTER
      });
 await AdMob.showBanner(bannerOptions);

// Work around, use spread operator (or $state.snapshot):
await AdMob.showBanner({ ...bannerOptions });

Logs

[Error] Unhandled Promise Rejection: DataCloneError: The object can not be cloned.
	postMessage
	postToNative (user-script:2:847)
	(anonymous function) (user-script:2:904)
	(anonymous function) (user-script:2:993)
	Promise
	(anonymous function) (user-script:2:992)
	(anonymous function)

System Info

System:
    OS: macOS 15.7.1
    CPU: (14) arm64 Apple M3 Max
    Memory: 602.67 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.19.0
    Yarn: 1.22.22
    npm: 11.6.1
  Browsers:
    Chrome: 142.0.7444.162
    Firefox: 143.0.1
    Safari: 26.0.1
  npmPackages:
    svelte: ^5.0.0-next.1 => 5.16.2

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions