diff options
author | 2022-04-19 11:22:15 -0400 | |
---|---|---|
committer | 2022-04-19 11:22:15 -0400 | |
commit | 4cf54c60aa63bd614b242da0602790015005673d (patch) | |
tree | 25fcec1da64890044742a1090b1773076a0d43e9 /packages/integrations/netlify/src/integration-functions.ts | |
parent | c35e94f5443d4ade07ff787d39b042eb3b9004fb (diff) | |
download | astro-4cf54c60aa63bd614b242da0602790015005673d.tar.gz astro-4cf54c60aa63bd614b242da0602790015005673d.tar.zst astro-4cf54c60aa63bd614b242da0602790015005673d.zip |
Netlify Edge function support (#3148)
* Netlify Edge function support
* Update readme with edge function information
* Adds a changeset
* Disable running edge function test in CI for now
Diffstat (limited to 'packages/integrations/netlify/src/integration-functions.ts')
-rw-r--r-- | packages/integrations/netlify/src/integration-functions.ts | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index 5b687cda4..2720eb591 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -1 +1,65 @@ -export { netlifyFunctions as default } from './index.js'; +import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro'; +import fs from 'fs'; + +export function getAdapter(): AstroAdapter { + return { + name: '@astrojs/netlify/functions', + serverEntrypoint: '@astrojs/netlify/netlify-functions.js', + exports: ['handler'], + args: {}, + }; +} + +interface NetlifyFunctionsOptions { + dist?: URL; +} + +function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegration { + let _config: AstroConfig; + let entryFile: string; + return { + name: '@astrojs/netlify', + hooks: { + 'astro:config:setup': ({ config }) => { + if (dist) { + config.outDir = dist; + } else { + config.outDir = new URL('./netlify/', config.root); + } + }, + 'astro:config:done': ({ config, setAdapter }) => { + setAdapter(getAdapter()); + _config = config; + }, + 'astro:build:start': async ({ buildConfig }) => { + entryFile = buildConfig.serverEntry.replace(/\.m?js/, ''); + buildConfig.client = _config.outDir; + buildConfig.server = new URL('./functions/', _config.outDir); + }, + 'astro:build:done': async ({ routes, dir }) => { + const _redirectsURL = new URL('./_redirects', dir); + + // Create the redirects file that is used for routing. + let _redirects = ''; + for (const route of routes) { + if (route.pathname) { + _redirects += ` +${route.pathname} /.netlify/functions/${entryFile} 200`; + } else { + const pattern = + '/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/'); + _redirects += ` +${pattern} /.netlify/functions/${entryFile} 200`; + } + } + + // Always use appendFile() because the redirects file could already exist, + // e.g. due to a `/public/_redirects` file that got copied to the output dir. + // If the file does not exist yet, appendFile() automatically creates it. + await fs.promises.appendFile(_redirectsURL, _redirects, 'utf-8'); + }, + }, + }; +} + +export { netlifyFunctions, netlifyFunctions as default }; |