Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@astrojs/netlify): Add on-demand builders Netlify functions #5874

Merged
merged 8 commits into from
Jan 27, 2023
5 changes: 5 additions & 0 deletions .changeset/twenty-pans-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': minor
---

Add on-demand builders Netlify functions
juanmiguelguerrero marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {})
'astro:build:done': async ({ routes, dir }) => {
await bundleServerEntry(_buildConfig, _vite);
await createEdgeManifest(routes, entryFile, _config.root);
await createRedirects(routes, dir, entryFile, true);
await createRedirects(routes, dir, entryFile, 'edge-functions');
},
},
};
Expand Down
7 changes: 5 additions & 2 deletions packages/integrations/netlify/src/integration-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ export function getAdapter(args: Args = {}): AstroAdapter {

interface NetlifyFunctionsOptions {
dist?: URL;
builders?: boolean;
binaryMediaTypes?: string[];
}

function netlifyFunctions({
dist,
builders,
binaryMediaTypes,
}: NetlifyFunctionsOptions = {}): AstroIntegration {
let _config: AstroConfig;
Expand All @@ -36,7 +38,7 @@ function netlifyFunctions({
});
},
'astro:config:done': ({ config, setAdapter }) => {
setAdapter(getAdapter({ binaryMediaTypes }));
setAdapter(getAdapter({ binaryMediaTypes, builders }));
_config = config;
entryFile = config.build.serverEntry.replace(/\.m?js/, '');

Expand All @@ -48,7 +50,8 @@ function netlifyFunctions({
}
},
'astro:build:done': async ({ routes, dir }) => {
await createRedirects(routes, dir, entryFile, false);
const type = builders ? 'builders' : 'functions'
await createRedirects(routes, dir, entryFile, type);
},
},
};
Expand Down
8 changes: 6 additions & 2 deletions packages/integrations/netlify/src/netlify-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { polyfill } from '@astrojs/webapi';
import type { Handler } from '@netlify/functions';
import { builder, Handler } from '@netlify/functions';
import { SSRManifest } from 'astro';
import { App } from 'astro/app';

Expand All @@ -8,6 +8,7 @@ polyfill(globalThis, {
});

export interface Args {
builders?: boolean;
binaryMediaTypes?: string[];
}

Expand All @@ -20,6 +21,7 @@ const clientAddressSymbol = Symbol.for('astro.clientAddress');
export const createExports = (manifest: SSRManifest, args: Args) => {
const app = new App(manifest);

const builders = args.builders ?? false;
const binaryMediaTypes = args.binaryMediaTypes ?? [];
const knownBinaryMediaTypes = new Set([
'audio/3gpp',
Expand Down Expand Up @@ -53,7 +55,7 @@ export const createExports = (manifest: SSRManifest, args: Args) => {
...binaryMediaTypes,
]);

const handler: Handler = async (event) => {
const myHandler: Handler = async (event) => {
const { httpMethod, headers, rawUrl, body: requestBody, isBase64Encoded } = event;
const init: RequestInit = {
method: httpMethod,
Expand Down Expand Up @@ -143,6 +145,8 @@ export const createExports = (manifest: SSRManifest, args: Args) => {
return fnResponse;
};

const handler = builders ? builder(myHandler) : myHandler

return { handler };
};

Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/netlify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export async function createRedirects(
routes: RouteData[],
dir: URL,
entryFile: string,
edge: boolean
type: 'functions' | 'edge-functions' | 'builders'
) {
const _redirectsURL = new URL('./_redirects', dir);
const kind = edge ? 'edge-functions' : 'functions';
const kind = type ?? 'functions';

// Create the redirects file that is used for routing.
let _redirects = '';
Expand Down