Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-pens-split.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': minor
---

feat: support using `edge` and `split` together
4 changes: 4 additions & 0 deletions .github/actions/netlify-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ inputs:
netlify-token:
description: 'Netlify authentication token'
required: true
edge:
description: 'Used to enable edge functions for the split test app'
default: false
outputs:
deployment-url:
description: 'URL of the deployed preview'
Expand All @@ -32,6 +35,7 @@ runs:
TEST_APP_DIR: ${{ inputs.test-app-dir }}
NETLIFY_AUTH_TOKEN: ${{ inputs.netlify-token }}
NETLIFY_PROJECT_ID: ${{ inputs.netlify-project-id }}
EDGE: ${{ inputs.edge }}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
run: |
TEST_APP_NAME=$(cat "$TEST_APP_DIR/package.json" | jq -r '.name')
echo "url=$(netlify deploy --json --filter="$TEST_APP_NAME" --dir="$TEST_APP_DIR/build" --functions=".netlify/v1/functions" --auth="$NETLIFY_AUTH_TOKEN" --site="$NETLIFY_PROJECT_ID" | jq -r '.deploy_url')" >> "$GITHUB_OUTPUT"
26 changes: 26 additions & 0 deletions .github/workflows/platform-tests-netlify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,29 @@ jobs:
with:
test-app-dir: ${{ env.TEST_APP_DIR }}
deployment-url: ${{ steps.deploy.outputs.deployment-url }}

test-split-edge:
if: github.repository == 'sveltejs/kit'
runs-on: ubuntu-latest
timeout-minutes: 15
environment: '@sveltejs/adapter-netlify platform tests'
env:
TEST_APP_DIR: packages/adapter-netlify/test/apps/split
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.sha || github.sha }}
persist-credentials: false

- uses: ./.github/actions/netlify-deploy
id: deploy
with:
test-app-dir: ${{ env.TEST_APP_DIR }}
netlify-project-id: ${{ secrets.NETLIFY_PROJECT_ID_SPLIT }}
netlify-token: ${{ secrets.NETLIFY_TOKEN }}
edge: true

- uses: ./.github/actions/platform-test
with:
test-app-dir: ${{ env.TEST_APP_DIR }}
deployment-url: ${{ steps.deploy.outputs.deployment-url }}
2 changes: 0 additions & 2 deletions documentation/docs/25-build-and-deploy/80-adapter-netlify.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ If `true`, your app will be deployed as a [Netlify Edge Function](https://docs.n

If `true`, your app will be split into multiple functions instead of a single one for the entire app.

If `edge` is `true`, this option cannot be used.

## Netlify alternatives to SvelteKit functionality

You may build your app using functionality provided directly by SvelteKit without relying on any Netlify functionality. Using the SvelteKit versions of these features will allow them to be used in dev mode, tested with integration tests, and to work with other adapters should you ever decide to switch away from Netlify. However, in some scenarios you may find it beneficial to use the Netlify versions of these features. One example would be if you're migrating an app that's already hosted on Netlify to SvelteKit.
Expand Down
67 changes: 34 additions & 33 deletions packages/adapter-netlify/files/edge.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import { server } from '0SERVER';
/** @param {import('@sveltejs/kit').Server} server */
export function init(server) {
/**
* We don't know the origin until we receive a request, but
* that's guaranteed to happen before we call `read`
* @type {string}
*/
let origin;

/**
* We don't know the origin until we receive a request, but
* that's guaranteed to happen before we call `read`
* @type {string}
*/
let origin;
const initialized = server.init({
env: Deno.env.toObject(),
read: async (file) => {
const url = `${origin}/${file}`;
const response = await fetch(url);

const initialized = server.init({
env: Deno.env.toObject(),
read: async (file) => {
const url = `${origin}/${file}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`read(...) failed: could not fetch ${url} (${response.status} ${response.statusText})`
);
}

if (!response.ok) {
throw new Error(
`read(...) failed: could not fetch ${url} (${response.status} ${response.statusText})`
);
return response.body;
}
});

return response.body;
}
});

/** @type {import('@netlify/edge-functions').EdgeFunction} */
export default async function handler(request, context) {
if (!origin) {
origin = new URL(request.url).origin;
}
/** @type {import('@netlify/edge-functions').EdgeFunction} */
return async function handler(request, context) {
if (!origin) {
origin = new URL(request.url).origin;
}

// always await initialization to prevent race condition with concurrent requests
await initialized;
// always await initialization to prevent race condition with concurrent requests
await initialized;

return server.respond(request, {
platform: { context },
getClientAddress() {
return context.ip;
}
});
return server.respond(request, {
platform: { context },
getClientAddress() {
return context.ip;
}
});
};
}
2 changes: 0 additions & 2 deletions packages/adapter-netlify/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export interface AdapterOptions {
edge?: boolean;
/**
* If `true`, your app will be split into multiple functions instead of a single one for the entire app.
*
* If `edge` is `true`, this option cannot be used.
* @default false
*/
split?: boolean;
Expand Down
Loading
Loading