From 8c08aa505a0b5238cdc2fbe8408b89ff495da1f7 Mon Sep 17 00:00:00 2001 From: Jake Sandler Date: Mon, 28 Feb 2022 10:11:48 -0500 Subject: [PATCH] Add Netlify Functions Context to Netlify Adapter (#4145) * Add Netlify Functions Context to Netlify Adapter * change to patch and update readme * fix lint * Update packages/adapter-netlify/README.md * Update many-pigs-deliver.md Co-authored-by: Rich Harris --- .changeset/many-pigs-deliver.md | 5 +++++ packages/adapter-netlify/README.md | 4 +++- packages/adapter-netlify/src/handler.js | 4 ++-- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/many-pigs-deliver.md diff --git a/.changeset/many-pigs-deliver.md b/.changeset/many-pigs-deliver.md new file mode 100644 index 000000000000..4617dfa1af8a --- /dev/null +++ b/.changeset/many-pigs-deliver.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-netlify': patch +--- + +Add Netlify Functions context as `event.platform.context` diff --git a/packages/adapter-netlify/README.md b/packages/adapter-netlify/README.md index 52979b7bc155..8ba42157d3f0 100644 --- a/packages/adapter-netlify/README.md +++ b/packages/adapter-netlify/README.md @@ -57,7 +57,9 @@ During compilation, redirect rules are automatically appended to your `_redirect ### Using Netlify Functions -[Netlify Functions](https://docs.netlify.com/functions/overview/) can be used alongside your SvelteKit routes. If you would like to add them to your site, you should create a directory for them and add the configuration to your `netlify.toml` file. For example: +With this adapter, SvelteKit endpoints are hosted as [Netlify Functions](https://docs.netlify.com/functions/overview/). Netlify function handlers have additional context, including [Netlify Identity](https://docs.netlify.com/visitor-access/identity/) information. You can access this context via the `event.platform.context` field inside your hooks and endpoints. + +Additionally, you can add your own Netlify functions by creating a directory for them and adding the configuration to your `netlify.toml` file. For example: ```toml [build] diff --git a/packages/adapter-netlify/src/handler.js b/packages/adapter-netlify/src/handler.js index 13eb9175c680..160706c1be2e 100644 --- a/packages/adapter-netlify/src/handler.js +++ b/packages/adapter-netlify/src/handler.js @@ -9,8 +9,8 @@ import { split_headers } from './headers'; export function init(manifest) { const server = new Server(manifest); - return async (event) => { - const rendered = await server.respond(to_request(event)); + return async (event, context) => { + const rendered = await server.respond(to_request(event), { platform: { context } }); const partial_response = { statusCode: rendered.status,