SvelteKit Application running inside Docker proxied by NGINX hangs on event.fetch to API running on same domain

So I have this SvelteKit application running on a Docker container on my server using node-adapter. I also have a .NET backend running on Docker in the same server. Both applications are proxied by NGINX (config below). I can confirm that the .NET API is working as expected, but the SvelteKit frontend hangs on a fetch request to my API. Code for the request looks like this:

event.fetch("https://example.com/api/users/me")

And it just hangs, not even an error is throwing, which I know because I have the call surrounded by a try-catch and I am logging any errors. And yes, logging is enabled with minimum level of INFO, so error logs should be visible (using pino logger).

Also, hitting the Docker container directly also causes the application to hang.

Here is my NGINX config:

server {
        listen 80;                                            
        server_name example.com;
        return 301 https://$host:443$request_uri;
}
                                                       
server {
        listen 443 ssl;
        server_name example.com;

        ssl_certificate /etc/nginx/ssl/fullchain.pem;
        ssl_certificate_key /etc/nginx/ssl/privkey.pem;
                                                                
        location / {
                proxy_pass http://localhost:3000;
                proxy_set_header Host $host;                           
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }

        location /api/ {
                proxy_pass http://localhost:5000/api/;            
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;                                                             }

        location /signin-cas/ {
                proxy_pass http://localhost:5000/signin-cas/;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }
}

And my Dockerfile

FROM node:20 as build

WORKDIR /app

COPY package.json ./
COPY package-lock.json ./

ARG PUBLIC_API_BASE_URL
ARG PUBLIC_APP_BASE_URL
ARG PUBLIC_CAS_COOKIE_NAME

RUN npm install

COPY . ./
RUN NODE_OPTIONS="--max_old_space_size=1512" npm run build

FROM node:20

COPY --from=build /app/build ./build
COPY package.json ./
COPY package-lock.json ./

RUN npm ci --omit dev

CMD ["node", "build"]

I’ve also set the environment variables as recommended here: https://kit.svelte.dev/docs/adapter-node.

At the final line of Dockerfile you call node build. This command does not start SvelteKit application. It just builds an application and creates .svelte-kit and build folders inside your project (if you did not changed it).
If you use node-adapter you should start your application at the end of your Dockerfile.
It must be something like this:

CMD ["node", "build/index.js"]

It hangs without errors because you told docker just to build an app and that’s it. Command CMD must start an app.

Moreover, you should expose a port for your application and write this port in nginx.conf. I guess you plan to use nginx as reverse-proxy.
Mean you should create one more nginx.conf besides main which is lying on your server and add additional info about your application. E.g. you host your SvelteKit application on 5000 port. You should expose this port in Dockerfile like this:

EXPOSE 5000

and add one more nginx.conf like this

worker_processes auto;

events {
    worker_connections 8000;
    multi_accept on;
}

http {
    include   /etc/nginx/mime.types;

    server {
        listen 5000;
        listen [::]:5000;

        gzip on;
        gzip_types text/plain text/html application/javascript application/x-javascript text/javascript text/css;

        root /build;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location ~* .(?:css|js)$ {
            access_log        off;
            log_not_found     off;

            add_header        Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
        }

        location ~* .(?:jpg|jpeg|gif|png|ico|xml)$ {
            access_log        off;
            log_not_found     off;

            expires           48h;

            add_header        Cache-Control "public";
        }
    }
}

Solution is in this Reddit thread: https://www.reddit.com/r/sveltejs/comments/1fdpxea/sveltekit_application_running_inside_docker/.

Basically:

  1. Svelte has this thing were requests to the same host turn into function calls, which was causing the hanging
  2. Changed the Svelte app to directly hit the backend API running on another host inside docker (only for the server sent requests)
  3. Configured handleFetch to send cookies as per https://kit.svelte.dev/docs/hooks#server-hooks-handlefetch

Many thanks to @khromov!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật