DEV Community

Cover image for Bring in the WhiteNoise, Bring in Da Funk - Building SaaS #34
Matt Layman
Matt Layman

Posted on • Originally published at mattlayman.com

Bring in the WhiteNoise, Bring in Da Funk - Building SaaS #34

In this episode, we added WhiteNoise to the app as a tool for handling static assets. This lets us move away from depending on Nginx for the task and gives shiny new features like Brotli support.

We installed WhiteNoise into the requirements.in file and used pip-tools to generate a new requirements.txt.

whitenoise[brotli]==4.1.4

Once WhiteNoise was installed, it needed two primary settings changes.

  1. Add a new middleware.
  2. Change the STATICFILES_STORAGE.
MIDDLEWARE = [
    ...
    "whitenoise.middleware.WhiteNoiseMiddleware",
    ...
]

STATICFILES_STORAGE = \
    "whitenoise.storage.CompressedManifestStaticFilesStorage"

This was enough to get WhiteNoise working. We checked the local development server, and the site continued to operate normally.

Unfortunately, the configuration was not enough to make static assets work with the Shiv app. Since STATIC_ROOT used a relative value of static, Shiv couldn't find the static files. This relative pathing was the same problem I faced when getting the templates directory working with the Shiv app. We used the same solution and switched to:

import conductor

conductor_dir = os.path.dirname(conductor.__file__)
STATIC_ROOT = os.path.join(conductor_dir, "static")

Finally, we needed to make the conductor package hold the collected static files. We could do this by tweaking MANIFEST.in with the following addition:

recursive-include conductor/static *

On the next stream, we'll make some CI changes and update deployment to discontinue use of Nginx for serving static files.

This article first appeared on mattlayman.com.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay