Commit 8ec29b41 authored by Patrick Bajao's avatar Patrick Bajao

Cache markdown when viewing a blob

In order to improve performance of
`Projects::BlobController#show.json` when viewing a markdown blob,
we need to utilize the cache so succeeding requests to the same
blob won't have to re-parse markdown.

Based on tests done locally, this improves the response time of
the endpoint when viewing a large markdown file from ~11s to ~4s.
parent 67ac24fb
...@@ -9,5 +9,15 @@ module BlobViewer ...@@ -9,5 +9,15 @@ module BlobViewer
self.extensions = Gitlab::MarkupHelper::EXTENSIONS self.extensions = Gitlab::MarkupHelper::EXTENSIONS
self.file_types = %i(readme) self.file_types = %i(readme)
self.binary = false self.binary = false
def banzai_render_context
{}.tap do |h|
h[:rendered] = blob.rendered_markup if blob.respond_to?(:rendered_markup)
if Feature.enabled?(:cached_markdown_blob, blob.project)
h[:cache_key] = ['blob', blob.id, 'commit', blob.commit_id]
end
end
end
end end
end end
- blob = viewer.blob - blob = viewer.blob
- context = blob.respond_to?(:rendered_markup) ? { rendered: blob.rendered_markup } : {}
.file-content.md .file-content.md
= markup(blob.name, blob.data, context) = markup(blob.name, blob.data, viewer.banzai_render_context)
---
name: cached_markdown_blob
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/44300
rollout_issue_url:
type: development
group: group::source code
default_enabled: false
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe BlobViewer::Markup do
include FakeBlobHelpers
let(:project) { create(:project, :repository) }
let(:blob) { fake_blob(path: 'CHANGELOG.md') }
subject { described_class.new(blob) }
describe '#banzai_render_context' do
it 'returns context needed for banzai rendering' do
expect(subject.banzai_render_context.keys).to eq([:cache_key])
end
context 'when blob does respond to rendered_markup' do
before do
allow(blob).to receive(:rendered_markup).and_return("some rendered markup")
end
it 'does sets rendered key' do
expect(subject.banzai_render_context.keys).to include(:rendered)
end
end
context 'when cached_markdown_blob feature flag is disabled' do
before do
stub_feature_flags(cached_markdown_blob: false)
end
it 'does not set cache_key key' do
expect(subject.banzai_render_context.keys).not_to include(:cache_key)
end
end
end
end
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