Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

fix support for next/dynamic import #426

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import pathToPosix from "./lib/pathToPosix";
import expressifyDynamicRoute from "./lib/expressifyDynamicRoute";
import pathToRegexStr from "./lib/pathToRegexStr";
import createServerlessConfig from "./lib/createServerlessConfig";
import isChunk from "./lib/isChunk";
import pathToFilename from "./lib/pathToFilename";

export const DEFAULT_LAMBDA_CODE_DIR = "default-lambda";
export const API_LAMBDA_CODE_DIR = "api-lambda";
Expand Down Expand Up @@ -123,9 +125,14 @@ class Builder {
const resolvedFilePath = path.resolve(filePath);
const dst = path.relative(this.nextConfigDir, resolvedFilePath);

// chunks are geting copied to root. others remain same
return fse.copy(
resolvedFilePath,
join(this.outputDir, handlerDirectory, dst)
join(
this.outputDir,
handlerDirectory,
isChunk(filePath) ? pathToFilename(dst) : dst
)
);
});
}
Expand Down
5 changes: 5 additions & 0 deletions packages/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export const handler = async (

const isHTMLPage = isStaticPage || isPrerenderedPage;

if (uri === "/service-worker.js") {
s3Origin.path = "/_next/static";
return request;
}

if (isHTMLPage || isPublicFile) {
s3Origin.path = isHTMLPage ? "/static-pages" : "/public";

Expand Down
6 changes: 6 additions & 0 deletions packages/lambda-at-edge/src/lib/isChunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const isChunk = (path: string): boolean => {
// Identify .next/serverless/[number].[alpha_numeric].js pattern
return /^(.next\/serverless\/)[\d]+\.+[\w,\s-]+\.(js)$/.test(path);
};

export default isChunk;
2 changes: 2 additions & 0 deletions packages/lambda-at-edge/src/lib/pathToFilename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const pathToFilename = (path: string): string => path.replace(/^.*[\\\/]/, "");
export default pathToFilename;