|
1 |
| -import { mkdir, rm } from 'node:fs/promises' |
2 |
| -import { join } from 'node:path' |
3 |
| -import { EDGE_HANDLER_DIR } from '../constants.js' |
4 |
| - |
5 |
| -/** |
6 |
| - * Create a Netlify edge function to run the Next.js server |
7 |
| - */ |
8 |
| -export const createEdgeHandler = async () => { |
9 |
| - // reset the handler directory |
10 |
| - await rm(join(process.cwd(), EDGE_HANDLER_DIR), { recursive: true, force: true }) |
11 |
| - await mkdir(join(process.cwd(), EDGE_HANDLER_DIR), { recursive: true }) |
| 1 | +import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js' |
| 2 | +import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { dirname, join, relative, resolve } from 'node:path' |
| 4 | +import { getMiddlewareManifest } from '../config.js' |
| 5 | +import { |
| 6 | + EDGE_FUNCTIONS_DIR, |
| 7 | + EDGE_HANDLER_NAME, |
| 8 | + PLUGIN_DIR, |
| 9 | + PLUGIN_NAME, |
| 10 | + PLUGIN_VERSION, |
| 11 | +} from '../constants.js' |
| 12 | + |
| 13 | +interface NetlifyManifest { |
| 14 | + version: 1 |
| 15 | + functions: NetlifyDefinition[] |
| 16 | +} |
| 17 | + |
| 18 | +type NetlifyDefinition = |
| 19 | + | { |
| 20 | + function: string |
| 21 | + name?: string |
| 22 | + path: string |
| 23 | + cache?: 'manual' |
| 24 | + generator: string |
| 25 | + } |
| 26 | + | { |
| 27 | + function: string |
| 28 | + name?: string |
| 29 | + pattern: string |
| 30 | + cache?: 'manual' |
| 31 | + generator: string |
| 32 | + } |
| 33 | + |
| 34 | +const getHandlerName = ({ name }: NextDefinition) => |
| 35 | + EDGE_HANDLER_NAME.replace('{{name}}', name.replace(/\W/g, '-')) |
| 36 | + |
| 37 | +const buildHandlerDefinitions = ( |
| 38 | + { name: definitionName, matchers, page }: NextDefinition, |
| 39 | + handlerName: string, |
| 40 | +): NetlifyDefinition[] => { |
| 41 | + return definitionName === 'middleware' |
| 42 | + ? [ |
| 43 | + { |
| 44 | + function: handlerName, |
| 45 | + name: 'Next.js Middleware Handler', |
| 46 | + path: '/*', |
| 47 | + generator: `${PLUGIN_NAME}@${PLUGIN_VERSION}`, |
| 48 | + } as any, |
| 49 | + ] |
| 50 | + : matchers.map((matcher) => ({ |
| 51 | + function: handlerName, |
| 52 | + name: `Next.js Edge Handler: ${page}`, |
| 53 | + pattern: matcher.regexp, |
| 54 | + cache: 'manual', |
| 55 | + generator: `${PLUGIN_NAME}@${PLUGIN_VERSION}`, |
| 56 | + })) |
| 57 | +} |
| 58 | + |
| 59 | +const copyHandlerDependencies = async ( |
| 60 | + { name: definitionName, files }: NextDefinition, |
| 61 | + handlerName: string, |
| 62 | +) => { |
| 63 | + await Promise.all( |
| 64 | + files.map(async (file) => { |
| 65 | + const srcDir = join(process.cwd(), '.next/standalone/.next') |
| 66 | + const destDir = join(process.cwd(), EDGE_FUNCTIONS_DIR, handlerName) |
| 67 | + |
| 68 | + if (file === `server/${definitionName}.js`) { |
| 69 | + const entrypoint = await readFile(join(srcDir, file), 'utf8') |
| 70 | + // const exports = `` |
| 71 | + const exports = ` |
| 72 | + export default _ENTRIES["middleware_${definitionName}"].default; |
| 73 | + // export default () => { |
| 74 | +
|
| 75 | + // console.log('here', _ENTRIES) |
| 76 | + // } |
| 77 | + ` |
| 78 | + await mkdir(dirname(join(destDir, file)), { recursive: true }) |
| 79 | + await writeFile( |
| 80 | + join(destDir, file), |
| 81 | + ` |
| 82 | + import './edge-runtime-webpack.js'; |
| 83 | +
|
| 84 | +
|
| 85 | + var _ENTRIES = {};\n`.concat(entrypoint, '\n', exports), |
| 86 | + ) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + await cp(join(srcDir, file), join(destDir, file)) |
| 91 | + }), |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +const writeHandlerFile = async ({ name: definitionName }: NextDefinition, handlerName: string) => { |
| 96 | + const handlerFile = resolve(EDGE_FUNCTIONS_DIR, handlerName, `${handlerName}.js`) |
| 97 | + const rel = relative(handlerFile, join(PLUGIN_DIR, 'dist/run/handlers/middleware.js')) |
| 98 | + await cp( |
| 99 | + join(PLUGIN_DIR, 'edge-runtime'), |
| 100 | + resolve(EDGE_FUNCTIONS_DIR, handlerName, 'edge-runtime'), |
| 101 | + { |
| 102 | + recursive: true, |
| 103 | + }, |
| 104 | + ) |
| 105 | + await writeFile( |
| 106 | + resolve(EDGE_FUNCTIONS_DIR, handlerName, `${handlerName}.js`), |
| 107 | + `import {handleMiddleware} from './edge-runtime/middleware.ts'; |
| 108 | + import handler from './server/${definitionName}.js'; |
| 109 | + export default (req, context) => handleMiddleware(req, context, handler); |
| 110 | + export const config = {path: "/*"}`, |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +const writeEdgeManifest = async (manifest: NetlifyManifest) => { |
| 115 | + await mkdir(resolve(EDGE_FUNCTIONS_DIR), { recursive: true }) |
| 116 | + await writeFile(resolve(EDGE_FUNCTIONS_DIR, 'manifest.json'), JSON.stringify(manifest, null, 2)) |
| 117 | +} |
| 118 | + |
| 119 | +export const createEdgeHandlers = async () => { |
| 120 | + await rm(EDGE_FUNCTIONS_DIR, { recursive: true, force: true }) |
| 121 | + |
| 122 | + const nextManifest = await getMiddlewareManifest() |
| 123 | + const nextDefinitions = [ |
| 124 | + ...Object.values(nextManifest.middleware), |
| 125 | + // ...Object.values(nextManifest.functions) |
| 126 | + ] |
| 127 | + const netlifyManifest: NetlifyManifest = { |
| 128 | + version: 1, |
| 129 | + functions: await nextDefinitions.reduce( |
| 130 | + async (netlifyDefinitions: Promise<NetlifyDefinition[]>, nextDefinition: NextDefinition) => { |
| 131 | + const handlerName = getHandlerName(nextDefinition) |
| 132 | + await copyHandlerDependencies(nextDefinition, handlerName) |
| 133 | + await writeHandlerFile(nextDefinition, handlerName) |
| 134 | + return [ |
| 135 | + ...(await netlifyDefinitions), |
| 136 | + ...buildHandlerDefinitions(nextDefinition, handlerName), |
| 137 | + ] |
| 138 | + }, |
| 139 | + Promise.resolve([]), |
| 140 | + ), |
| 141 | + } |
| 142 | + |
| 143 | + await writeEdgeManifest(netlifyManifest) |
12 | 144 | }
|
0 commit comments