0

The configuration mentioned below works absolutely fine with express but it is giving error when used with fastify.

export const serveStaticModule_one: ServeStaticModuleOptions = {
    rootPath: join(process.cwd(), 'one')
};

export const serveStaticModule_two: ServeStaticModuleOptions = {
    rootPath: join(process.cwd(), 'two'),
    renderPath: '/two'
};

export const serveStaticModule_three: ServeStaticModuleOptions = {
    rootPath: join(process.cwd(), 'three'),
    renderPath: '/three',
    serveStaticOptions: {
        index: 'client.html'
    }
};
package used version
@nestjs/platform-fastify 8.2.3
@nestjs/serve-static 2.2.2
fastify-static 4.5.0

Exact error is The decorator 'sendFile' has already been added
I tried passing these object to single ServeStaticModule.forRoot() and to multiple, but none of them works.

Amit Kumar
  • 620
  • 4
  • 17

1 Answers1

0

I found the solution by creating new type.

type FastifyServeStaticModuleOptions = _ServeStaticModuleOptions & {
    serveStaticOptions: {
        decorateReply: boolean;
    };
};

then i created object like this:

export const serveStaticModule_one: FastifyServeStaticModuleOptions = {
    rootPath: join(process.cwd(), 'one'),
    serveStaticOptions: {
        decorateReply: false
    }
};

It worked perfectly after this.

Amit Kumar
  • 620
  • 4
  • 17
  • I have a very similar issue after creating the type like you mentioned, the error goes away but only one SPA (default one) is working. `export const serveStaticModule_one: FastifyServeStaticModuleOptions = { rootPath: join(__dirname, '..', '..', 'customer'), serveStaticOptions: { decorateReply: false } };` `export const serveStaticModule_two: FastifyServeStaticModuleOptions = { rootPath: join(__dirname, '..', '..', 'admin'), serveRoot: '/admin', serveStaticOptions: { decorateReply: false } };` Finally added them in the import section separately.Any idea? – A J Qarshi Mar 24 '22 at 15:07
  • i will try to recreate this situation tomorrow.... however i think issue is with serveRoot or renderPath... I am always confused with them! can you try to fiddle with these options? – Amit Kumar Mar 25 '22 at 17:25
  • I have looked into them and `serveRoot` is the one which I should be using. This indicates the path under which the SPA app will be served. `renderPath` (set to `*` by default which means all the paths) is concatenated with the `serveRoot`. when set to *, module returns index.html file in response. – A J Qarshi Mar 30 '22 at 06:25