Skip to main content

Serve static assets (CSS, JS, images, etc.) directly or from a CDN.

Project description

plain.assets

Serve static assets (CSS, JS, images, etc.) directly or from a CDN.

Overview

To serve assets, put them in app/assets or app/{package}/assets.

Then include the AssetsRouter in your own router, typically under the assets/ path.

# app/urls.py
from plain.assets.urls import AssetsRouter
from plain.urls import include, Router


class AppRouter(Router):
    namespace = ""
    urls = [
        include("assets/", AssetsRouter),
        # your other routes here...
    ]

Now in your template you can use the asset() function to get the URL, which will output the fully compiled and fingerprinted URL.

<link rel="stylesheet" href="{{ asset('css/style.css') }}">

Local development

When you're working with settings.DEBUG = True, the assets will be served directly from their original location. You don't need to run plain assets build or configure anything else.

Production deployment

In production, one of your deployment steps should be to compile the assets.

plain assets build

By default, this generates "fingerprinted" and compressed versions of the assets, which are then served by your app. This means that a file like main.css will result in two new files, like main.d0db67b.css and main.d0db67b.css.gz.

The purpose of fingerprinting the assets is to allow the browser to cache them indefinitely. When the content of the file changes, the fingerprint will change, and the browser will use the newer file. This cuts down on the number of requests that your app has to handle related to assets.

Using AssetView directly

In some situations you may want to use the AssetView at a custom URL, for example to serve a favicon.ico. You can do this quickly by using the AssetView.as_view() class method.

from plain.assets.views import AssetView
from plain.urls import path, Router


class AppRouter(Router):
    namespace = ""
    urls = [
        path("favicon.ico", AssetView.as_view(asset_path="favicon.ico")),
    ]

FAQs

How do you reference assets in Python code?

There is a get_asset_url function that you can use to get the URL of an asset in Python code. This is useful if you need to reference an asset in a non-template context, such as in a redirect or an API response.

from plain.assets.urls import get_asset_url

url = get_asset_url("css/style.css")

What if I need the files in a different location?

The generated/copied files are stored in {repo}/.plain/assets/compiled. If you need them to be somewhere else, try simply moving them after compilation.

plain assets build
mv .plain/assets/compiled /path/to/your/static

How do I upload the assets to a CDN?

The steps for this will vary, but the general idea is to compile them, and then upload the compiled assets from their compiled location.

# Compile the assets
plain assets build

# List the newly compiled files
ls .plain/assets/compiled

# Upload the files to your CDN
./example-upload-to-cdn-script

Use the ASSETS_CDN_URL setting to tell the {{ asset() }} template function where to point.

# app/settings.py
ASSETS_CDN_URL = "https://cdn.example.com/"

When ASSETS_CDN_URL is set, the {{ asset() }} function returns full CDN URLs directly (e.g., https://cdn.example.com/css/style.d0db67b.css).

If you also have AssetsRouter included in your URLs, requests to local asset paths will redirect to the CDN:

  • Original paths (e.g., /assets/css/style.css) use a 302 temporary redirect to the fingerprinted CDN URL. The mapping can change on rebuild.
  • Terminal paths (fingerprinted or non-fingerprinted final paths) use a 301 permanent redirect. The path itself is stable.

Only assets in the manifest are redirected. Other assets (like page assets from plain-pages) are served directly by your app.

How do I control asset access logging?

By default, 304 Not Modified responses for assets are excluded from the server access log to reduce noise. Set ASSETS_LOG_304 = True in your settings to include them.

Why aren't the originals copied to the compiled directory?

The default behavior is to fingerprint assets, which is an exact copy of the original file but with a different filename. The originals aren't copied over because you should generally always use this fingerprinted path (that automatically uses longer-lived caching).

If you need the originals for any reason, you can use plain assets build --keep-original, though this will typically be combined with --no-fingerprint otherwise the fingerprinted files will still get priority in {{ asset() }} template calls.

Note that by default, the ASSETS_REDIRECT_ORIGINAL setting is True, which will redirect requests for the original file to the fingerprinted file.

Installation

Install the plain.assets package from PyPI:

uv add plain.assets

Add to your INSTALLED_PACKAGES:

# app/settings.py
INSTALLED_PACKAGES = [
    ...
    "plain.assets",
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plain_assets-0.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_assets-0.1.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file plain_assets-0.1.0.tar.gz.

File metadata

  • Download URL: plain_assets-0.1.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for plain_assets-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b19710fa522f7f27989f80a42f340fc84b6eac47e081faad28895492156013e
MD5 99e12793eeeb27aaf27e75139a1ffae9
BLAKE2b-256 5e41c00975320721c536ac4bee71c842690ef3705159a5f97a43d7bf8441d19d

See more details on using hashes here.

File details

Details for the file plain_assets-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: plain_assets-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for plain_assets-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c7fbacaec816fabd3d1331a1cf654b41aee71e7dce0bd32cefe6e54ab209315
MD5 f0eaa07ad08ed774254a0329eeecd455
BLAKE2b-256 f0db55b26756e6e05da16147f2650d7acb3dda448f6fcb43c6bf4f0dcbf31f9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page