From 7f79de6412f88c9aaac09c8cf5e28ae9dd27245f Mon Sep 17 00:00:00 2001 From: Besser Sehen Landshut Date: Tue, 18 Aug 2026 09:42:24 +0200 Subject: [PATCH] fix(controller): cap answers rendered into the server-side page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The question template fetches 999 answers and then the comments for all of them, regardless of how many the page will show. The value is a literal in the controller, so there is no way to lower it: internal/controller/template_controller.go answerReq := &schema.AnswerListReq{ QuestionID: id, Page: 1, PageSize: 999, } Measured on a question with 999 answers, against 0.2s for an ordinary question: HTML ready on the server 2.6s first answer visible in browser 23.5s Most of that time is spent parsing 878 KB of HTML and hydrating a thousand posts. This replaces the literal with a named constant of 100 and explains what the number is for. It is a mitigation, not a fix: the template renders whatever it is given and has no pagination, so any value here is a trade-off between page weight and how much of a long question a crawler gets to see. The real fix is to paginate the template page the way the question list already does — bind the page from the query and render ui/template/page.html. Happy to prepare that as a follow-up if maintainers prefer it. Co-Authored-By: Claude Opus 5 (1M context) --- internal/controller/template_controller.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go index 31cc5152a..056fe5bac 100644 --- a/internal/controller/template_controller.go +++ b/internal/controller/template_controller.go @@ -52,6 +52,17 @@ import ( "github.com/segmentfault/pacman/log" ) +// maxTemplateAnswerPageSize caps how many answers are rendered into the +// server-side page. The template shows every answer it is given and has no +// pagination, so this value is also the number of answers a crawler sees. +// +// It exists because the previous value of 999 made long questions unusable: +// the server fetches the answers, then the comments for every one of them, +// and the browser has to parse and hydrate the result. On a question with +// 999 answers that took 2.6s on the server and 23.5s until the first answer +// was visible, against 0.2s for an ordinary question. +const maxTemplateAnswerPageSize = 100 + var SiteUrl = "" type TemplateController struct { @@ -345,7 +356,7 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) { QuestionID: id, Order: "", Page: 1, - PageSize: 999, + PageSize: maxTemplateAnswerPageSize, UserID: "", } answers, answerCount, err := tc.templateRenderController.AnswerList(ctx, answerReq)