Skip to content

Allow disabling CORS fetch mode in the service worker#2932

Open
puffo wants to merge 1 commit into
basecamp:mainfrom
puffo:service-worker-cors-toggle
Open

Allow disabling CORS fetch mode in the service worker#2932
puffo wants to merge 1 commit into
basecamp:mainfrom
puffo:service-worker-cors-toggle

Conversation

@puffo

@puffo puffo commented Jun 10, 2026

Copy link
Copy Markdown

Fixes the issue described in #2780, as suggested by @flavorjones.

Problem

The service worker fetches Active Storage URLs with mode: "cors" so it can inspect response sizes for maxEntrySize. For this to work correctly, it relies on a load balancer injecting CORS headers on the 302 redirect to the presigned storage URL.

Self-hosted instances with cross-site S3-compatible storage (no load-balancer) aren't able to inject CORS headers, so the redirect breaks inline image loading.

Commit 45d1657 fixed this for development with MinIO by making it same-site, but production self-hosted instances with storage on a different site are still affected.

Fix

CORS fetch mode remains the default (no behavior change for SaaS, local-disk self-hosted, or dev/MinIO setups, and maxEntrySize enforcement is preserved). Affected self-hosters can set SERVICE_WORKER_CORS_ENABLED=false to omit fetchOptions: { mode: "cors" } from the storage rule.

Self-hosted instances using cross-site S3-compatible storage have no
load balancer to add CORS headers to Active Storage redirects, so the
service worker's `mode: "cors"` fetch fails and inline images break.
Setting SERVICE_WORKER_CORS_ENABLED=false omits the CORS fetch option.
Copilot AI review requested due to automatic review settings June 10, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a configurable toggle for service worker fetch CORS behavior to address cross-origin storage providers, along with documentation and tests.

Changes:

  • Make fetchOptions: { mode: "cors" } in the service worker conditional on SERVICE_WORKER_CORS_ENABLED.
  • Add integration tests covering the rendered service worker template with the toggle on/off.
  • Document the new environment variable in Docker deployment instructions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
test/controllers/pwa_controller_test.rb Adds tests to verify rendered service worker output with/without CORS fetchOptions.
docs/docker-deployment.md Documents when/how to disable the service worker CORS behavior via env var.
app/views/pwa/service_worker.js.erb Conditionally emits fetchOptions: { mode: "cors" } based on an env var.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +45 to +46
maxEntries: 500<% unless ENV["SERVICE_WORKER_CORS_ENABLED"] == "false" %>,
fetchOptions: { mode: "cors" }<% end %>
Comment on lines +25 to +30
def switch_env(key, value)
old, ENV[key] = ENV[key], value
yield
ensure
ENV[key] = old
end
Comment on lines +45 to +46
maxEntries: 500<% unless ENV["SERVICE_WORKER_CORS_ENABLED"] == "false" %>,
fetchOptions: { mode: "cors" }<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants