From fbc32cf65566b50a85e78239826fbce2bb4a964f Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Sun, 10 Apr 2022 14:34:49 -0700 Subject: [PATCH] remove site requirement from netlify adapter (#3041) * remove site requirement from netlify adapter * update readme --- .../netlify/{readme.md => src/README.md} | 4 --- packages/integrations/netlify/src/index.ts | 14 +++------- .../netlify/src/netlify-functions.ts | 26 +++++++++++-------- 3 files changed, 18 insertions(+), 26 deletions(-) rename packages/integrations/netlify/{readme.md => src/README.md} (83%) diff --git a/packages/integrations/netlify/readme.md b/packages/integrations/netlify/src/README.md similarity index 83% rename from packages/integrations/netlify/readme.md rename to packages/integrations/netlify/src/README.md index 3e9d7879b..1a77ed598 100644 --- a/packages/integrations/netlify/readme.md +++ b/packages/integrations/netlify/src/README.md @@ -10,10 +10,6 @@ import netlify from '@astrojs/netlify/functions'; export default defineConfig({ adapter: netlify(), - // Where your Netlify app will be deployed. - // Feel free to use a local URL (i.e. http://localhost:8080) - // to test local builds via the netlify CLI - site: 'https://my-production-url.netlify.app', }); ``` diff --git a/packages/integrations/netlify/src/index.ts b/packages/integrations/netlify/src/index.ts index 67a036335..9a428e260 100644 --- a/packages/integrations/netlify/src/index.ts +++ b/packages/integrations/netlify/src/index.ts @@ -1,12 +1,12 @@ import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro'; import fs from 'fs'; -export function getAdapter(site: string | undefined): AstroAdapter { +export function getAdapter(): AstroAdapter { return { name: '@astrojs/netlify', serverEntrypoint: '@astrojs/netlify/netlify-functions.js', exports: ['handler'], - args: { site }, + args: { }, }; } @@ -28,15 +28,7 @@ function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegrat } }, 'astro:config:done': ({ config, setAdapter }) => { - let site = null; - try { - site = new URL(config.base, config.site); - } catch { - throw new Error( - 'The Netlify adapter requires a deployment URL. Ensure a "site" is specified in your astro.config. If you provided a "base" in your astro.config, ensure it is a valid path.' - ); - } - setAdapter(getAdapter(site.toString())); + setAdapter(getAdapter()); _config = config; }, 'astro:build:start': async ({ buildConfig }) => { diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts index cb95f1f3f..474cac4b6 100644 --- a/packages/integrations/netlify/src/netlify-functions.ts +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -7,20 +7,24 @@ polyfill(globalThis, { exclude: 'window document', }); -interface Args { - site?: string; -} +interface Args {} export const createExports = (manifest: SSRManifest, args: Args) => { const app = new App(manifest); - const site = new URL(args.site ?? `https://netlify.com`); const handler: Handler = async (event) => { - const headers = new Headers(event.headers as any); - const request = new Request(new URL(event.path, site).toString(), { - method: event.httpMethod, - headers, - }); + const { httpMethod, headers, rawUrl, body: requestBody, isBase64Encoded } = event; + const init: RequestInit = { + method: httpMethod, + headers: new Headers(headers as any), + }; + // Attach the event body the the request, with proper encoding. + if (httpMethod !== 'GET' && httpMethod !== 'HEAD') { + const encoding = isBase64Encoded ? 'base64' : 'utf-8'; + init.body = + typeof requestBody === 'string' ? Buffer.from(requestBody, encoding) : requestBody; + } + const request = new Request(rawUrl, init); if (!app.match(request)) { return { @@ -30,12 +34,12 @@ export const createExports = (manifest: SSRManifest, args: Args) => { } const response = await app.render(request); - const body = await response.text(); + const responseBody = await response.text(); return { statusCode: 200, headers: Object.fromEntries(response.headers.entries()), - body, + body: responseBody, }; };