diff --git a/.changeset/early-lions-occur.md b/.changeset/early-lions-occur.md new file mode 100644 index 000000000000..cc3e53e925c4 --- /dev/null +++ b/.changeset/early-lions-occur.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +fix: ensure `read` works in an edge function that has deployment protection. Protection bypass automation must be enabled diff --git a/packages/adapter-vercel/files/edge.js b/packages/adapter-vercel/files/edge.js index e2b6301a004b..1a66bf2d8f9a 100644 --- a/packages/adapter-vercel/files/edge.js +++ b/packages/adapter-vercel/files/edge.js @@ -5,6 +5,14 @@ import { manifest } from 'MANIFEST'; const server = new Server(manifest); +/** @type {HeadersInit | undefined} */ +let read_headers; +if (process.env.VERCEL_AUTOMATION_BYPASS_SECRET) { + read_headers = { + 'x-vercel-protection-bypass': process.env.VERCEL_AUTOMATION_BYPASS_SECRET + }; +} + /** * We don't know the origin until we receive a request, but * that's guaranteed to happen before we call `read` @@ -15,10 +23,19 @@ let origin; const initialized = server.init({ env: /** @type {Record} */ (process.env), read: async (file) => { - const response = await fetch(`${origin}/${file}`); + const url = `${origin}/${file}`; + const response = await fetch(url, { + // we need to add a bypass header if the user has deployment protection enabled + // see https://vercel.com/docs/deployment-protection/methods-to-bypass-deployment-protection/protection-bypass-automation + headers: read_headers + }); + + // TODO: if the user hasn't enabled protection bypass for automation we should probably tell them to do so but only if deployment protection is enabled + if (!response.ok) { - throw new Error(`Failed to fetch ${file}: ${response.status} ${response.statusText}`); + throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`); } + return response.body; } });