NuxtN
Nuxt9mo ago
22 replies
Equinox

Module middleware doesn't work in build mode

Hi, I'm trying to build a npm package that would plug into nuxt at the server-side level and watch every GET request to run some analytics

so far I've limited myself to just console logging to check if requests are intercepted in the right way

I don't understand why but everything works fine in dev mode, however as soon as I build not a single request is caught, i don't see the console logs anymore, it feels like the middleware is not packaged into the build

I've tried adding {build:transpile: ['@askdoppler/nuxt']}} to my nuxt.config.ts on the test app i'm trying the package on but it doesn't work either

i'm kinda lost if anyone has suggestions it'd be welcome

index.ts

import module from "./module.js";

export default module;


module.ts
import { defineNuxtModule, addServerHandler, createResolver } from "@nuxt/kit";

export default defineNuxtModule({
  meta: {
    name: "@askdoppler/nuxt",
    configKey: "doppler",
  },
  defaults: {
    apiKey: "",
  },
  setup(options, nuxt) {
    console.log("Middleware setup");

    const { resolve } = createResolver(import.meta.url);
    nuxt.options.build.transpile.push(resolve("runtime"));

    addServerHandler({
      middleware: true,
      handler: resolve("./runtime/middleware"),
    });
  },
});


/runtime/middleware.ts
import { defineEventHandler, type H3Event } from "h3";
import { getSource, logCrawl } from "@askdoppler/core";

export default defineEventHandler((e: H3Event) => {
  try {
    console.log("Middlewre activated");
  } catch (error) {
    console.error("Middleware error:", error);
  }
});
Was this page helpful?