Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache control setting #74

Merged
merged 1 commit into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = ({ env }) => ({
containerName: env("STORAGE_CONTAINER_NAME"),
defaultPath: "assets",
cdnBaseURL: env("STORAGE_CDN_URL"), // optional
defaultCacheControl: env("STORAGE_CACHE_CONTROL") // optional
},
},
},
Expand All @@ -60,6 +61,7 @@ module.exports = ({ env }) => ({
| containerName | true | Container name |
| defaultPath | true | The path to use when there is none being specified. Defaults to `assets` |
| cdnBaseURL | false | CDN base url |
| defaultCacheControl | false | Cache-Control header value for all uploaded files |

### Security Middleware Configuration

Expand Down Expand Up @@ -120,6 +122,9 @@ When `serviceBaseURL` is not provided, default `https://${account}.blob.core.win

`cdnBaseURL` is optional, it is useful when using CDN in front of your storage account. Images will be returned with the CDN URL instead of the storage account URL.

`defaultCacheControl` is optional. It is useful when you want to allow clients to use a cached version of the file. Azure storage will return this value in the [`Cache-Control` HTTP-header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control?retiredLocale=de) of the response.


## Contributing

Contributions are welcome
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config = {
containerName: string;
defaultPath: string;
cdnBaseURL?: string;
defaultCacheControl?: string;
};

type StrapiFile = File & {
Expand Down Expand Up @@ -69,7 +70,10 @@ async function handleUpload(
const containerClient = blobSvcClient.getContainerClient(trimParam(config.containerName));
const client = containerClient.getBlockBlobClient(getFileName(config.defaultPath, file));
const options = {
blobHTTPHeaders: { blobContentType: file.mime },
blobHTTPHeaders: {
blobContentType: file.mime,
blobCacheControl: trimParam(config.defaultCacheControl)
jakeFeldman marked this conversation as resolved.
Show resolved Hide resolved
},
};

const cdnBaseURL = trimParam(config.cdnBaseURL);
Expand Down Expand Up @@ -121,6 +125,10 @@ module.exports = {
label: 'CDN base url (optional)',
type: 'text',
},
defaultCacheControl: {
label: 'Default cache-control setting for all uploaded files',
type: 'text',
},
},
init: (config: Config) => {
const blobSvcClient = makeBlobServiceClient(config);
Expand Down