Skip to content

Allow custom worker script when deploying to Cloudflare #13692

@victormihalache

Description

@victormihalache

Describe the problem

When deploying to Cloudflare there's no way to customize the worker. This means no Durable Objects, no RPC methods, no CRON jobs, just the fetch() handler.

Describe the proposed solution

Currently when building with the adapter-cloudflare the entry Worker file gets generated at .svelte-kit/cloudflare/_worker.js. At its core it just exports the fetch handler for the Worker.

// other stuff...

var worker_default = {
  async fetch(req, env, context) {
    // request handling...
  }
};
export {
  worker_default as default
};

Other frameworks like React Router create a workers folder in the root directory of the project containing app.ts—the worker script. It looks like this:

import { createRequestHandler } from "react-router";

declare module "react-router" {
  export interface AppLoadContext {
    cloudflare: {
      env: Env;
      ctx: ExecutionContext;
    };
  }
}

const requestHandler = createRequestHandler(
  () => import("virtual:react-router/server-build"),
  import.meta.env.MODE
);

export default {
  async fetch(request, env, ctx) {
    return requestHandler(request, {
      cloudflare: { env, ctx },
    });
  },
} satisfies ExportedHandler<Env>;

React Router basically provides a handler you can use in your worker in the fetch(). Why wouldn't SvelteKit be able to provide something similar?

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions