diff options
author | 2023-07-17 15:53:10 +0100 | |
---|---|---|
committer | 2023-07-17 15:53:10 +0100 | |
commit | 4c93bd8154c210ebce6ad2889bd8bfdf4c349a78 (patch) | |
tree | e0b9fb9474845411b35f177260408f444d0631ff /packages/integrations/netlify/src/integration-edge-functions.ts | |
parent | cc8e9de88179d2ed4b70980c60b41448db393429 (diff) | |
download | astro-4c93bd8154c210ebce6ad2889bd8bfdf4c349a78.tar.gz astro-4c93bd8154c210ebce6ad2889bd8bfdf4c349a78.tar.zst astro-4c93bd8154c210ebce6ad2889bd8bfdf4c349a78.zip |
feat(@astrojs/netlify): edge middleware support (#7632)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
Diffstat (limited to 'packages/integrations/netlify/src/integration-edge-functions.ts')
-rw-r--r-- | packages/integrations/netlify/src/integration-edge-functions.ts | 110 |
1 files changed, 9 insertions, 101 deletions
diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 72721bd59..ac7c124fb 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -1,21 +1,10 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro'; -import esbuild from 'esbuild'; -import * as fs from 'fs'; -import * as npath from 'path'; -import { fileURLToPath } from 'url'; -import { createRedirects } from './shared.js'; - -interface BuildConfig { - server: URL; - client: URL; - serverEntry: string; - assets: string; -} - -const SHIM = `globalThis.process = { - argv: [], - env: Deno.env.toObject(), -};`; +import { + bundleServerEntry, + createEdgeManifest, + createRedirects, + type NetlifyEdgeFunctionsOptions, +} from './shared.js'; export function getAdapter(): AstroAdapter { return { @@ -25,92 +14,10 @@ export function getAdapter(): AstroAdapter { }; } -interface NetlifyEdgeFunctionsOptions { - dist?: URL; -} - -interface NetlifyEdgeFunctionManifestFunctionPath { - function: string; - path: string; -} - -interface NetlifyEdgeFunctionManifestFunctionPattern { - function: string; - pattern: string; -} - -type NetlifyEdgeFunctionManifestFunction = - | NetlifyEdgeFunctionManifestFunctionPath - | NetlifyEdgeFunctionManifestFunctionPattern; - -interface NetlifyEdgeFunctionManifest { - functions: NetlifyEdgeFunctionManifestFunction[]; - version: 1; -} - -async function createEdgeManifest(routes: RouteData[], entryFile: string, dir: URL) { - const functions: NetlifyEdgeFunctionManifestFunction[] = []; - for (const route of routes) { - if (route.pathname) { - functions.push({ - function: entryFile, - path: route.pathname, - }); - } else { - functions.push({ - function: entryFile, - // Make route pattern serializable to match expected - // Netlify Edge validation format. Mirrors Netlify's own edge bundler: - // https://github.com/netlify/edge-bundler/blob/main/src/manifest.ts#L34 - pattern: route.pattern.source.replace(/\\\//g, '/').toString(), - }); - } - } - - const manifest: NetlifyEdgeFunctionManifest = { - functions, - version: 1, - }; - - const baseDir = new URL('./.netlify/edge-functions/', dir); - await fs.promises.mkdir(baseDir, { recursive: true }); - - const manifestURL = new URL('./manifest.json', baseDir); - const _manifest = JSON.stringify(manifest, null, ' '); - await fs.promises.writeFile(manifestURL, _manifest, 'utf-8'); -} - -async function bundleServerEntry({ serverEntry, server }: BuildConfig, vite: any) { - const entryUrl = new URL(serverEntry, server); - const pth = fileURLToPath(entryUrl); - await esbuild.build({ - target: 'es2020', - platform: 'browser', - entryPoints: [pth], - outfile: pth, - allowOverwrite: true, - format: 'esm', - bundle: true, - external: ['@astrojs/markdown-remark'], - banner: { - js: SHIM, - }, - }); - - // Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash. - try { - const chunkFileNames = - vite?.build?.rollupOptions?.output?.chunkFileNames ?? `chunks/chunk.[hash].mjs`; - const chunkPath = npath.dirname(chunkFileNames); - const chunksDirUrl = new URL(chunkPath + '/', server); - await fs.promises.rm(chunksDirUrl, { recursive: true, force: true }); - } catch {} -} - export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}): AstroIntegration { let _config: AstroConfig; let entryFile: string; - let _buildConfig: BuildConfig; + let _buildConfig: AstroConfig['build']; let _vite: any; return { name: '@astrojs/netlify/edge-functions', @@ -164,7 +71,8 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) } }, 'astro:build:done': async ({ routes, dir }) => { - await bundleServerEntry(_buildConfig, _vite); + const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server); + await bundleServerEntry(entryUrl, _buildConfig.server, _vite); await createEdgeManifest(routes, entryFile, _config.root); const dynamicTarget = `/.netlify/edge-functions/${entryFile}`; const map: [RouteData, string][] = routes.map((route) => { |