From 1c802e39ee2509f462e273733f3f58a6461eeeb2 Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Mon, 16 Jan 2023 20:01:25 +0100 Subject: [PATCH 1/7] Add on-demand builders option --- .../netlify/src/integration-edge-functions.ts | 2 +- .../integrations/netlify/src/integration-functions.ts | 7 +++++-- packages/integrations/netlify/src/netlify-functions.ts | 8 ++++++-- packages/integrations/netlify/src/shared.ts | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index a05f8f3a0fff..6af2ee9ee507 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -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'); }, }, }; diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index e8ff4bd1f899..ab83d9183d0e 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -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; @@ -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/, ''); @@ -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); }, }, }; diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts index 57b042af9ee7..9c9a70848aab 100644 --- a/packages/integrations/netlify/src/netlify-functions.ts +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -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'; @@ -8,6 +8,7 @@ polyfill(globalThis, { }); export interface Args { + builders?: boolean; binaryMediaTypes?: string[]; } @@ -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', @@ -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, @@ -143,6 +145,8 @@ export const createExports = (manifest: SSRManifest, args: Args) => { return fnResponse; }; + const handler = builders ? builder(myHandler) : myHandler + return { handler }; }; diff --git a/packages/integrations/netlify/src/shared.ts b/packages/integrations/netlify/src/shared.ts index 2c648984a2b6..55dd02286c42 100644 --- a/packages/integrations/netlify/src/shared.ts +++ b/packages/integrations/netlify/src/shared.ts @@ -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 = ''; From 27eee35bc8bb1fe5e2c6ab2f675f141513a56cd9 Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Tue, 17 Jan 2023 20:40:54 +0100 Subject: [PATCH 2/7] chore: add changeset --- .changeset/twenty-pans-agree.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/twenty-pans-agree.md diff --git a/.changeset/twenty-pans-agree.md b/.changeset/twenty-pans-agree.md new file mode 100644 index 000000000000..cef546080ce5 --- /dev/null +++ b/.changeset/twenty-pans-agree.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': minor +--- + +Add on-demand builders Netlify functions From 44c4b82c67b6d875b97503d72c32b8b169c3ad61 Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Sat, 21 Jan 2023 10:43:06 +0100 Subject: [PATCH 3/7] docs: add documentation in configuration section --- packages/integrations/netlify/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/integrations/netlify/README.md b/packages/integrations/netlify/README.md index 69f66ca73dca..81c8ac18f612 100644 --- a/packages/integrations/netlify/README.md +++ b/packages/integrations/netlify/README.md @@ -117,6 +117,28 @@ And then point to the dist in your `netlify.toml`: directory = "dist/functions" ``` +### builders + +On-demand Builders are serverless functions used to generate web content as needed that’s automatically cached on Netlify’s Edge CDN. To activate rendering with this type of functions, use the `builders` option: + +```js +// astro.config.mjs +import { defineConfig } from 'astro/config'; +import netlify from '@astrojs/netlify/functions'; + +export default defineConfig({ + output: 'server', + adapter: netlify({ + builders: true + }), +}); +``` + +This functionality is only available whith the `@astrojs/netlify/functions` adapter and is not compatible with Edge Functions. + +For more information about Netlify On-demand Builders visit the [Netlify documentation.](https://docs.netlify.com/configure-builds/on-demand-builders/) + + ### binaryMediaTypes > This option is only needed for the Functions adapter and is not needed for Edge Functions. From ce3bc6536098948b79fc6d53c9ff81ad13bf475f Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Thu, 26 Jan 2023 13:46:36 +0100 Subject: [PATCH 4/7] Update packages/integrations/netlify/README.md Co-authored-by: Sarah Rainsberger --- packages/integrations/netlify/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/netlify/README.md b/packages/integrations/netlify/README.md index 81c8ac18f612..4577bc9a3a72 100644 --- a/packages/integrations/netlify/README.md +++ b/packages/integrations/netlify/README.md @@ -119,7 +119,7 @@ directory = "dist/functions" ### builders -On-demand Builders are serverless functions used to generate web content as needed that’s automatically cached on Netlify’s Edge CDN. To activate rendering with this type of functions, use the `builders` option: +[Netlify On-demand Builders](https://docs.netlify.com/configure-builds/on-demand-builders/) are serverless functions used to build and cache page content on Netlify’s Edge CDN. You can enable these functions with the `builders` option: ```js // astro.config.mjs From cf2398f5881849b2264608c4eb11834ea446732b Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Thu, 26 Jan 2023 13:47:13 +0100 Subject: [PATCH 5/7] Update packages/integrations/netlify/README.md Co-authored-by: Sarah Rainsberger --- packages/integrations/netlify/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/integrations/netlify/README.md b/packages/integrations/netlify/README.md index 4577bc9a3a72..6627e290e750 100644 --- a/packages/integrations/netlify/README.md +++ b/packages/integrations/netlify/README.md @@ -136,7 +136,6 @@ export default defineConfig({ This functionality is only available whith the `@astrojs/netlify/functions` adapter and is not compatible with Edge Functions. -For more information about Netlify On-demand Builders visit the [Netlify documentation.](https://docs.netlify.com/configure-builds/on-demand-builders/) ### binaryMediaTypes From cc6c096affca2efedd931ce5db42c93b9cb09bf9 Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Thu, 26 Jan 2023 13:49:28 +0100 Subject: [PATCH 6/7] Update packages/integrations/netlify/README.md Co-authored-by: Sarah Rainsberger --- packages/integrations/netlify/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/netlify/README.md b/packages/integrations/netlify/README.md index 6627e290e750..ec72f2a2cad6 100644 --- a/packages/integrations/netlify/README.md +++ b/packages/integrations/netlify/README.md @@ -134,7 +134,7 @@ export default defineConfig({ }); ``` -This functionality is only available whith the `@astrojs/netlify/functions` adapter and is not compatible with Edge Functions. +On-demand Builders are only available with the `@astrojs/netlify/functions` adapter and are not compatible with Edge Functions. From 8cc7f469f32f6993b2b667c639d1715538490fd1 Mon Sep 17 00:00:00 2001 From: Juan Miguel Guerrero Date: Fri, 27 Jan 2023 00:31:37 +0100 Subject: [PATCH 7/7] Update .changeset/twenty-pans-agree.md Co-authored-by: Sarah Rainsberger --- .changeset/twenty-pans-agree.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/twenty-pans-agree.md b/.changeset/twenty-pans-agree.md index cef546080ce5..0a747c2480c8 100644 --- a/.changeset/twenty-pans-agree.md +++ b/.changeset/twenty-pans-agree.md @@ -2,4 +2,4 @@ '@astrojs/netlify': minor --- -Add on-demand builders Netlify functions +Add `builders` config option for Netlify On-demand Builders.