Skip to content

Commit

Permalink
feat: disable static file server in lambda function
Browse files Browse the repository at this point in the history
  • Loading branch information
aheissenberger committed Dec 20, 2024
1 parent f8987ae commit b0e31be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/hello-world-aws/cdk/lib/react-server-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export class ReactServerStack extends cdk.Stack {
},

architecture: lambda.Architecture.ARM_64,
memorySize: 256,
memorySize: 512,
timeout: cdk.Duration.seconds(10),
logRetention: logs.RetentionDays.THREE_DAYS,
tracing: lambda.Tracing.ACTIVE,
//tracing: lambda.Tracing.ACTIVE,
});

const integration = new HttpLambdaIntegration(
Expand Down
1 change: 1 addition & 0 deletions packages/react-server-adapter-aws/functions/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { awsLambdaAdapter as lambdaHandler } from "@lazarv/react-server-adapter-
const rsHandler = await createHandler({
origin: process.env.ORIGIN || "http://localhost:3000",
outDir: process.env?.OUT_DIR,
serveStaticFiles: false,
});

export const handler = lambdaHandler(rsHandler);
7 changes: 7 additions & 0 deletions packages/react-server-adapter-aws/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export const adapter = createAdapter({
"awsLambdaAdapterStreaming"
);
}
const serveStaticFiles = adapterOptions?.serveStaticFiles ?? false;
if (serveStaticFiles) {
entryFileContent = entryFileContent.replace(
"serveStaticFiles: false",
"serveStaticFiles: true"
);
}

await clearDirectory(outServerDir);
await mkdir(outServerDir, { recursive: true });
Expand Down
30 changes: 18 additions & 12 deletions packages/react-server-adapter-aws/libs/create-middleware.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export async function createMiddleware(root, options) {
options.outDir = ".react-server";
}

const serveStaticFiles = options.serveStaticFiles ?? false;

const workerUrl = resolve(
join(
options.outDir,
Expand All @@ -54,7 +56,7 @@ export async function createMiddleware(root, options) {
};
runtime$(
typeof config.runtime === "function"
? config.runtime(initialRuntime) ?? initialRuntime
? (config.runtime(initialRuntime) ?? initialRuntime)
: {
...initialRuntime,
...config.runtime,
Expand All @@ -66,18 +68,22 @@ export async function createMiddleware(root, options) {
const initialHandlers = [
urlParser,
async () => PrerenderStorage.enterWith({}),
await staticHandler(join(cwd, options.outDir, "dist"), {
cwd: join(options.outDir, "dist"),
}),
await staticHandler("{client,assets}", { cwd: options.outDir }),
await staticHandler(join(cwd, options.outDir), {
cwd: options.outDir,
}),
...(config.public !== false
...(serveStaticFiles
? [
await staticHandler(join(cwd, publicDir), {
cwd: publicDir,
await staticHandler(join(cwd, options.outDir, "dist"), {
cwd: join(options.outDir, "dist"),
}),
await staticHandler("{client,assets}", { cwd: options.outDir }),
await staticHandler(join(cwd, options.outDir), {
cwd: options.outDir,
}),
...(config.public !== false
? [
await staticHandler(join(cwd, publicDir), {
cwd: publicDir,
}),
]
: []),
]
: []),
await trailingSlashHandler(),
Expand All @@ -94,7 +100,7 @@ export async function createMiddleware(root, options) {

const middleware = compose(
typeof config.handlers === "function"
? config.handlers(initialHandlers) ?? initialHandlers
? (config.handlers(initialHandlers) ?? initialHandlers)
: [...initialHandlers, ...(config.handlers ?? [])]
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ export class ReactServerStack extends cdk.Stack {
},

architecture: lambda.Architecture.ARM_64,
memorySize: 256,
memorySize: 512,
timeout: cdk.Duration.seconds(10),
logRetention: logs.RetentionDays.THREE_DAYS,
tracing: lambda.Tracing.ACTIVE,
//tracing: lambda.Tracing.ACTIVE,
});

const integration = new HttpLambdaIntegration(
Expand Down

0 comments on commit b0e31be

Please sign in to comment.