Skip to content

Commit

Permalink
fix(s3-static-assets): prevent client cache of static pages (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozf committed May 30, 2020
1 parent 5714297 commit 647d74a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
11 changes: 8 additions & 3 deletions packages/s3-static-assets/src/index.ts
Expand Up @@ -3,7 +3,10 @@ import path from "path";
import fse from "fs-extra";
import readDirectoryFiles from "./lib/readDirectoryFiles";
import filterOutDirectories from "./lib/filterOutDirectories";
import { IMMUTABLE_CACHE_CONTROL_HEADER } from "./lib/constants";
import {
IMMUTABLE_CACHE_CONTROL_HEADER,
NO_STORE_CACHE_CONTROL_HEADER
} from "./lib/constants";
import S3ClientFactory, { Credentials } from "./lib/s3";
import pathToPosix from "./lib/pathToPosix";
import { PrerenderManifest } from "next/dist/build/index";
Expand Down Expand Up @@ -66,7 +69,8 @@ const uploadStaticAssets = async (
/^pages\//,
""
)}`,
filePath: pageFilePath
filePath: pageFilePath,
cacheControl: NO_STORE_CACHE_CONTROL_HEADER
});
});

Expand Down Expand Up @@ -105,7 +109,8 @@ const uploadStaticAssets = async (

return s3.uploadFile({
s3Key: path.posix.join("static-pages", relativePageFilePath),
filePath: pageFilePath
filePath: pageFilePath,
cacheControl: NO_STORE_CACHE_CONTROL_HEADER
});
});

Expand Down
2 changes: 2 additions & 0 deletions packages/s3-static-assets/src/lib/constants.ts
Expand Up @@ -4,3 +4,5 @@ export const IMMUTABLE_CACHE_CONTROL_HEADER =
export const DEFAULT_PUBLIC_DIR_CACHE_CONTROL =
"public, max-age=31536000, must-revalidate";
export const DEFAULT_PUBLIC_DIR_CACHE_REGEX = /\.(gif|jpe?g|jp2|tiff|png|webp|bmp|svg|ico)$/i;

export const NO_STORE_CACHE_CONTROL_HEADER = "no-store";
11 changes: 6 additions & 5 deletions packages/s3-static-assets/tests/upload-assets.test.ts
Expand Up @@ -2,7 +2,8 @@ import path from "path";
import uploadStaticAssets from "../src/index";
import {
IMMUTABLE_CACHE_CONTROL_HEADER,
DEFAULT_PUBLIC_DIR_CACHE_CONTROL
DEFAULT_PUBLIC_DIR_CACHE_CONTROL,
NO_STORE_CACHE_CONTROL_HEADER
} from "../src/lib/constants";
import AWS, {
mockGetBucketAccelerateConfigurationPromise,
Expand Down Expand Up @@ -136,15 +137,15 @@ describe.each`
expect.objectContaining({
Key: "static-pages/todos/terms.html",
ContentType: "text/html",
CacheControl: undefined
CacheControl: NO_STORE_CACHE_CONTROL_HEADER
})
);

expect(mockUpload).toBeCalledWith(
expect.objectContaining({
Key: "static-pages/todos/terms/[section].html",
ContentType: "text/html",
CacheControl: undefined
CacheControl: NO_STORE_CACHE_CONTROL_HEADER
})
);
});
Expand Down Expand Up @@ -184,15 +185,15 @@ describe.each`
expect.objectContaining({
Key: "static-pages/todos/terms/a.html",
ContentType: "text/html",
CacheControl: undefined
CacheControl: NO_STORE_CACHE_CONTROL_HEADER
})
);

expect(mockUpload).toBeCalledWith(
expect.objectContaining({
Key: "static-pages/todos/terms/b.html",
ContentType: "text/html",
CacheControl: undefined
CacheControl: NO_STORE_CACHE_CONTROL_HEADER
})
);
});
Expand Down

0 comments on commit 647d74a

Please sign in to comment.