-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
23 changed files
with
333 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/netlify': minor | ||
--- | ||
|
||
Adds support for Netlify Edge Functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(globalThis as any).process = { | ||
argv: [], | ||
env: {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
98 changes: 98 additions & 0 deletions
98
packages/integrations/netlify/src/integration-edge-functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { AstroAdapter, AstroIntegration, AstroConfig, RouteData } from 'astro'; | ||
import * as fs from 'fs'; | ||
|
||
export function getAdapter(): AstroAdapter { | ||
return { | ||
name: '@astrojs/netlify/edge-functions', | ||
serverEntrypoint: '@astrojs/netlify/netlify-edge-functions.js', | ||
exports: ['default'], | ||
}; | ||
} | ||
|
||
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, | ||
pattern: route.pattern.source | ||
}); | ||
} | ||
} | ||
|
||
const manifest: NetlifyEdgeFunctionManifest = { | ||
functions, | ||
version: 1 | ||
}; | ||
|
||
const manifestURL = new URL('./manifest.json', dir); | ||
const _manifest = JSON.stringify(manifest, null, ' '); | ||
await fs.promises.writeFile(manifestURL, _manifest, 'utf-8'); | ||
} | ||
|
||
export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}): AstroIntegration { | ||
let _config: AstroConfig; | ||
let entryFile: string; | ||
return { | ||
name: '@astrojs/netlify/edge-functions', | ||
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('./edge-functions/', _config.outDir); | ||
}, | ||
'astro:build:setup': ({ vite, target }) => { | ||
if (target === 'server') { | ||
vite.ssr = { | ||
noExternal: true, | ||
}; | ||
} | ||
}, | ||
'astro:build:done': async ({ routes, dir }) => { | ||
|
||
await createEdgeManifest(routes, entryFile, new URL('./edge-functions/', dir)); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export { | ||
netlifyEdgeFunctions as default | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
20 changes: 20 additions & 0 deletions
20
packages/integrations/netlify/src/netlify-edge-functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import './edge-shim.js'; | ||
import { SSRManifest } from 'astro'; | ||
import { App } from 'astro/app'; | ||
|
||
export function createExports(manifest: SSRManifest) { | ||
const app = new App(manifest); | ||
|
||
const handler = async (request: Request): Promise<Response> => { | ||
if(app.match(request)) { | ||
return app.render(request); | ||
} | ||
|
||
return new Response(null, { | ||
status: 404, | ||
statusText: 'Not found' | ||
}); | ||
}; | ||
|
||
return { 'default': handler }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @ts-nocheck | ||
export { fromFileUrl } from 'https://deno.land/std@0.110.0/path/mod.ts'; | ||
export { assertEquals, assert } from 'https://deno.land/std@0.132.0/testing/asserts.ts'; |
18 changes: 18 additions & 0 deletions
18
packages/integrations/netlify/test/edge-functions/edge-basic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// @ts-ignore | ||
import { runBuild } from './test-utils.ts'; | ||
// @ts-ignore | ||
import { assertEquals, assert } from './deps.ts'; | ||
|
||
// @ts-ignore | ||
Deno.test({ | ||
name: 'Edge Basics', | ||
async fn() { | ||
let close = await runBuild('./fixtures/edge-basic/'); | ||
const { default: handler } = await import('./fixtures/edge-basic/dist/edge-functions/entry.mjs'); | ||
const response = await handler(new Request('http://example.com/')); | ||
assertEquals(response.status, 200); | ||
const html = await response.text(); | ||
assert(html, 'got some html'); | ||
await close(); | ||
}, | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/integrations/netlify/test/edge-functions/fixtures/edge-basic/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import { netlifyEdgeFunctions } from '@astrojs/netlify'; | ||
|
||
export default defineConfig({ | ||
adapter: netlifyEdgeFunctions({ | ||
dist: new URL('./dist/', import.meta.url), | ||
}), | ||
experimental: { | ||
ssr: true | ||
} | ||
}) |
9 changes: 9 additions & 0 deletions
9
packages/integrations/netlify/test/edge-functions/fixtures/edge-basic/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@test/netlify-edge-astro-basic", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*", | ||
"@astrojs/netlify": "workspace:*" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/integrations/netlify/test/edge-functions/fixtures/edge-basic/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head><title>Testing</title></head> | ||
<body> | ||
<h1>Test page</h1> | ||
<h2>Links</h2> | ||
<ul> | ||
<li><a href="/two/">Two</a></li> | ||
</ul> | ||
</body> | ||
</html> |
6 changes: 6 additions & 0 deletions
6
packages/integrations/netlify/test/edge-functions/fixtures/edge-basic/src/pages/two.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<head><title>Page Two</title></head> | ||
<body> | ||
<h1>Page two</h1> | ||
</body> | ||
</html> |
13 changes: 13 additions & 0 deletions
13
packages/integrations/netlify/test/edge-functions/test-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @ts-ignore | ||
import { fromFileUrl } from './deps.ts'; | ||
const dir = new URL('./', import.meta.url); | ||
|
||
export async function runBuild(fixturePath: string) { | ||
// @ts-ignore | ||
let proc = Deno.run({ | ||
cmd: ['node', '../../../../../../astro/astro.js', 'build', '--silent'], | ||
cwd: fromFileUrl(new URL(fixturePath, dir)), | ||
}); | ||
await proc.status(); | ||
return async () => await proc.close(); | ||
} |
Oops, something went wrong.