diff options
author | 2022-03-25 12:08:02 -0400 | |
---|---|---|
committer | 2022-03-25 12:08:02 -0400 | |
commit | 80034c6cbc89761618847e6df43fd49560a05aa9 (patch) | |
tree | 8433295f09289c5e3f4c35ed9965e80865a3cc5e /packages/integrations/netlify/src | |
parent | df8fac50b950bffd5f46599bce9b42daed6ae524 (diff) | |
download | astro-80034c6cbc89761618847e6df43fd49560a05aa9.tar.gz astro-80034c6cbc89761618847e6df43fd49560a05aa9.tar.zst astro-80034c6cbc89761618847e6df43fd49560a05aa9.zip |
Netlify adapter (#2879)
* Netlify adapter
* Remove package.json export that doesnt exist
* Fix out path
* Make netlifyFunctions the default
* Make the dist configurable
* Add an export for the functions
* Append of the file exists
Diffstat (limited to 'packages/integrations/netlify/src')
-rw-r--r-- | packages/integrations/netlify/src/index.ts | 64 | ||||
-rw-r--r-- | packages/integrations/netlify/src/integration-functions.ts | 3 | ||||
-rw-r--r-- | packages/integrations/netlify/src/netlify-functions.ts | 43 |
3 files changed, 110 insertions, 0 deletions
diff --git a/packages/integrations/netlify/src/index.ts b/packages/integrations/netlify/src/index.ts new file mode 100644 index 000000000..4ddacfaf2 --- /dev/null +++ b/packages/integrations/netlify/src/index.ts @@ -0,0 +1,64 @@ +import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro'; +import fs from 'fs'; + +export function getAdapter(site: string | undefined): AstroAdapter { + return { + name: '@astrojs/netlify', + serverEntrypoint: '@astrojs/netlify/netlify-functions.js', + exports: ['handler'], + args: { site } + }; +} + +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.dist = dist; + } else { + config.dist = new URL('./netlify/', config.projectRoot); + } + }, + 'astro:config:done': ({ config, setAdapter }) => { + setAdapter(getAdapter(config.buildOptions.site)); + _config = config; + }, + 'astro:build:start': async({ buildConfig }) => { + entryFile = buildConfig.serverEntry.replace(/\.m?js/, ''); + buildConfig.client = _config.dist; + buildConfig.server = new URL('./functions/', _config.dist); + }, + '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` + } + } + + if(fs.existsSync(_redirects)) { + await fs.promises.appendFile(_redirectsURL, _redirects, 'utf-8'); + } else { + await fs.promises.writeFile(_redirectsURL, _redirects, 'utf-8'); + } + } + }, + }; +} + +export { + netlifyFunctions, + netlifyFunctions as default +}; diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts new file mode 100644 index 000000000..540fcdee2 --- /dev/null +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -0,0 +1,3 @@ +export { + netlifyFunctions as default +} from './index.js'; diff --git a/packages/integrations/netlify/src/netlify-functions.ts b/packages/integrations/netlify/src/netlify-functions.ts new file mode 100644 index 000000000..cf6c871a9 --- /dev/null +++ b/packages/integrations/netlify/src/netlify-functions.ts @@ -0,0 +1,43 @@ +import { SSRManifest } from 'astro'; +import type { Handler } from "@netlify/functions"; +import { App } from 'astro/app'; +import { polyfill } from '@astrojs/webapi'; + +polyfill(globalThis, { + exclude: 'window document', +}); + +interface Args { + site?: string; +} + +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 + }); + + if(!app.match(request)) { + return { + statusCode: 404, + body: 'Not found' + }; + } + + const response = await app.render(request); + const body = await response.text(); + + return { + statusCode: 200, + headers: Object.fromEntries(response.headers.entries()), + body + }; + } + + return { handler }; +}; |