Skip to content

Commit

Permalink
perf: omit search for immutable static requests (misskey-dev#13265)
Browse files Browse the repository at this point in the history
* perf: omit search for immutable static requests

* perf: also applies to /files

* fix: exclude /proxy

* /files/:key/*を301 redirectに

---------

Co-authored-by: tamaina <[email protected]>
(cherry picked from commit 309a943)

# Conflicts:
#	packages/backend/src/server/web/ClientServerService.ts
  • Loading branch information
acid-chicken authored and nacika-ins committed Sep 18, 2024
1 parent ebc33f8 commit 2ec1d0e
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 70 deletions.
9 changes: 9 additions & 0 deletions packages/backend/src/misc/fastify-hook-handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { onRequestHookHandler } from 'fastify';

export const handleRequestRedirectToOmitSearch: onRequestHookHandler = (request, reply, done) => {
const index = request.url.indexOf('?');
if (~index) {
reply.redirect(301, request.url.slice(0, index));
}
done();
};
32 changes: 18 additions & 14 deletions packages/backend/src/server/FileServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import { isMimeImage } from '@/misc/is-mime-image.js';
import { correctFilename } from '@/misc/correct-filename.js';
import { handleRequestRedirectToOmitSearch } from '@/misc/fastify-hook-handlers.js';
import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from 'fastify';

const _filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -62,20 +63,23 @@ export class FileServerService {
done();
});

fastify.get('/files/app-default.jpg', (request, reply) => {
const file = fs.createReadStream(`${_dirname}/assets/dummy.png`);
reply.header('Content-Type', 'image/jpeg');
reply.header('Cache-Control', 'max-age=31536000, immutable');
return reply.send(file);
});

fastify.get<{ Params: { key: string; } }>('/files/:key', async (request, reply) => {
return await this.sendDriveFile(request, reply)
.catch(err => this.errorHandler(request, reply, err));
});
fastify.get<{ Params: { key: string; } }>('/files/:key/*', async (request, reply) => {
return await this.sendDriveFile(request, reply)
.catch(err => this.errorHandler(request, reply, err));
fastify.register((fastify, options, done) => {
fastify.addHook('onRequest', handleRequestRedirectToOmitSearch);
fastify.get('/files/app-default.jpg', (request, reply) => {
const file = fs.createReadStream(`${_dirname}/assets/dummy.png`);
reply.header('Content-Type', 'image/jpeg');
reply.header('Cache-Control', 'max-age=31536000, immutable');
return reply.send(file);
});

fastify.get<{ Params: { key: string; } }>('/files/:key', async (request, reply) => {
return await this.sendDriveFile(request, reply)
.catch(err => this.errorHandler(request, reply, err));
});
fastify.get<{ Params: { key: string; } }>('/files/:key/*', async (request, reply) => {
return await reply.redirect(301, `${this.config.url}/files/${request.params.key}`);
});
done();
});

fastify.get<{
Expand Down
Loading

0 comments on commit 2ec1d0e

Please sign in to comment.