-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@astrojs/netlify): edge middleware support (#7632)
Co-authored-by: Bjorn Lu <[email protected]> Co-authored-by: Yan Thomas <[email protected]>
- Loading branch information
1 parent
cc8e9de
commit 4c93bd8
Showing
25 changed files
with
411 additions
and
161 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,10 @@ | ||
--- | ||
'@astrojs/netlify': minor | ||
--- | ||
|
||
When a project uses the new option Astro `build.excludeMiddleware`, the | ||
`@astrojs/netlify/functions` adapter will automatically create an Edge Middleware | ||
that will automatically communicate with the Astro Middleware. | ||
|
||
Check the [documentation](https://github.com/withastro/astro/blob/main/packages/integrations/netlify/README.md#edge-middleware-with-astro-middleware) for more details. | ||
|
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
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,75 @@ | ||
import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
import { join } from 'node:path'; | ||
import { existsSync } from 'node:fs'; | ||
import { ASTRO_LOCALS_HEADER } from './integration-functions.js'; | ||
import { DENO_SHIM } from './shared.js'; | ||
|
||
/** | ||
* It generates a Netlify edge function. | ||
* | ||
*/ | ||
export async function generateEdgeMiddleware( | ||
astroMiddlewareEntryPointPath: URL, | ||
outPath: string, | ||
netlifyEdgeMiddlewareHandlerPath: URL | ||
): Promise<URL> { | ||
const entryPointPathURLAsString = JSON.stringify( | ||
fileURLToPath(astroMiddlewareEntryPointPath).replace(/\\/g, '/') | ||
); | ||
|
||
const code = edgeMiddlewareTemplate(entryPointPathURLAsString, netlifyEdgeMiddlewareHandlerPath); | ||
const bundledFilePath = join(outPath, 'edgeMiddleware.js'); | ||
const esbuild = await import('esbuild'); | ||
await esbuild.build({ | ||
stdin: { | ||
contents: code, | ||
resolveDir: process.cwd(), | ||
}, | ||
target: 'es2020', | ||
platform: 'browser', | ||
outfile: bundledFilePath, | ||
allowOverwrite: true, | ||
format: 'esm', | ||
bundle: true, | ||
minify: false, | ||
banner: { | ||
js: DENO_SHIM, | ||
}, | ||
}); | ||
return pathToFileURL(bundledFilePath); | ||
} | ||
|
||
function edgeMiddlewareTemplate(middlewarePath: string, netlifyEdgeMiddlewareHandlerPath: URL) { | ||
const filePathEdgeMiddleware = fileURLToPath(netlifyEdgeMiddlewareHandlerPath); | ||
let handlerTemplateImport = ''; | ||
let handlerTemplateCall = '{}'; | ||
if (existsSync(filePathEdgeMiddleware + '.js') || existsSync(filePathEdgeMiddleware + '.ts')) { | ||
const stringified = JSON.stringify(filePathEdgeMiddleware.replace(/\\/g, '/')); | ||
handlerTemplateImport = `import handler from ${stringified}`; | ||
handlerTemplateCall = `handler({ request, context })`; | ||
} else { | ||
} | ||
return ` | ||
${handlerTemplateImport} | ||
import { onRequest } from ${middlewarePath}; | ||
import { createContext, trySerializeLocals } from 'astro/middleware'; | ||
export default async function middleware(request, context) { | ||
const url = new URL(request.url); | ||
const ctx = createContext({ | ||
request, | ||
params: {} | ||
}); | ||
ctx.locals = ${handlerTemplateCall}; | ||
const next = async () => { | ||
request.headers.set(${JSON.stringify(ASTRO_LOCALS_HEADER)}, trySerializeLocals(ctx.locals)); | ||
return await context.next(); | ||
}; | ||
return onRequest(ctx, next); | ||
} | ||
export const config = { | ||
path: "/*" | ||
} | ||
`; | ||
} |
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
Oops, something went wrong.