Serving static files (css/js) from Django
chrismv48
HOBBYOP

a year ago

I've been working on my first railway deploy but cannot figure out this static file serving issue. The error I get is:

Refused to apply style from 'https://stagesidekickv2-production.up.railway.app/static/css/tailwind.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

According to the web, this really just means it couldn't find the file. Visiting the file URL directly confirms this with a 404.

My project has a single static directory at the root level. I'm also using whitenoise and have followed their django instructions here: https://whitenoise.readthedocs.io/en/stable/django.html#.

Here is how I have my settings configured:

# Static files configuration
STATIC_ROOT = BASE_DIR / "staticfiles"
STATIC_URL = "static/"
STATICFILES_FINDER = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
STATICFILES_DIRS = [
    BASE_DIR / "static",  # Shared static files
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

My project id is 51d489e5-fe6e-4c2b-bef5-68d1035698d4 in case it helps.

Solved

7 Replies

chrismv48
HOBBYOP

a year ago

anyone? 😦


a year ago

are you collecting the static files?


a year ago

example -


chrismv48
HOBBYOP

a year ago

Yes. Here's my railways.json and build.sh

railways.json

{
  "$schema": "https://railway.com/railway.schema.json",
  "build": {
    "builder": "NIXPACKS",
    "nixpacksPlan": {
      "providers": ["python", "node"]
    }
  },
  "deploy": {
    "startCommand": "gunicorn stage_sidekick.wsgi:application",
    "preDeployCommand": "chmod +x build.sh && ./build.sh",
    "healthcheckPath": "/",
    "healthcheckTimeout": 100,
    "restartPolicyType": "ON_FAILURE"
  }
}

build.sh

#!/usr/bin/env bash
# exit on error
set -o errexit

# Install dependencies
pip install pipenv
pipenv install --system --deploy

# Collect static files
python manage.py collectstatic --no-input

# Run migrations
python manage.py migrate

chrismv48
HOBBYOP

a year ago

Just checking in to let future folks know that the issue was trying to include the collectstatic command as part of the preDeployCommand. Apparently commands executed via preDeployCommand are run in a different container than the app and therefore any generated files are ignored. So running collectstatic had no effect. To fix, I moved any commands that generate files to the startCommand instead.


a year ago

glad to hear you solved this, marking as closed


a year ago

!s


Status changed to Solved adam 12 months ago


Loading...