NuxtN
Nuxt12mo ago
kogratte

Third party static content

I'm working on a nuxt project which I now want to extend using a third party generated content (a static website generated using another toolchain).
What would be the best way to go? I'm not able to properly use this kind of module (it didn't work):

export default defineNuxtModule({
  setup() {
    addServerHandler({
      route: `docs/*`,
      handler: './docs/index.ts'
    });
  }
});

With ./docs/index.tsbeing defined as below:
import { defineEventHandler, serveStatic } from 'h3';
import { stat, readFile } from 'node:fs/promises';
import { join } from 'pathe';

export default defineEventHandler((event) => {
  const publicDir = '../public/docs/';

  return serveStatic(event, {
    getContents: (id) => readFile(join(publicDir, id || 'index.html')),
    getMeta: async (id) => {
      const stats = await stat(join(publicDir, id)).catch(() => {});

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

      return {
        size: stats.size,
        mtime: stats.mtimeMs
      };
    }
  });
});
Was this page helpful?