remove site requirement from netlify adapter (#3041)

* remove site requirement from netlify adapter

* update readme
This commit is contained in:
Fred K. Schott 2022-04-10 14:34:49 -07:00 committed by GitHub
parent d63dcd505a
commit fbc32cf655
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 26 deletions

View file

@ -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',
});
```

View file

@ -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 }) => {

View file

@ -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,
};
};