Skip to content

Commit

Permalink
キャッシュを考慮して別URLに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Aug 28, 2024
1 parent 8dd4a92 commit 509682a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 5 deletions.
16 changes: 16 additions & 0 deletions app/controllers/user_custom_css_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class UserCustomCssController < ActionController::Base # rubocop:disable Rails/ApplicationController
before_action :authenticate_user!

def show
render content_type: 'text/css'
end

private

def user_custom_css
current_user.custom_css_text
end
helper_method :user_custom_css
end
6 changes: 6 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ def mascot_url
full_asset_url(instance_presenter.mascot&.file&.url || frontend_asset_path('images/elephant_ui_plane.svg'))
end

def user_custom_css?
return false if current_account&.user.nil?

current_account.user.setting_use_custom_css && current_account.user.custom_css_text.present?
end

def user_custom_css_version
return '0' if current_account&.user&.custom_css.nil?

Expand Down
4 changes: 0 additions & 4 deletions app/views/custom_css/show.css.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@
}

<%- end %>
<%- if user_custom_css? %>
<%= raw user_custom_css %>

<%- end %>
5 changes: 4 additions & 1 deletion app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
= csrf_meta_tags unless skip_csrf_meta_tags?
%meta{ name: 'style-nonce', content: request.content_security_policy_nonce }

= stylesheet_link_tag custom_css_path({ version: user_custom_css_version }), skip_pipeline: true, host: root_url, media: 'all'
= stylesheet_link_tag custom_css_path, skip_pipeline: true, host: root_url, media: 'all'

- if user_custom_css?
= stylesheet_link_tag user_custom_css_path({ version: user_custom_css_version }), skip_pipeline: true, host: root_url, media: 'all'

= yield :header_tags

Expand Down
1 change: 1 addition & 0 deletions app/views/user_custom_css/show.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= raw user_custom_css %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def redirect_with_vary(path)
get 'manifest', to: 'manifests#show', defaults: { format: 'json' }
get 'intent', to: 'intents#show'
get 'custom.css', to: 'custom_css#show', as: :custom_css
get 'user_custom.css', to: 'user_custom_css#show', as: :user_custom_css

get 'remote_interaction_helper', to: 'remote_interaction_helper#index'

Expand Down
48 changes: 48 additions & 0 deletions spec/requests/user_custom_css_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'User custom CSS' do
let(:user) { Fabricate(:user) }
let(:custom_css) { '* { display: none !important; }' }

describe 'GET /user_custom.css' do
context 'without sign in' do
it 'returns 422' do
get '/user_custom.css'

expect(response).to have_http_status(401)
end
end

context 'with sign in but custom css is not enabled' do
before do
user.update!(custom_css_text: custom_css)
sign_in user
end

it 'returns custom css' do
get '/user_custom.css'

expect(response).to have_http_status(200)
expect(response.content_type).to include 'text/css'
expect(response.body.strip).to eq custom_css
end
end

context 'with sign in and custom css is enabled' do
before do
user.update!(custom_css_text: custom_css, settings: { 'web.use_custom_css': true })
sign_in user
end

it 'returns custom css' do
get '/user_custom.css'

expect(response).to have_http_status(200)
expect(response.content_type).to include 'text/css'
expect(response.body.strip).to eq custom_css
end
end
end
end

0 comments on commit 509682a

Please sign in to comment.