Skip to content

Commit

Permalink
Adds context to platform in SvelteKit endpoints (#3868)
Browse files Browse the repository at this point in the history
* Added context to platform in endpoints with adapter-cloudflare.

* update docs

* Update sharp-moles-confess.md

Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
isaac-mcfadyen and Rich-Harris authored Feb 14, 2022
1 parent ad603e7 commit 9ee4934
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-moles-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-cloudflare': patch
---

Add `context` to `event.platform` object
5 changes: 4 additions & 1 deletion packages/adapter-cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ When configuring your project settings, you must use the following settings:
## Environment variables

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:
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:

```diff
// src/app.d.ts
Expand All @@ -64,6 +64,9 @@ declare namespace App {
+ env: {
+ COUNTER: DurableObjectNamespace;
+ };
+ context: {
+ waitUntil(promise: Promise<any>): void;
+ }
+ }

interface Session {}
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare/files/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export default {
/**
* @param {Request} req
* @param {any} env
* @param {any} context
*/
async fetch(req, env) {
async fetch(req, env, context) {
const url = new URL(req.url);

// static assets
Expand Down Expand Up @@ -49,7 +50,7 @@ export default {

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

0 comments on commit 9ee4934

Please sign in to comment.