Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fastify): serve index file when it exists #1587

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/loaders/fastify.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ export class FastifyLoader extends AbstractLoader {
const renderFn = (req: any, res: any) => {
fs.stat(indexFilePath, (err) => {
if (err) {
const stream = fs.createReadStream(indexFilePath);
if (options.serveStaticOptions?.setHeaders) {
const stat = fs.statSync(indexFilePath);
options.serveStaticOptions.setHeaders(res, indexFilePath, stat);
}
res.type('text/html').send(stream);
} else {
const error = new NotFoundException();
res.status(error.getStatus()).send(error.getResponse());
return;
}

const stream = fs.createReadStream(indexFilePath);

if (options.serveStaticOptions?.setHeaders) {
const stat = fs.statSync(indexFilePath);
options.serveStaticOptions.setHeaders(res, indexFilePath, stat);
}

res.type('text/html').send(stream);
});
};

Expand Down
13 changes: 0 additions & 13 deletions tests/e2e/fastify-adapter.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,6 @@ describe('Fastify adapter', () => {
});
});

describe('when trying to get a non-existing file', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should remain the default behavior unless a flag (TBD which flag) is enabled. For express it is fallthrough: true so I guess we could use it for Fastify too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I think it's a good idea, I just made a commit to re-use this flag on fastify, I also updated the tests, it seems to cover all the cases. Tell me what you think

it('should return 404', async () => {
return app
.inject({
method: 'GET',
url: '/404'
})
.then((result) => {
expect(result.statusCode).toEqual(404);
});
});
});

afterAll(async () => {
await app.close();
});
Expand Down