Return embedded file through API with caching Last updated: 27. Nov 2025

Reads a logo image file from a #/Config file and returns it to the client with caching.

Example

Code #/API/Logo.js

() => {

    let c = docly.getFile("#/Config");
    let logo = c.Logo;
    let parts = logo.split(".");
    let ext = parts[parts.length - 1];
    docly.setMime("image/" + ext);

    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);

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

            // If the difference in seconds is less than 1, set the result code to 304 (Not Modified)
            return dateDifference.TotalSeconds < 1;
        }
        return false;

    }

    // Sjekk om klienten allerede har en oppdatert versjon
    if (checkIfCached()) return docly.setResultCode(304);

    return docly.downloadEmbedded("#/Config", logo, true);
}