I have a SvelteKit project hosted on Netlify, and the idea is that if a user accesses the route mynetlifyproject.com/route, it should render content from another domain, specifically from app.myrailsproject.com/route.
To make this work, in my Netlify project I created a _redirects file and added the following line:
/route/* https://app.myrailsproject.com/route/:splat 200
After deploying and testing, the content loads as expected - the HTML and CSS are delivered correctly. However, when inspecting the page, the console shows several errors indicating a failure to fetch the page’s scripts, as shown below:
Access to script at 'https://app.myrailsproject.com/vite/assets/script.af31239f.js' from origin 'https://www.mynetlifyproject.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
This suggests that the CORS settings on my Rails app are not allowing the files to be delivered to the specified origin. So I went ahead and added the following CORS configuration:
allow do
origins 'https://www.mynetlifyproject.com'
resource '/vite/assets/*',
headers: :any,
methods: [:get, :options],
expose: ['Access-Control-Allow-Origin']
end
allow do
origins '*'
resource '/public/vite/assets/*', headers: :any, methods: [:post, :get],
expose: ['Access-Control-Allow-Origin']
end
Above, I tried two configurations—one specifying the domain directly, and another allowing all origins—but the problem persisted in both cases.
So, I began to suspect that the issue might not be with Rails' CORS configuration, but with NGINX.
I then tried adding the following configuration to NGINX:
cat myapp_production
upstream puma_myapp_production {
server unix:/var/www/myapp/shared/tmp/sockets/myapp-puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost myapp.local;
root /var/www/myapp/current/public;
try_files $uri/index.html $uri @puma_myapp_production;
client_max_body_size 4G;
keepalive_timeout 10;
more_set_headers 'X-Debug-CORS: $http_origin';
error_page 500 502 504 /500.html;
error_page 503 @503;
location @puma_myapp_production {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_set_header X-Forwarded-Proto http;
proxy_pass http://puma_myapp_production;
# limit_req zone=one;
access_log /var/www/myapp/shared/log/nginx.access.log;
error_log /var/www/myapp/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location ~ ^/vite/assets/ {
add_header 'Access-Control-Allow-Origin' '*' always;
}
location = /vite/assets/indica.f8da5aba.js {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
}
location = /vite/assets/chart.9bbb5b62.js {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
}
location ^~ /packs/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
As you can see, there are three attempts to allow CORS: two targeting specific files and one to open access for the whole directory, but the issue still persists.