NuxtN
Nuxt2mo ago
4 replies
Zusor

Video playback weird when using `serveStatic` instead of `publicAssets`

When serving video files with serveStatic:
import { defineEventHandler, serveStatic } from "h3";
import { stat, readFile } from "node:fs/promises";
import { join, resolve } from "node:path";

export default defineEventHandler((event) => {
  const runtimeConfig = useRuntimeConfig();
  const publicDir = resolve(runtimeConfig.paths.drive);

  function cleanupId(id: string) {
    return id.replace("/content", "");
  }

  return serveStatic(event, {
    getContents: (id) => {
      return readFile(join(publicDir, cleanupId(id)));
    },

    getMeta: async (id) => {
      const stats = await stat(join(publicDir, cleanupId(id))).catch(() => {});

      if (!stats || !stats.isFile()) {
        return;
      }

      return {
        size: stats.size,
        mtime: stats.mtimeMs,
      };
    },
  });
});

Video playback seems weird, I can't jump forward in the video and generally everything behaves in a strange way.
If I use publicAssets (which isn't realistic in production) it works perfectly fine with the same video files.
Is there a way I can get serveStatic to behave correctly?
Was this page helpful?