bounswe / bounswe2024group12

This repository contains Group 12's work for CMPE 352 - Spring 2024.
6 stars 1 forks source link

Fix Deployment Static Files Conflict #320

Open sonerkuyar opened 3 days ago

sonerkuyar commented 3 days ago

Description

When accessing Swagger UI at /api/swagger/, the required static files (e.g., swagger-ui-bundle.js) were not loading correctly. Instead, the React app's static files were being served, causing the following error:

Uncaught SyntaxError: Unexpected token '<'

This occurred because both the frontend and backend static files were being served from the same path (/static/), leading to a conflict.


Acceptance Criteria

Reviewer:

This issue will be reviewed by: @ounuvar @ozankaymak

sonerkuyar commented 3 days ago

Problem can be seen due to different reasons but no solutions works for me at different discussions Swagger issue

Steps I Took

  1. Checked network requests in the browser and saw that requests to /static/... were serving the React app's files instead of the Swagger files.
  2. Updated the Nginx configuration to explicitly route backend static files under /api/static/ and frontend files under /static/. However, the issue persisted because the backend was still generating static file URLs starting with /static/, which conflicted with the React app.

What Didn’t Work


Solution

Steps That Worked

  1. Updated the STATIC_URL setting in settings.py to /api/static/:

    STATIC_URL = '/api/static/'

    This ensured that all backend static files were served under /api/static/.

  2. Ran the collectstatic command to collect all static files into the correct directory:

    python manage.py collectstatic
  3. Adjusted the Nginx configuration to serve backend static files from /api/static/:

    location /api/static/ {
       alias /root/bounswe2024group12/app/backend/staticfiles/;  # Adjusted to the actual path
       expires 1y;  # Cache files for better performance
       access_log off;
    }
  4. Restarted Nginx to apply the changes:

    sudo systemctl reload nginx

Result

I've made appropriate changes at deployment machine and fixed the error.