React Native Web Support
React Native for Web enables developers to use the same React Native codebase for both mobile and web platforms.
Support for React Native Web is available since versions 1.12.1 of the PowerSync React Native SDK and 1.8.0 if the JavaScript/Web SDK, and is currently in a beta release.
A demo app showcasing this functionality is available here:
powersync-js/demos/react-native-web-supabase-todolist
Configuring PowerSync in your React Native for Web project
To ensure that PowerSync features are fully supported in your React Native Web project, follow the below steps. This documentation covers necessary web worker configurations, database instantiation, and multi-platform implementations.
1. Install Web SDK
The PowerSync Web SDK, alongside the PowerSync React Native SDK, is required for Web support.
See installation instructions here.
2. Configure Web Workers
For React Native for Web, workers need to be configured when instantiating PowerSyncDatabase
. An example of this is available here.
To do this, copy the contents of node_modules/@powersync/web/dist
to the root of your project (typically in the public
directory). To make it easier to manage these files in the public
directory, it is recommended to place the contents in a nested directory like @powersync
.
You can run the following bash command to automate the copying process. It will create copy the contents to /public/@powersync
.
3. Instantiate Web Workers
The example below demonstrates how to instantiate the workers (PowerSync requires a database and a sync worker) when instantiating PowerSyncDatabase
. You can either specify a path to the worker (they are available in the worker
directory of the dist
contents), or provide a factory function to create the worker.
This PowerSyncDatabaseWeb
database will be used alongside the native PowerSyncDatabase
to support platform-specific implementations. See the Instantiating PowerSync section below for more details.
4. Enable multiple platforms
To target both mobile and web platforms, you need to adjust the Metro configuration and handle platform-specific libraries accordingly.
Metro config
Refer to the example here. Setting config.resolver.resolveRequest
allows Metro to behave differently based on the platform.
Implementations
Many react-native
and web
packages are implemented with only their specific platform in mind, as such there may be times where you will need to evaluate the platform and provide alternative implementations.
Instantiating PowerSync
The following snippet constructs the correct PowerSyncDatabase
depending on the platform that the code is executing on.
Implementations that don’t support both mobile and web
Which can then be used agnostically in a component.
5. Configure UMD target
React Native Web requires the UMD target of @powersync/web
(available at @powersync/web/umd
). To fully support this target version, configure the following in your project:
- Add
config.resolver.unstable_enablePackageExports = true;
to yourmetro.config.js
file. - TypeScript projects: In the
tsconfig.json
file specify themoduleResolution
to beBundler
.
Was this page helpful?