How to serve multiple SPA in nestjs using fastify

1.1k Views Asked by At

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.

1

There are 1 best solutions below

3
On

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.