You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I managed to get a cloudflare worker adapter working but it wasn't super smooth. I encountered the following problems, which we need to consider if we want to support javascript runtimes other than nodejs.
Dynamic Require Statements
The current code has a load option that hard coded during build to:
The cloudflare worker environment (which is basically a web worker environment) doesn't support require. To get around this, cloudflare uses webpack, Webpack has issues with resolving that require.
Since we have the full manifest, we can optionally skip the dynamic require and generate code to static require the items.
While cloudflare's builder uses webpack and can include node modules, the current renderer requires some which is node specific (fs, node-fetch, url). Luckily they are confined to the preload_context in renderer/page.js
To work around this there are a couple of options:
Adapters can use webpack's fallback functionality to provide shims
Svelte kit can be documented as explicitly nodejs environments only
We can provide a mechanism for the adapter to inject the required functionality
I had a stab at the third option by introducing the concept of a platform. A platform is a set (currently 3) of methods required by the svelte kit render function. It is passed in as part of the options (2nd parameter) to app.render.
In my implementation, SvelteKit provides a default node platform implementation that is used internally by dev, start, and prerender. When the render function is run from an adapter, the adapter needs to pass in a platform. It can just pass in the node platform:
or provide its own like the cloudflare worker one in the diff, which uses the global versions of response, and fetch available to the web worker scope, and the getAssetFromKV to fetch static files.
The question is, is this something we want to pursue?
One disadvantage is that it will be yet another interface for adapter writers to be aware of, most which will be nodejs based, so this is just pure boilerplate for those.
Its advantage is that it would allow usage in different JS environments, such as web workers or embedded JS engines like in modern TVs. Being able to bundle an app up as a service worker might open up some pretty cool opportunities.
Is there a nicer way to do this?
Prototype:
The changes needed to be made have been prototyped in order to get the cf worker adapter going and can be seen in this commit:
I think that supporting non-node platforms makes sense. There's a lot of code duplication between the adapters at the moment. It would be good to refactor these so that we have base classes or components that can be called. If there's a base class that provides the node platform then I don't think it would be any extra work for the existing adapters.
We may want to consider renaming the existing node-adapter if there's also a node-platform in order to clarify things. It might be better called node-server-adapter or something.
I managed to get a cloudflare worker adapter working but it wasn't super smooth. I encountered the following problems, which we need to consider if we want to support javascript runtimes other than nodejs.
Dynamic Require Statements
The current code has a load option that hard coded during build to:
The cloudflare worker environment (which is basically a web worker environment) doesn't support require. To get around this, cloudflare uses webpack, Webpack has issues with resolving that require.
Since we have the full manifest, we can optionally skip the dynamic require and generate code to static require the items.
Node specific modules
While cloudflare's builder uses webpack and can include node modules, the current renderer requires some which is node specific (
fs
,node-fetch
,url
). Luckily they are confined to thepreload_context
inrenderer/page.js
To work around this there are a couple of options:
I had a stab at the third option by introducing the concept of a
platform
. A platform is a set (currently 3) of methods required by the svelte kit render function. It is passed in as part of the options (2nd parameter) to app.render.In my implementation, SvelteKit provides a default node platform implementation that is used internally by
dev
,start
, andprerender
. When the render function is run from an adapter, the adapter needs to pass in a platform. It can just pass in the node platform:or provide its own like the cloudflare worker one in the diff, which uses the global versions of response, and fetch available to the web worker scope, and the getAssetFromKV to fetch static files.
The Question
The question is, is this something we want to pursue?
One disadvantage is that it will be yet another interface for adapter writers to be aware of, most which will be nodejs based, so this is just pure boilerplate for those.
Its advantage is that it would allow usage in different JS environments, such as web workers or embedded JS engines like in modern TVs. Being able to bundle an app up as a service worker might open up some pretty cool opportunities.
Is there a nicer way to do this?
Prototype:
The changes needed to be made have been prototyped in order to get the cf worker adapter going and can be seen in this commit:
2cac0f7
The text was updated successfully, but these errors were encountered: