Skip to content

Commit

Permalink
vercel implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Oct 2, 2023
1 parent 9b7a2a4 commit 05c4844
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/core/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type CreateContext = {
rerouteImpl?: RerouteImplementation;
};

const defaultRerouteImpl: RerouteImplementation = (path, context) => { throw new Error(`.reroute() has not been implemented for edge middleware.`) }
const defaultRerouteImpl: RerouteImplementation = (path, context) => { throw new Error(`.reroute() has not been implemented for this edge middleware.`) }

/**
* Creates a context to be passed to Astro middleware `onRequest` function.
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/vercel/src/serverless/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function getAdapter({
hybridOutput: 'stable',
staticOutput: 'stable',
serverOutput: 'stable',
reroute: 'experimental',
assets: {
supportKind: 'stable',
isSharpCompatible: true,
Expand Down
2 changes: 2 additions & 0 deletions packages/integrations/vercel/src/serverless/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
import { applyPolyfills } from 'astro/app/node';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { rerouteImpl } from './reroute.js';

import { ASTRO_LOCALS_HEADER } from './adapter.js';
import { getRequest, setResponse } from './request-transform.js';
Expand All @@ -10,6 +11,7 @@ applyPolyfills();

export const createExports = (manifest: SSRManifest) => {
const app = new App(manifest);
app.setReroute(rerouteImpl);

const handler = async (req: IncomingMessage, res: ServerResponse) => {
let request: Request;
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/vercel/src/serverless/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ function edgeMiddlewareTemplate(middlewarePath: string, vercelEdgeMiddlewareHand
${handlerTemplateImport}
import { onRequest } from ${middlewarePath};
import { createContext, trySerializeLocals } from 'astro/middleware';
import { rerouteImpl } from "@astrojs/vercel/serverless/reroute.js"
export default async function middleware(request, context) {
const url = new URL(request.url);
const ctx = createContext({
request,
params: {}
params: {},
rerouteImpl,
});
ctx.locals = ${handlerTemplateCall};
const next = async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/integrations/vercel/src/serverless/reroute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { trySerializeLocals } from 'astro/middleware';
import { ASTRO_LOCALS_HEADER } from './adapter.js';

export function rerouteImpl(path: string | URL, context: { request: Request, locals: Record<string, any> }) {
const newUrl = new URL(path, context.request.url);
const newHeaders = { ...context.request.headers, [ASTRO_LOCALS_HEADER]: trySerializeLocals(context.locals) };
const newRequest = new Request(newUrl, { ...context.request, headers: newHeaders });
// TODO: should not handle external requests
return fetch(newRequest);
}

0 comments on commit 05c4844

Please sign in to comment.