summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/src/index.ts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-04-19 11:22:15 -0400
committerGravatar GitHub <noreply@github.com> 2022-04-19 11:22:15 -0400
commit4cf54c60aa63bd614b242da0602790015005673d (patch)
tree25fcec1da64890044742a1090b1773076a0d43e9 /packages/integrations/netlify/src/index.ts
parentc35e94f5443d4ade07ff787d39b042eb3b9004fb (diff)
downloadastro-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/index.ts')
-rw-r--r--packages/integrations/netlify/src/index.ts73
1 files changed, 8 insertions, 65 deletions
diff --git a/packages/integrations/netlify/src/index.ts b/packages/integrations/netlify/src/index.ts
index c473de67a..121495652 100644
--- a/packages/integrations/netlify/src/index.ts
+++ b/packages/integrations/netlify/src/index.ts
@@ -1,65 +1,8 @@
-import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
-import fs from 'fs';
-
-export function getAdapter(): AstroAdapter {
- return {
- name: '@astrojs/netlify',
- 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 };
+export {
+ netlifyFunctions,
+ netlifyFunctions as default
+} from './integration-functions.js';
+
+export {
+ netlifyEdgeFunctions
+} from './integration-edge-functions.js';