How to ensure a Sveltekit project can actually run on Cloudflare workers?

1.5k views Asked by At

Sveltekit and Cloudflare seem to be a great match. My objective is to deliver well-tested code, that runs smoothly on Cloudflare pages/workers. To do this, I use Cypress.io for e2e testing. The problem is that I can't trust the tests running locally, as using any Node.js API works fine locally (for example Buffer.from). But this code breaks the site when deploying to Cloudflare.

How can I run the Sveltekit project locally to ensure it will work once deployed?

As a bonus, would it somehow be possible to configure the typescript project so it would throw errors when accessing Node.js API's? According to this Cloudflare page, workers are running a v8 instance. We are using the "official" adapter.

Thanks for your help.

1

There are 1 answers

2
lukeed On

SvelteKit is planning ways to make this easier in the long-term. You can hear Rich talk a bit about this in the most recent Svelte NYC meetup.

In the meantime, your best bet is to include something like a "postbuild" hook that points miniflare to the output file that SK produced. This will ensure that the contents are bootable according to a Cloudflare Workers emulated environment. You can run an e2e test suite against the localhost endpoint it spawns for you, too.