Skip to content

Internal Server Error on Cloudflare with @sveltejs/adapter-cloudflare from v7.1.0 #3157

@flayks

Description

@flayks

Describe the bug

From the version 7.1.0 of @sveltejs/adapter-cloudflare, using the svelte-loader library returns a Internal Server Error 1101 on Cloudflare.

Version 7.0.5 works fine.

See this issue I made for further detail. I clearly identified that using this library causes the issue:
sveltejs/kit#14027 (comment)

As stated by a Svelte maintainer, the major change since version 7.1.0 is this:

The biggest change to the worker was in https://github.com/sveltejs/kit/pull/13859/files#diff-9956f0811d38fc2939d554ee334ccda62ba5c4697329b42d20d304730b0c0304 which moved the server initialisation outside the fetch handler and passes instances of env to the server at the top-level. Therefore, it might be worth checking if there's some logic in the init hook that's trying to access env. However, we might need to move it back into the fetch handler so that code inside the hook can access bindings

Workers do not allow I/O from outside a request context. This means that even though env is accessible from the top-level scope, you will not be able to access every binding's methods.

For instance, environment variables and secrets are accessible, and you are able to call env.NAMESPACE.get to get a Durable Object stub in the top-level context. However, calling methods on the Durable Object stub, making calls to a KV store, and calling to other Workers will not work.

https://developers.cloudflare.com/workers/runtime-apis/bindings/#importing-env-as-a-global

A workaround, found by Sonnet 4, is to not import the server client at top level and instead use it in a SvelteKit hook handler with async import. This seems to do the job but this is not ideal. It should work how it's supposed to be from the README file.

To Reproduce

Use SvelteKit using the Cloudflare adapter v7.1.0+ and the svelte-loader library

Expected behavior

It should act the same from v.7.0.5 to 7.1.0+

Screenshots

Image

Which versions of Sanity are you using?

@sanity/cli (global) 4.1.1 (up to date)
@sanity/code-input 5.1.3 (up to date)
@sanity/eslint-config-studio 5.0.2 (up to date)
@sanity/orderable-document-list 1.4.0 (up to date)
@sanity/vision 4.1.1 (up to date)
sanity 4.1.1 (up to date)

What operating system are you using?

macOS, but hosted on Cloudflare Pages

Which versions of Node.js / npm are you running?

11.4.2
v22.16.0
same on server

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