Skip to content

Commit

Permalink
edge function to node fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Jan 18, 2024
1 parent b014bb1 commit f3ead9c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
28 changes: 13 additions & 15 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export default function vercelServerless({
}

let _config: AstroConfig;
let buildTempFolder: URL;
let serverEntry: string;
let _buildTempFolder: URL;
let _serverEntry: string;
let _entryPoints: Map<RouteData, URL>;
let _middlewareEntryPoint: URL | undefined;
// Extra files to be merged with `includeFiles` during build
Expand Down Expand Up @@ -199,8 +199,8 @@ export default function vercelServerless({
setAdapter(getAdapter({ functionPerRoute, edgeMiddleware }));

_config = config;
buildTempFolder = config.build.server;
serverEntry = config.build.serverEntry;
_buildTempFolder = config.build.server;
_serverEntry = config.build.serverEntry;

if (config.output === 'static') {
throw new AstroError(
Expand Down Expand Up @@ -272,23 +272,19 @@ export default function vercelServerless({
}
} else {
await createFunctionFolder({
functionName: 'render',
functionName: '_render',
runtime,
entry: new URL(serverEntry, buildTempFolder),
entry: new URL(_serverEntry, _buildTempFolder),
config: _config,
logger,
NTF_CACHE,
includeFiles,
excludeFiles,
maxDuration,
});
const dest = _middlewareEntryPoint ? '_middleware' : 'render';
for (const route of routes) {
if (route.prerender) continue;
routeDefinitions.push({
src: route.pattern.source,
dest: 'render',
middlewarePath: _middlewareEntryPoint ? "_middleware" : undefined
});
if (!route.prerender) routeDefinitions.push({ src: route.pattern.source, dest });
}
}
if (_middlewareEntryPoint) {
Expand Down Expand Up @@ -316,7 +312,9 @@ export default function vercelServerless({
? [
{
src: '/.*',
dest: fourOhFourRoute.prerender ? '/404.html' : 'render',
dest: fourOhFourRoute.prerender ? '/404.html'
: _middlewareEntryPoint ? '_middleware'
: 'render',
status: 404,
},
]
Expand All @@ -339,7 +337,7 @@ export default function vercelServerless({
});

// Remove temporary folder
await removeDir(buildTempFolder);
await removeDir(_buildTempFolder);
},
},
};
Expand Down Expand Up @@ -420,7 +418,7 @@ async function createFunctionFolder({
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/serverless-functions/configuration
await writeJson(vcConfig, {
runtime,
handler,
handler: handler.replaceAll("\\","/"),
launcherType: 'Nodejs',
maxDuration,
supportsResponseStreaming: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/integrations/vercel/src/serverless/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export const createExports = (manifest: SSRManifest) => {
const handler = async (req: IncomingMessage, res: ServerResponse) => {
const clientAddress = req.headers['x-forwarded-for'] as string | undefined;
const localsHeader = req.headers[ASTRO_LOCALS_HEADER];
const forwardedForPath = req.headers['x-astro-path'];
if (typeof forwardedForPath === 'string') {
req.url = forwardedForPath;
}
const locals =
typeof localsHeader === 'string'
? JSON.parse(localsHeader)
Expand Down
10 changes: 5 additions & 5 deletions packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ function edgeMiddlewareTemplate(astroMiddlewareEntryPointPath: URL, vercelEdgeMi
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 () => {
return new Response(null, {
const { origin } = new URL(request.url);
const next = () =>
fetch(new URL('/_render', request.url), {
headers: {
'x-middleware-next': '1',
...Object.fromEntries(request.headers.entries()),
'x-astro-path': request.url.replace(origin, ''),
'x-astro-locals': trySerializeLocals(ctx.locals)
}
})
};
return onRequest(ctx, next);
}`;
Expand Down

0 comments on commit f3ead9c

Please sign in to comment.