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

UnknownError - sqlite3.oo1.OpfsDb is not a constructor #147

Closed
steida opened this issue May 3, 2023 · 5 comments
Closed

UnknownError - sqlite3.oo1.OpfsDb is not a constructor #147

steida opened this issue May 3, 2023 · 5 comments
Labels
invalid This doesn't seem right

Comments

@steida
Copy link
Contributor

steida commented May 3, 2023

Why?

@steida steida added the invalid This doesn't seem right label May 3, 2023
@steida
Copy link
Contributor Author

steida commented May 3, 2023

You have to add COOP and COEP HTTP Headers as described in https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep

In Next.js, add async headers:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,

  // ADD THIS:
  async headers() {
    return [
      {
        source: "/(.*?)",
        headers: [
          {
            key: "cross-origin-embedder-policy",
            value: "require-corp",
          },
          {
            key: "cross-origin-opener-policy",
            value: "same-origin",
          },
        ],
      },
    ];
  },
};

module.exports = nextConfig;

@steida steida closed this as completed May 3, 2023
@FilipChalupa
Copy link
Contributor

Is there any recommended way to force localStorage instead of opfs in all browsers? I would love to try Evolu but I'm not able to get it working with opfs in our private pnpm monorepo.

Patching Browser.js with false on line 19 to force localStorage works for me but seems very dirty.


For opfs I've tried setting cross-origin-*policy headers in next-config.js, adding "type": "module" to package.json, copypasting your examples/nextjs/tsconfig.json. All without any luck. I'm still getting the sqlite3.oo1.OpfsDb is not a constructor in both Chrome (MS Edge) and Firefox.

@steida
Copy link
Contributor Author

steida commented May 24, 2023

Type module or tsconfig are irrelevant. You have to enable https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep.

Of course, I can add an option to enforce LocalStorage, but OPFS should work, or my detection is wrong. Maybe this will help https://github.com/mandolyte/sqlitenext#opfs_db (there is a link to vercel/next.js#17991

@FilipChalupa
Copy link
Contributor

The problem is that source: "/(.*?)" is not matching root url / and my app runs under /. 😅

Solution:

const headers = [
	{
		key: 'cross-origin-embedder-policy',
		value: 'require-corp',
	},
	{
		key: 'cross-origin-opener-policy',
		value: 'same-origin',
	},
]

const nextConfig = {
  // ADD THIS:
  async headers() {
  	return [
  		{
  			source: '/',
  			headers,
  		},
  		{
  			source: '/(.*?)',
  			headers,
  		},
  	]
  },
};

@steida
Copy link
Contributor Author

steida commented Jun 12, 2023

@FilipChalupa Thank you, it will help others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants