Skip to content

Commit

Permalink
Add Netlify Functions Context to Netlify Adapter (#4145)
Browse files Browse the repository at this point in the history
* 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 <hello@rich-harris.dev>
  • Loading branch information
jsandler18 and Rich-Harris authored Feb 28, 2022
1 parent 9d3b6c4 commit 8c08aa5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-pigs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

Add Netlify Functions context as `event.platform.context`
4 changes: 3 additions & 1 deletion packages/adapter-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-netlify/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8c08aa5

Please sign in to comment.