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.

Pre-compile build steps

plain assets build runs three things in order before compiling assets:

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

    [tool.plain.assets.build.run]
    openapi = {cmd = "plain api generate-openapi --validate > app/assets/openapi.json"}
    
  2. Package-registered build hooks via the plain.assets.build 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 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.2.0.tar.gz (15.0 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.2.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plain_assets-0.2.0.tar.gz
  • Upload date:
  • Size: 15.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 ec2f952d9ff1cfc8a216021d0608278f0f616b189909a876a2f275a6fd1626b7
MD5 02234b2022e5e9fb452b9317008bf21f
BLAKE2b-256 1548a533fa1cf21b345c14164be07be220143ec1751de9aca686cc610783aa6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plain_assets-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0690f016a1939dc474d9b78e318cfb27d9f64023e8083e9498749567708b2bda
MD5 e09d38ef54302252e41e4761657f6eb2
BLAKE2b-256 fb04f3058955774c75b63e1c492b06a26cb4c5f266748614ff7d127337a6d098

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