Skip to content

Document workaround for slow HEAD #51

@Pyrolistical

Description

@Pyrolistical

When this library loads a database file, it does a HEAD to get the total length of the database file. On some dev servers the etag is calculated, which in turns reads the entire database file. For large databases this is very slow, on the order of seconds.

I found this to be the case when using vite on windows. I wrote a small vite plugin which intercepts these HEAD calls and just returns Content-Length

import fs from "node:fs";

const SkipHeadEtag = () => ({
  name: "skip-head-etag",
  configureServer(server) {
    server.middlewares.use((req, res, next) => {
      if (req.method === "HEAD") {
        const { size } = fs.statSync(req.url.slice(1));
        res.setHeader("Accept-Ranges", "bytes");
        res.setHeader("Cache-Control", "no-cache");
        res.setHeader("Content-Length", String(size));
        res.end();
        return;
      }
      next();
    });
  },
});

That plugin is incomplete. It doesn't handle 404 properly, but shows the main idea of the workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions