From fc56a56efca627b0fc18bef4b5434468c77cfad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 12 Feb 2024 14:36:54 +0900 Subject: [PATCH] perf: omit search for immutable static requests --- .../src/server/web/ClientServerService.ts | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/server/web/ClientServerService.ts b/packages/backend/src/server/web/ClientServerService.ts index 2592253b8a..6ad833bacc 100644 --- a/packages/backend/src/server/web/ClientServerService.ts +++ b/packages/backend/src/server/web/ClientServerService.ts @@ -41,7 +41,7 @@ import { ReversiGameEntityService } from '@/core/entities/ReversiGameEntityServi import { FeedService } from './FeedService.js'; import { UrlPreviewService } from './UrlPreviewService.js'; import { ClientLoggerService } from './ClientLoggerService.js'; -import type { FastifyInstance, FastifyPluginOptions, FastifyReply } from 'fastify'; +import type { FastifyInstance, FastifyPluginOptions, FastifyReply, onRequestHookHandler } from 'fastify'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); @@ -53,6 +53,14 @@ const swAssets = `${_dirname}/../../../../../built/_sw_dist_/`; const viteOut = `${_dirname}/../../../../../built/_vite_/`; const tarball = `${_dirname}/../../../../../built/tarball/`; +const handleRedirectToOmitSearch: onRequestHookHandler = (request, reply, done) => { + const index = request.url.indexOf('?'); + if (~index) { + reply.redirect(301, request.url.slice(0, index)); + } + done(); +}; + @Injectable() export class ClientServerService { private logger: Logger; @@ -253,11 +261,16 @@ export class ClientServerService { //#region vite assets if (this.config.clientManifestExists) { - fastify.register(fastifyStatic, { - root: viteOut, - prefix: '/vite/', - maxAge: ms('30 days'), - decorateReply: false, + fastify.register((fastify, options, done) => { + fastify.register(fastifyStatic, { + root: viteOut, + prefix: '/vite/', + maxAge: ms('30 days'), + immutable: true, + decorateReply: false, + }); + fastify.addHook('onRequest', handleRedirectToOmitSearch); + done(); }); } else { const port = (process.env.VITE_PORT ?? '5173'); @@ -292,11 +305,16 @@ export class ClientServerService { decorateReply: false, }); - fastify.register(fastifyStatic, { - root: tarball, - prefix: '/tarball/', - immutable: true, - decorateReply: false, + fastify.register((fastify, options, done) => { + fastify.register(fastifyStatic, { + root: tarball, + prefix: '/tarball/', + maxAge: ms('30 days'), + immutable: true, + decorateReply: false, + }); + fastify.addHook('onRequest', handleRedirectToOmitSearch); + done(); }); fastify.get('/favicon.ico', async (request, reply) => {