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 compile or configure anything else.

Production deployment

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

plain assets compile

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.

Pre-compile hooks

plain assets compile runs three things in order:

  1. User-defined shell commands from [tool.plain.assets.run] in your pyproject.toml. Useful for codegen that produces files into app/assets/:

    [tool.plain.assets.run]
    openapi = {cmd = "plain api generate-openapi --validate > app/assets/openapi.json"}
    
  2. Package-registered hooks via the plain.assets.compile entry-point group. Packages like plain.tailwind and plain.esbuild ship one of these to compile CSS / JS before the asset fingerprinter runs.

  3. The asset compile itself (fingerprinting, compression).

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 compile
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 compile

# 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 compile --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.3.0.tar.gz (15.2 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.3.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plain_assets-0.3.0.tar.gz
  • Upload date:
  • Size: 15.2 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.3.0.tar.gz
Algorithm Hash digest
SHA256 003f6022a6307bf3f8689fb3a7156b8670694a2031d29e6e2a950082471fc3ed
MD5 3a257c0bb2227e688057099af54388f9
BLAKE2b-256 333ae43e4b777f39af947401e755ec23dc0aa72225817640ba9d8787aefb9e64

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plain_assets-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 18.5 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d0fccc58f6d278a42d85449deb8703028ae96a26754e469ef1a53b4580ed543
MD5 0a6af6f2b23e499afd4aa6c310b7dcf4
BLAKE2b-256 90ab8ba373431b839fb8addef959a560618ce991bd14d35e0bc43af50fdd3b6f

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