Skip to content

Commit

Permalink
feat(): apply headers to index.html served as fallback, close #242
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Jun 17, 2020
1 parent fc0da23 commit 532ca90
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/loaders/express.loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import * as fs from 'fs';
import { AbstractHttpAdapter } from '@nestjs/core';
import { ServeStaticModuleOptions } from '../interfaces/serve-static-options.interface';
import {
Expand Down Expand Up @@ -27,6 +28,13 @@ export class ExpressLoader extends AbstractLoader {

const renderFn = (req: unknown, res: any, next: Function) => {
if (!isRouteExcluded(req, options.exclude)) {
if (
options.serveStaticOptions &&
options.serveStaticOptions.setHeaders
) {
const stat = fs.statSync(indexFilePath);
options.serveStaticOptions.setHeaders(res, indexFilePath, stat);
}
res.sendFile(indexFilePath);
} else {
next();
Expand Down
7 changes: 7 additions & 0 deletions lib/loaders/fastify.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ export class FastifyLoader extends AbstractLoader {
});
app.get(options.renderPath, (req: any, res: any) => {
const stream = fs.createReadStream(indexFilePath);
if (
options.serveStaticOptions &&
options.serveStaticOptions.setHeaders
) {
const stat = fs.statSync(indexFilePath);
options.serveStaticOptions.setHeaders(res, indexFilePath, stat);
}
res.type('text/html').send(stream);
});
}
Expand Down

0 comments on commit 532ca90

Please sign in to comment.