Skip to content

Commit e961db0

Browse files
danielcondemarinsclaughl
authored andcommitted
html pages now upload
1 parent 8517f0c commit e961db0

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import AWS from "aws-sdk";
66

77
type UploadStaticAssetsOptions = {
88
bucketName: string;
9-
nextAppDir: string;
9+
nextConfigDir: string;
1010
};
1111
const filePathToS3Key = (filePath: string): string => {
1212
const relevantFilePathPart = filePath.substring(
@@ -29,11 +29,11 @@ const readDirectoryFiles = (directory: string): Promise<Array<Item>> => {
2929
const uploadStaticAssets = async (
3030
options: UploadStaticAssetsOptions
3131
): Promise<void> => {
32-
const { bucketName, nextAppDir } = options;
32+
const { bucketName, nextConfigDir } = options;
3333

3434
const s3 = new AWS.S3();
3535

36-
const dotNextDirectory = path.join(nextAppDir, ".next");
36+
const dotNextDirectory = path.join(nextConfigDir, ".next");
3737

3838
const BUILD_ID = fse
3939
.readFileSync(path.join(dotNextDirectory, "BUILD_ID"))
@@ -63,6 +63,30 @@ const uploadStaticAssets = async (
6363
.promise();
6464
});
6565

66+
const pagesManifest = await fse.readJSON(
67+
path.join(dotNextDirectory, "serverless/pages-manifest.json")
68+
);
69+
70+
const htmlPageUploadTasks = Object.values(pagesManifest)
71+
.filter(pageFile => (pageFile as string).endsWith(".html"))
72+
.map(async pageFile => {
73+
const fileBody = await fse.readFile(
74+
path.join(dotNextDirectory, `serverless/${pageFile}`)
75+
);
76+
77+
return s3
78+
.upload({
79+
Bucket: bucketName,
80+
Key: `static-pages/${(pageFile as string).replace(/^pages\//, "")}`,
81+
Body: fileBody,
82+
ContentType: "text/html",
83+
CacheControl: undefined
84+
})
85+
.promise();
86+
});
87+
88+
uploadTasks.concat(htmlPageUploadTasks);
89+
6690
await Promise.all(uploadTasks);
6791
// read public/ folder and upload files
6892
// read static/ folder and upload files

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe("Upload assets tests", () => {
1111
beforeEach(async () => {
1212
await uploadStaticAssets({
1313
bucketName: "test-bucket-name",
14-
nextAppDir: path.join(__dirname, "./fixtures/basic-next-app")
14+
nextConfigDir: path.join(__dirname, "./fixtures/basic-next-app")
1515
});
1616
});
1717

@@ -36,17 +36,21 @@ describe("Upload assets tests", () => {
3636
});
3737

3838
it("uploads prerendered HTML pages specified in pages manifest", async () => {
39-
expect(mockUpload).toBeCalledWith({
40-
Key: "static-pages/todos/terms.html",
41-
ContentType: "text/html",
42-
CacheControl: undefined
43-
});
39+
expect(mockUpload).toBeCalledWith(
40+
expect.objectContaining({
41+
Key: "static-pages/todos/terms.html",
42+
ContentType: "text/html",
43+
CacheControl: undefined
44+
})
45+
);
4446

45-
expect(mockUpload).toBeCalledWith({
46-
Key: "static-pages/todos/terms/[section].html",
47-
Body: expect.any(Buffer),
48-
ContentType: "text/html",
49-
CacheControl: undefined
50-
});
47+
expect(mockUpload).toBeCalledWith(
48+
expect.objectContaining({
49+
Key: "static-pages/todos/terms/[section].html",
50+
Body: expect.any(Buffer),
51+
ContentType: "text/html",
52+
CacheControl: undefined
53+
})
54+
);
5155
});
5256
});

0 commit comments

Comments
 (0)