Disable cache of admin panels index.html

default discord avatar
chiefkoshi11 months ago

Hey!



Is there any way I can disable the admin panel's index.html cache or add s-max-age=0? I use Koyeb for deployments and with each deployment I am faced with a white screen of death as its trying to load old hashes of the JS files. The index.html file seems to always be cached.



Solved it by hijacking res.end and res.write.



app.use("*", (req, res, next) => {
    if (req.path.includes("api")) return next();
    // Force no cache for admin
    // Hijack res.write
    const originalWrite = res.write;
    const originalEnd = res.end;
    const originalSend = res.send;
    //@ts-expect-error
    res.write = (...restArgs) => {
        if (
            !res.headersSent &&
            String(res.getHeader("Content-Type")).includes("text/html")
        ) {
            res.set(
                "Cache-Control",
                "s-maxage=0, max-age=0, must-revalidate, no-cache, no-store",
            );
        }
        originalWrite.apply(res, restArgs);
    };

    // Hijack res.end
    //@ts-expect-error
    res.end = (...restArgs) => {
        if (
            !res.headersSent &&
            String(res.getHeader("Content-Type")).includes("text/html")
        ) {
            res.set(
                "Cache-Control",
                "s-maxage=0, max-age=0, must-revalidate, no-cache, no-store",
            );
        }
        originalEnd.apply(res, restArgs);
    };

    // Hijack res.send
    //@ts-expect-error
    res.send = (...restArgs) => {
        if (
            !res.headersSent &&
            String(res.getHeader("Content-Type")).includes("text/html")
        ) {
            res.set(
                "Cache-Control",
                "s-maxage=0, max-age=0, must-revalidate, no-cache, no-store",
            );
        }
        originalSend.apply(res, restArgs);
    };

    next();
});
    Star on GitHub

    Star

    Chat on Discord

    Discord

    online

    Can't find what you're looking for?

    Get help straight from the Payload team with an Enterprise License.