Files
turso/examples/javascript/sync-wasm-vite
dependabot[bot] c8e2b844ad build(deps-dev): bump source-map-js
Bumps [source-map-js](https://github.com/7rulnik/source-map-js) from 1.2.1 to 1.2.2.
- [Release notes](https://github.com/7rulnik/source-map-js/releases)
- [Changelog](https://github.com/7rulnik/source-map-js/blob/main/CHANGELOG.md)
- [Commits](https://github.com/7rulnik/source-map-js/compare/v1.2.1...v1.2.2)

---
updated-dependencies:
- dependency-name: source-map-js
  dependency-version: 1.2.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-10-14 07:15:20 +00:00
..

sync-wasm-vite

This is a minimal example showing how to use @tursodatabase/sync-wasm in the browser with Vite.

The @tursodatabase/sync-wasm package extends a regular database with bidirectional synchronization between a local file and a remote Turso Cloud database.

It allows you to:

  • pull(): fetch and apply remote changes into your local database. This ensures your local state reflects the latest server-side updates.

  • push(): upload local changes back to the remote database. This is useful when you’ve made inserts/updates locally and want to replicate them to the cloud.

Together, these methods make it possible to work offline-first (mutating the local database) and later sync your changes to Turso Cloud, while also bringing in any updates from other clients.


Usage

npm install

export VITE_TURSO_AUTH_TOKEN=$(turso db tokens create <db-name>) # create auth token for the database in the Turso Cloud
export VITE_TURSO_DATABASE_URL=$(turso db show <db-name> --url) # fetch URL for the database in the Turso Cloud

npm run dev
# or build assets and serve them with simple node.js server
npm run build
npm run serve

⚠️ Warning: When using VITE_TURSO_AUTH_TOKEN in the browser, this token will be bundled into your client-side code.
That means it is publicly visible to anyone who loads your site.

  • Do not treat this token as a secret.
  • Only use it with databases or roles that are safe to expose (e.g., read-only, demo instances).

Important: COOP / COEP Headers

Because @tursodatabase/database-wasm relies on SharedArrayBuffer, you need Cross-Origin headers in development and production:

Vite dev server config (vite.config.js)

import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    headers: {
      'Cross-Origin-Opener-Policy': 'same-origin',
      'Cross-Origin-Embedder-Policy': 'require-corp',
    }
  }
})

Static production server (server.mjs)

When serving the dist/ build, also make sure your server sets these headers:

res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");

Vercel deployment

If you deploy to Vercel, add a vercel.json file to ensure COOP / COEP headers are set:

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "Cross-Origin-Opener-Policy", "value": "same-origin" },
        { "key": "Cross-Origin-Embedder-Policy", "value": "require-corp" }
      ]
    }
  ]
}