I deployed a small app (Django, React). The app deploys fine, but the js script implementing the React portion does not show. On the Client side, I’m getting the following error:
With that, I re-deployed. The app seemingly deployed correctly, but I get the same MIME errors as above. Note: All of this works correctly when I run the app locally on my machine.
Any help would be greatly appreciated. I’m somewhat of a newbie.
Since nobody else has responded, I’ll take a guess
I’d assume the reason you are getting text/html as the MIME type is because the request for that static file is being served from the same domain as your app, and so is being treated like a dynamic request. As you mention with the toml, you could get Fly to handle that for you, as the request has to pass through its proxy to get to your app. It can adjust the headers.
However it may be easier to handle it in your app. Then it would be consistent with local (where you don’t have the Fly proxy). You’d likely want something like Using WhiteNoise with Django - WhiteNoise 6.3.0 documentation to handle static files. Nodejs has something similar - you need to tell it you want it to handle static files (paths, headers etc), else it doesn’t know to do that.
If you already have whitenoise (or equivalent) it would likely be a case of adjusting the path to account for where the files are on Fly vs local. Maybe via an environment variable.
Thank you Greg, I appreciate you taking the time. I do have WhiteNoise. I will look at the Documentation. I copied my app from local to Fly replicating the file structure. In the Dockerfile:
COPY . .
I’m not sure I follow what you mean by adjusting the path to account for where files are. Should the relative paths be the same?
Yep, that Dockerfile line will indeed copy the files across (you can check if you run fly ssh console you should be able to SSH-in to your app’s vm, and look around its file system to make sure).
As regards paths, yes, relative ones should be the same. I just wondered if there were any absolute ones, which wouldn’t be the same on your local machine vs a Fly vm e.g
First, although I responded that I had Whitenoise installed, I actually only had the STATICFILES_STORAGE variable defined. I forgot to include WhiteNoise in the Middleware. I included the Middleware and I set the WHITENOISE_MIMETYPES. These solved the problem.
For completeness, the settings.py contains the following:
WHITENOISE_MIMETYPES = {'.js': 'text/javascript'}
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
...,
]
# Static files (CSS, JavaScript, Images)
# The below settings are based on info here: https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR/"staticfiles"
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
Hope this helps someone else experiencing similar issue.