Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Simplify theme screenshot, rely on a 3rd party (#12569)
Damian Legawiec27c69d81fda3
diff --git a/admin/app/assets/stylesheets/spree/admin/shared/_base.scss b/admin/app/assets/stylesheets/spree/admin/shared/_base.scssindex a2dfd3c630..3019ba254d 100644--- a/admin/app/assets/stylesheets/spree/admin/shared/_base.scss+++ b/admin/app/assets/stylesheets/spree/admin/shared/_base.scss@@ -648,7 +648,7 @@ turbo-frame[busy] #turbo-spinner {}.overlay-background {- background-color: rgba(0, 0, 0, 0.4);+ background-color: rgba($white, 0.3);transition: background-color 0.3s ease-in-out;}diff --git a/admin/app/controllers/spree/admin/themes_controller.rb b/admin/app/controllers/spree/admin/themes_controller.rbindex 5caf87d4b3..e952b4f3bf 100644--- a/admin/app/controllers/spree/admin/themes_controller.rb+++ b/admin/app/controllers/spree/admin/themes_controller.rb@@ -68,7 +68,7 @@ module Spreeenddef collection- super.without_previews.order(default: :desc).includes(screenshot_attachment: :blob)+ super.without_previews.order(default: :desc)endendenddiff --git a/admin/app/views/spree/admin/dashboard/_store_preview.html.erb b/admin/app/views/spree/admin/dashboard/_store_preview.html.erbindex fe38848d54..3b6254ee62 100644--- a/admin/app/views/spree/admin/dashboard/_store_preview.html.erb+++ b/admin/app/views/spree/admin/dashboard/_store_preview.html.erb@@ -1,31 +1,22 @@<% if current_store.default_theme %>- <div class="card mb-4">- <%= link_to spree.edit_admin_theme_path(current_store.default_theme), class: 'd-block card-img-top border-bottom overflow-auto bg-light position-relative store-preview-link', style: "height: 220px" do %>+ <div class="card mb-4 bg-transparent">+ <%= link_to spree.edit_admin_theme_path(current_store.default_theme), class: 'd-block border rounded mx-2 mt-2 mb-0 overflow-auto bg-light position-relative store-preview-link' do %><div class="position-absolute w-100 h-100 align-items-center justify-content-center overlay-background store-preview-actions">- <button class="btn btn-outline-primary">+ <button class="btn btn-primary"><%= icon('tools') %><%= Spree.t('admin.edit_theme') %></button></div>- <% if current_store.default_theme.screenshot.attached? && current_store.default_theme.screenshot.variable? %>- <%= image_tag(- main_app.cdn_image_url(- current_store.default_theme.screenshot.variant(- spree_image_variant_options(resize_to_fill: [400, 250])- )- ),- class: 'w-100 h-100 bg-light',- loading: :lazy,- style: 'height: 220px'- ) %>- <% else %>- <div class="w-100 h-100 d-flex flex-column align-items-center justify-content-center bg-light text-muted">- <%= icon('camera-off', class: 'text-muted', height: 24) %>- </div>- <% end %>++ <%= image_tag(+ current_store.default_theme.screenshot_url,+ class: 'w-100 bg-light rounded',+ loading: :lazy,+ height: 172+ ) %><% end %>- <div class="card-body pt-2">+ <div class="card-body px-2 pt-0 pb-2"><div class="flex my-2 p-1 w-100 rounded border" data-controller="clipboard" data-clipboard-success-content-value="Copied!"><% if current_store.default_custom_domain&.active? %><%= text_field_tag :custom_domain, current_store.default_custom_domain.url, readonly: true, class: 'form-control-plaintext py-0', data: { clipboard_target: 'source' } %>@@ -51,7 +42,7 @@</div></div><% unless current_store.default_custom_domain&.active? %>- <div class="card-footer border-top">+ <div class="card-footer border-top p-2"><%= link_to_with_icon 'world-www', 'Connect your own domain', spree.admin_custom_domains_path, class: 'btn btn-link w-100 py-2' %></div><% end %>diff --git a/admin/app/views/spree/admin/themes/_theme.html.erb b/admin/app/views/spree/admin/themes/_theme.html.erbindex dcdec980b2..d7718b9cf8 100644--- a/admin/app/views/spree/admin/themes/_theme.html.erb+++ b/admin/app/views/spree/admin/themes/_theme.html.erb@@ -1,16 +1,12 @@<tr id="<%= spree_dom_id theme %>"><td class="w-70 p-0"><%= link_to theme.ready? ? spree.edit_admin_theme_path(theme) : '#', class: 'd-flex align-items-top p-3' do %>- <% if theme.screenshot.attached? && theme.screenshot.variable? %>+ <% if theme.screenshot_url.present? %><%= image_tag(- main_app.cdn_image_url(- theme.screenshot.variant(- spree_image_variant_options(resize_to_fill: [240, 144])- )- ),- class: 'img-thumbnail',- style: 'width: 120px; height: 72px',- loading: :lazy+ theme.screenshot_url,+ class: 'img-thumbnail',+ style: 'width: 120px; height: 72px',+ loading: :lazy) %><% else %><%= render 'spree/admin/shared/no_image', width: 120, height: 72 %>diff --git a/core/app/jobs/spree/themes/screenshot_job.rb b/core/app/jobs/spree/themes/screenshot_job.rbdeleted file mode 100644index 5e16da3afe..0000000000--- a/core/app/jobs/spree/themes/screenshot_job.rb+++ /dev/null@@ -1,29 +0,0 @@-module Spree- module Themes- class ScreenshotJob < Spree::BaseJob- queue_as Spree.queues.themes-- def perform(theme_id)- theme = Spree::Theme.find(theme_id)-- browser = Ferrum::Browser.new- browser.go_to(theme.store.formatted_url)-- Tempfile.create(['screenshot', '.png']) do |tempfile|- browser.screenshot(path: tempfile.path)-- theme.skip_screenshot_update = true- theme.screenshot.attach(- io: File.open(tempfile.path),- filename: "#{theme.store.name}-#{theme.name}-#{theme.updated_at}.png",- content_type: 'image/png'- )- theme.save!- theme.skip_screenshot_update = false- end- ensure- browser&.quit- end- end- end-enddiff --git a/core/app/models/spree/theme.rb b/core/app/models/spree/theme.rbindex bdbcf6554d..efa069a901 100644--- a/core/app/models/spree/theme.rb+++ b/core/app/models/spree/theme.rb@@ -24,11 +24,6 @@ module Spreehas_many :layout_sections, class_name: 'Spree::PageSection', dependent: :destroy, as: :pageablealias sections layout_sections- #- # Attachments- #- has_one_attached :screenshot, service: Spree.public_storage_service_name-### Callbacks@@ -37,13 +32,20 @@ module Spreebefore_save :ensure_default_exists_and_is_uniqueafter_create :create_default_pages, :create_layout_sections, unless: :duplicating?before_destroy :change_name_to_archived- after_commit :take_screenshot, on: [:create, :update]## Virtual attributes#attribute :duplicating, :boolean, default: false- attribute :skip_screenshot_update, :boolean, default: false++ def screenshot_url+ @screenshot_url ||= if ENV['SCREENSHOT_API_TOKEN'].present?+ "https://shot.screenshotapi.net/v3/screenshot?token=#{ENV['SCREENSHOT_API_TOKEN']}&url=#{current_store.url_or_custom_domain}&fresh=true&output=image&file_type=png&no_cookie_banners=true&retina=true&enable_caching=true&wait_for_event=load&thumbnail_width=700"+ else+ # default screenshot+ "https://s3.eu-central-2.wasabisys.com/w.storage.screenshotapi.net/demo_spreecommerce_org_299a49137b25.png"+ end+ end## Class methods@@ -188,15 +190,5 @@ module Spreedef change_name_to_archivedupdate_columns(name: "#{name} (Archived)")end-- def take_screenshot- return # Return for now, we will enable this after we move to Docker- return if Rails.env.test? # skip in tests- return if skip_screenshot_update- # Skip if screenshot is already attached and was created less than a minute ago- return if screenshot.attached? && screenshot.blob.created_at > 1.minute.ago-- Spree::Themes::ScreenshotJob.perform_later(id)- endendenddiff --git a/core/spec/models/spree/theme_spec.rb b/core/spec/models/spree/theme_spec.rbindex b4cfe1a529..52a28f0e31 100644--- a/core/spec/models/spree/theme_spec.rb+++ b/core/spec/models/spree/theme_spec.rb@@ -36,31 +36,6 @@ describe Spree::Theme, type: :model doendendend-- xdescribe 'after update' do # Enable after installing Chrome- before do- allow(Rails.env).to receive(:test?).and_return(false)- end-- it 'should queue a job to take screenshot' do- theme- expect { theme.update!(name: 'New Name') }.to have_enqueued_job(Spree::Themes::ScreenshotJob)- end-- it 'should not queue job when last screenshot was taken less than minute ago' do- theme.screenshot.attach(- io: File.open(Spree::Core::Engine.root.join('spec', 'fixtures', 'thinking-cat.jpg')),- filename: 'thinking-cat.jpg'- )-- expect { theme.update!(name: 'New Name') }.not_to have_enqueued_job(Spree::Themes::ScreenshotJob)- end-- it 'should not queue job when skip_screenshot_update is true' do- theme.skip_screenshot_update = true- expect { theme.update!(name: 'New Name') }.not_to have_enqueued_job(Spree::Themes::ScreenshotJob)- end- endenddescribe '#create_preview' do