summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/src/middleware.ts
diff options
context:
space:
mode:
authorGravatar Emanuele Stoppa <my.burning@gmail.com> 2023-07-17 15:53:10 +0100
committerGravatar GitHub <noreply@github.com> 2023-07-17 15:53:10 +0100
commit4c93bd8154c210ebce6ad2889bd8bfdf4c349a78 (patch)
treee0b9fb9474845411b35f177260408f444d0631ff /packages/integrations/netlify/src/middleware.ts
parentcc8e9de88179d2ed4b70980c60b41448db393429 (diff)
downloadastro-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/middleware.ts')
-rw-r--r--packages/integrations/netlify/src/middleware.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/packages/integrations/netlify/src/middleware.ts b/packages/integrations/netlify/src/middleware.ts
new file mode 100644
index 000000000..a53d4fbde
--- /dev/null
+++ b/packages/integrations/netlify/src/middleware.ts
@@ -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: "/*"
+}
+`;
+}