Skip to content

Commit 9ee4934

Browse files
Adds context to platform in SvelteKit endpoints (#3868)
* Added context to platform in endpoints with adapter-cloudflare. * update docs * Update sharp-moles-confess.md Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent ad603e7 commit 9ee4934

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.changeset/sharp-moles-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-cloudflare': patch
3+
---
4+
5+
Add `context` to `event.platform` object

packages/adapter-cloudflare/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When configuring your project settings, you must use the following settings:
5353
5454
## Environment variables
5555

56-
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV namespaces etc, is passed to SvelteKit via the `platform` property, meaning you can access it in hooks and endpoints:
56+
The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object, containing KV namespaces etc, is passed to SvelteKit via the `platform` property along with `context`, meaning you can access it in hooks and endpoints:
5757

5858
```diff
5959
// src/app.d.ts
@@ -64,6 +64,9 @@ declare namespace App {
6464
+ env: {
6565
+ COUNTER: DurableObjectNamespace;
6666
+ };
67+
+ context: {
68+
+ waitUntil(promise: Promise<any>): void;
69+
+ }
6770
+ }
6871

6972
interface Session {}

packages/adapter-cloudflare/files/worker.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export default {
99
/**
1010
* @param {Request} req
1111
* @param {any} env
12+
* @param {any} context
1213
*/
13-
async fetch(req, env) {
14+
async fetch(req, env, context) {
1415
const url = new URL(req.url);
1516

1617
// static assets
@@ -49,7 +50,7 @@ export default {
4950

5051
// dynamically-generated pages
5152
try {
52-
return await app.render(req, { platform: { env } });
53+
return await app.render(req, { platform: { env, context } });
5354
} catch (e) {
5455
return new Response('Error rendering route: ' + (e.message || e.toString()), { status: 500 });
5556
}

0 commit comments

Comments
 (0)