Commit f9f91472 authored by Sean McGivern's avatar Sean McGivern

Fix page_description helper performance

This helper is used for extracting part of the issue / MR / whatever
description for use in the description meta tag:

1. To do that, we look at the source of the Markdown description.
2. We then strip out all HTML tags.
3. And then take the first 30 words.

Doing that can be really slow - especially as Markdown is supposed to be
treated as plain text. There are many better ways to do this, but the
immediate performance fix is to swap steps 2 and 3. This does mean that
the description may be less than 30 words (or even empty), but it is
much faster when the description is very long.
parent bc4a18ec
# coding: utf-8
# frozen_string_literal: true
module PageLayoutHelper
......@@ -36,7 +37,7 @@ module PageLayoutHelper
if description.present?
@page_description = description.squish
elsif @page_description.present?
sanitize(@page_description, tags: []).truncate_words(30)
sanitize(@page_description.truncate_words(30), tags: [])
end
end
......
---
title: Fix performance issue with large Markdown content in issue or merge request
description
merge_request: 28597
author:
type: performance
......@@ -38,6 +38,14 @@ describe PageLayoutHelper do
expect(helper.page_description).to eq 'Bold Header'
end
it 'truncates before sanitizing' do
helper.page_description('<b>Bold</b> <img> <img> <img> <h1>Header</h1> ' * 10)
# 12 words because <img> was counted as a word
expect(helper.page_description)
.to eq('Bold Header Bold Header Bold Header Bold Header Bold Header Bold Header...')
end
end
describe 'page_image' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment