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

Commit 187bbc8

Browse files
authored
fix(lambda-at-edge): fail build when "public/static" folder exists as this conflicts with static/* cache behavior (#709)
1 parent b1f614a commit 187bbc8

File tree

15 files changed

+54
-0
lines changed

15 files changed

+54
-0
lines changed

packages/libs/lambda-at-edge/src/build.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@ class Builder {
623623
return copyIfExists(source, destination);
624624
});
625625

626+
// Check if public/static exists and fail build since this conflicts with static/* behavior.
627+
if (await fse.pathExists(path.join(nextStaticDir, "public", "static"))) {
628+
throw new Error(
629+
"You cannot have assets in the directory [public/static] as they conflict with the static/* CloudFront cache behavior. Please move these assets into another directory."
630+
);
631+
}
632+
626633
const buildPublicOrStaticDirectory = async (
627634
directory: "public" | "static"
628635
) => {

packages/libs/lambda-at-edge/tests/build/build.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,31 @@ describe("Builder Tests", () => {
341341
});
342342
});
343343

344+
describe("Build edge cases", () => {
345+
let builder: Builder;
346+
347+
beforeEach(async () => {
348+
const mockExeca = execa as jest.Mock;
349+
mockExeca.mockResolvedValueOnce();
350+
351+
fseRemoveSpy = jest.spyOn(fse, "remove").mockImplementation(() => {
352+
return;
353+
});
354+
fseEmptyDirSpy = jest.spyOn(fse, "emptyDir");
355+
});
356+
357+
it("fails build when there is a public/static directory that conflicts with static/* behavior", async () => {
358+
const fixturePath = join(
359+
__dirname,
360+
"./simple-app-fixture-public-static-error"
361+
);
362+
builder = new Builder(fixturePath, outputDir, {});
363+
await expect(builder.build()).rejects.toThrow(
364+
"You cannot have assets in the directory [public/static] as they conflict with the static/* CloudFront cache behavior. Please move these assets into another directory."
365+
);
366+
});
367+
});
368+
344369
describe("Custom handler", () => {
345370
let fseRemoveSpy: jest.SpyInstance;
346371
let fseEmptyDirSpy: jest.SpyInstance;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-build-id
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Having a cache/ folder allows to test the cleanup of .next/ except for cache/ for faster builds
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": 2,
3+
"routes": {
4+
"/": {
5+
"initialRevalidateSeconds": false,
6+
"srcRoute": null,
7+
"dataRoute": "/_next/data/zsWqBqLjpgRmswfQomanp/index.json"
8+
},
9+
"/contact": {
10+
"initialRevalidateSeconds": false,
11+
"srcRoute": null,
12+
"dataRoute": "/_next/data/zsWqBqLjpgRmswfQomanp/contact.json"
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"pages404":true,"basePath":"","redirects":[],"rewrites":[],"headers":[],"dynamicRoutes":[]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"/": "pages/index.js"
3+
}

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/.next/serverless/pages/index.html

Whitespace-only changes.

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/.next/serverless/pages/index.js

Whitespace-only changes.

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/.next/serverless/pages/index.json

Whitespace-only changes.

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/.next/static/chunks/chunk1.js

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { target: "serverless" };

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/public/static/donotdelete.txt

Whitespace-only changes.

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/public/sw.js

Whitespace-only changes.

packages/libs/lambda-at-edge/tests/build/simple-app-fixture-public-static-error/static/donotdelete.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)