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

Commit 062b968

Browse files
upload chunks and runtime build files
1 parent 2b74304 commit 062b968

File tree

9 files changed

+73
-4
lines changed

9 files changed

+73
-4
lines changed

packages/s3-static-assets/dist/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ const uploadStaticAssets = (options) => __awaiter(void 0, void 0, void 0, functi
4242
cacheControl: constants_1.IMMUTABLE_CACHE_CONTROL_HEADER
4343
});
4444
}));
45+
const buildManifest = yield fs_extra_1.default.readJson(path_1.default.join(dotNextDirectory, "build-manifest.json"));
46+
const buildManifestFileUploads = Object.values(buildManifest.pages)
47+
.reduce((acc, pageBuildFiles) => {
48+
return acc.concat(pageBuildFiles);
49+
}, [])
50+
.map(relativeFilePath => {
51+
return s3.uploadFile({
52+
s3Key: `_next/${relativeFilePath}`,
53+
filePath: path_1.default.join(dotNextDirectory, relativeFilePath),
54+
cacheControl: constants_1.IMMUTABLE_CACHE_CONTROL_HEADER
55+
});
56+
});
4557
const pagesManifest = yield fs_extra_1.default.readJSON(path_1.default.join(dotNextDirectory, "serverless/pages-manifest.json"));
4658
const htmlPageUploads = Object.values(pagesManifest)
4759
.filter(pageFile => pageFile.endsWith(".html"))
@@ -67,6 +79,7 @@ const uploadStaticAssets = (options) => __awaiter(void 0, void 0, void 0, functi
6779
const staticDirUploads = yield uploadPublicOrStaticDirectory("static");
6880
const allUploads = [
6981
...nextBuildFileUploads,
82+
...buildManifestFileUploads,
7083
...htmlPageUploads,
7184
...publicDirUploads,
7285
...staticDirUploads

packages/s3-static-assets/src/index.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ type UploadStaticAssetsOptions = {
1313
credentials: Credentials;
1414
};
1515

16+
type NextBuildManifest = {
17+
pages: {
18+
[pageRoute: string]: string[];
19+
};
20+
};
21+
1622
const uploadStaticAssets = async (
1723
options: UploadStaticAssetsOptions
1824
): Promise<AWS.S3.ManagedUpload.SendData[]> => {
@@ -49,6 +55,22 @@ const uploadStaticAssets = async (
4955
});
5056
});
5157

58+
const buildManifest: NextBuildManifest = await fse.readJson(
59+
path.join(dotNextDirectory, "build-manifest.json")
60+
);
61+
62+
const buildManifestFileUploads = Object.values(buildManifest.pages)
63+
.reduce((acc, pageBuildFiles) => {
64+
return acc.concat(pageBuildFiles);
65+
}, [])
66+
.map(relativeFilePath => {
67+
return s3.uploadFile({
68+
s3Key: `_next/${relativeFilePath}`,
69+
filePath: path.join(dotNextDirectory, relativeFilePath),
70+
cacheControl: IMMUTABLE_CACHE_CONTROL_HEADER
71+
});
72+
});
73+
5274
const pagesManifest = await fse.readJSON(
5375
path.join(dotNextDirectory, "serverless/pages-manifest.json")
5476
);
@@ -94,10 +116,11 @@ const uploadStaticAssets = async (
94116
const staticDirUploads = await uploadPublicOrStaticDirectory("static");
95117

96118
const allUploads = [
97-
...nextBuildFileUploads,
98-
...htmlPageUploads,
99-
...publicDirUploads,
100-
...staticDirUploads
119+
...nextBuildFileUploads, // .next/static/BUILD_ID/*
120+
...buildManifestFileUploads, // .next/static/runtime/x.js, //.next/static/chunks/y.js ... as specified in build-manifest.json
121+
...htmlPageUploads, // prerendered HTML pages
122+
...publicDirUploads, // app public dir
123+
...staticDirUploads // app static dir
101124
];
102125

103126
return Promise.all(allUploads);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"pages": {}
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"pages": {
3+
"/": ["static/runtime/runtime1.js"],
4+
"/foo": ["static/chunks/chunk1.js"]
5+
}
6+
}

packages/s3-static-assets/tests/fixtures/basic-next-app/.next/static/chunks/chunk1.js

Whitespace-only changes.

packages/s3-static-assets/tests/fixtures/basic-next-app/.next/static/runtime/runtime1.js

Whitespace-only changes.

packages/s3-static-assets/tests/upload-assets.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ describe("Upload tests", () => {
157157
);
158158
});
159159

160+
it("uploads files specified in the build-manifest.json", async () => {
161+
await upload();
162+
163+
expect(mockUpload).toBeCalledWith(
164+
expect.objectContaining({
165+
Key: "_next/static/chunks/chunk1.js",
166+
CacheControl: IMMUTABLE_CACHE_CONTROL_HEADER
167+
})
168+
);
169+
170+
expect(mockUpload).toBeCalledWith(
171+
expect.objectContaining({
172+
Key: "_next/static/runtime/runtime1.js",
173+
CacheControl: IMMUTABLE_CACHE_CONTROL_HEADER
174+
})
175+
);
176+
});
177+
160178
describe("when no public or static directory exists", () => {
161179
it("upload does not crash", () => upload("./fixtures/app-no-public-dir"));
162180
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"pages": {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"pages": {}
3+
}

0 commit comments

Comments
 (0)