Docly Developers
ExampleUpdated 15 Oct 2026

Return embedded file through API with caching

Reads a file embedded in the #/Config document and returns it as a download, answering 304 when the client already has the current version. Note: downloadEmbedded always sends the file as a download (Content-Disposition: attachment, Content-Type: application/octet-stream). It cannot be used as the src of an . To show an image on a page, store it in a published document and use docly.linkImage — that also gives you scaling and caching for free.

Example

Code #/API/Logo.js

export default () => {

    let c = docly.getFile("#/Config");
    if (!c || !c.Logo) throw new Error("No logo configured");
    let logo = c.Logo;
    let parts = logo.split(".");
    let ext = parts[parts.length - 1].toLowerCase();

    function checkIfCached() {
        let formattedFileModifiedDate = docly.format(c.LastModified, "R");
        docly.setHeader("Last-Modified", formattedFileModifiedDate);

        let ifModifiedSinceHeader = docly.getHeader("If-Modified-Since");
        if (ifModifiedSinceHeader) {
            // Convert the 'If-Modified-Since' header to a Date object
            let ifModifiedSinceDate = new Date(ifModifiedSinceHeader);

            // Difference between the 'If-Modified-Since' date and the document's last modified date
            let dateDifference = docly.dateDiff(ifModifiedSinceDate, c.LastModified);

            // Less than 1 second means the client already has this version
            return dateDifference.TotalSeconds < 1;
        }
        return false;
    }

    // The client already has the current version
    if (checkIfCached()) return docly.setResultCode(304);

    // Third parameter is the file name the download gets
    return docly.downloadEmbedded("#/Config", logo, "logo." + ext);
}