Skip to content

Commit

Permalink
remove site requirement from netlify adapter (withastro#3041)
Browse files Browse the repository at this point in the history
* remove site requirement from netlify adapter

* update readme
  • Loading branch information
FredKSchott committed Apr 10, 2022
1 parent d63dcd5 commit fbc32cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://localhost:8080)
// to test local builds via the netlify CLI
site: 'https://my-production-url.netlify.app',
});
```

Expand Down
14 changes: 3 additions & 11 deletions packages/integrations/netlify/src/index.ts
Original file line number Diff line number Diff line change
@@ -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: { },
};
}

Expand All @@ -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 }) => {
Expand Down
26 changes: 15 additions & 11 deletions packages/integrations/netlify/src/netlify-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
};
};

Expand Down

0 comments on commit fbc32cf

Please sign in to comment.