Skip to main content

datasette-hashed-urls

PyPI Changelog Tests License

Optimize Datasette performance behind a caching proxy

When you open a database file in immutable mode using the -i option, Datasette calculates a SHA-256 hash of the contents of that file on startup.

This content hash can then optionally be used to create URLs that are guaranteed to change if the contents of the file changes in the future.

The result is pages that can be cached indefinitely by both browsers and caching proxies - providing a significant performance boost.

Demo

A demo of this plugin is running at https://datasette-hashed-urls.vercel.app/

Installation

Install this plugin in the same environment as Datasette.

$ datasette install datasette-hashed-urls

Usage

Once installed, this plugin will act on any immutable database files that are loaded into Datasette:

datasette -i fixtures.db

The database will automatically be renamed to incorporate a hash of the contents of the SQLite file - so the above database would be served as:

http://127.0.0.1:8001/fixtures-aa7318b

Every page that accesss that database, including JSON endpoints, will be served with the following far-future cache expiry header:

cache-control: max-age=31536000, public

Here max-age=31536000 is the number of seconds in a year.

A caching proxy such as Cloudflare can then be used to cache and accelerate content served by Datasette.

When the database file is updated and the server is restarted, the hash will change and content will be served from a new URL. Any hits to the previous hashed URLs will be automatically redirected.

If you run Datasette using the --crossdb option to enable cross-database queries the _memory database will also have a hash added to its URL - in this case, the hash will be a combination of the hashes of the other attached databases.

Configuration

You can use the max_age plugin configuration setting to change the cache duration specified in the cache-control HTTP header.

To set the cache expiry time to one hour you would add this to your Datasette metadata.json configuration file:

{
    "plugins": {
        "datasette-hashed-urls": {
            "max_age": 3600
        }
    }
}

History

This functionality used to ship as part of Datasette itself, as a feature called Hashed URL mode.

That feature has been deprecated and will be removed in Datasette 1.0. This plugin should be used as an alternative.

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd datasette-hashed-urls
python3 -mvenv venv
source venv/bin/activate

Or if you are using pipenv:

pipenv shell

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Metadata

Release files for datasette-hashed-urls 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for datasette-hashed-urls 0.4
File Size Uploaded Provenance Actions
datasette-hashed-urls-0.4.tar.gz 8.3 kB Without provenance Details for datasette-hashed-urls-0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for datasette-hashed-urls 0.4
File Interpreter ABI Platform Actions
datasette_hashed_urls-0.4-py3-none-any.whl Python 3 none any Details for datasette_hashed_urls-0.4-py3-none-any.whl

Total release size: 17.2 kB

Release files / datasette-hashed-urls-0.4.tar.gz

Download URL datasette-hashed-urls-0.4.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f6f9d69f9a4bd65947489b9a10f8143882abee444e24013bbc1cd430932c1358
BLAKE2b-256 checksum
How to use checksums
beacf991e3c41f6e935968c0ba5def91571ed69c97273568b77dadc488141ec3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / datasette_hashed_urls-0.4-py3-none-any.whl

Download URL datasette_hashed_urls-0.4-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b719001a4c8a5c39cf654dd595df2d1ee93ede52253dbec7098c5a4631b80588
BLAKE2b-256 checksum
How to use checksums
bf5180b6de42f85ffdcde2474977fc3ab4987e24379d8d2aa658897aed2589a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Security for datasette-hashed-urls 0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page