-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serverless-component): custom cache control config. for next pub…
…lic directory
- Loading branch information
Showing
20 changed files
with
352 additions
and
90 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export const IMMUTABLE_CACHE_CONTROL_HEADER = | ||
"public, max-age=31536000, immutable"; | ||
|
||
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; |
48 changes: 48 additions & 0 deletions
48
packages/s3-static-assets/src/lib/getPublicAssetCacheControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import path from "path"; | ||
import regexParser from "regex-parser"; | ||
import { | ||
DEFAULT_PUBLIC_DIR_CACHE_CONTROL, | ||
DEFAULT_PUBLIC_DIR_CACHE_REGEX | ||
} from "./constants"; | ||
|
||
export type PublicDirectoryCache = | ||
| boolean | ||
| { | ||
test?: string; | ||
value?: string; | ||
}; | ||
|
||
/** | ||
* If options is not present, or is explicitly set to true, returns a default Cache-Control configuration for image types. | ||
* If options is explicitly set to false, it returns undefined. | ||
* If assigned an options object, it uses whichever value is defined there, falling back to the default if one is not present. | ||
*/ | ||
const getPublicAssetCacheControl = ( | ||
filePath: string, | ||
options?: PublicDirectoryCache | ||
): string | undefined => { | ||
if (options === false) { | ||
return undefined; | ||
} | ||
|
||
let value: string = DEFAULT_PUBLIC_DIR_CACHE_CONTROL; | ||
let test: RegExp = DEFAULT_PUBLIC_DIR_CACHE_REGEX; | ||
|
||
if (typeof options === "object") { | ||
if (options.value) { | ||
value = options.value; | ||
} | ||
|
||
if (options.test) { | ||
test = regexParser(options.test); | ||
} | ||
} | ||
|
||
if (test.test(path.basename(filePath))) { | ||
return value; | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export default getPublicAssetCacheControl; |
4 changes: 4 additions & 0 deletions
4
...ages/s3-static-assets/tests/fixtures/app-with-images/.next/serverless/pages-manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"/todos/terms": "pages/todos/terms.html", | ||
"/todos/terms/[section]": "pages/todos/terms/[section].html" | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file added
BIN
+88 Bytes
packages/s3-static-assets/tests/fixtures/app-with-images/public/1x1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+88 Bytes
packages/s3-static-assets/tests/fixtures/app-with-images/static/1x1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.