Allow disabling CORS fetch mode in the service worker#2932
Open
puffo wants to merge 1 commit into
Open
Conversation
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.
Contributor
There was a problem hiding this comment.
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 onSERVICE_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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.