Skip to content

Commit

Permalink
Merge pull request #74 from fl034/cache-control-strapiv4
Browse files Browse the repository at this point in the history
Add cache control setting
  • Loading branch information
jakeFeldman authored Sep 9, 2023
2 parents e3a1d76 + 95b71c4 commit eee32db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -64,6 +65,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 @@ -124,6 +126,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)
},
};

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

0 comments on commit eee32db

Please sign in to comment.