r/htmx • • 1d ago

FastAPI freeze

https://github.com/listfold/fastapi-freeze
1 Upvotes

1 comment sorted by

1

u/iainmaitland 18h ago

I just updated the README to properly describe this, with particular reference to HTMX:

fastapi-freeze

"🌎🧑‍🚀 wait it's all a static site generator ... 🔫🧑‍🚀 always has been"

Now you can use your favorite full-stack framework as a static site generator.

Wait isn't this just a cache?

Yes basically.

To understand fastapi-freeze, imagine making all possible valid HTTP requests to your app's REST API, taking those HTML responses, putting them someplace (normally a build directory) and calling that the "static build".

Now you no longer need to do any computation to produce a valid response to a GET request, rather the system simply returns the HTML that corresponds to the request parameters from the directory of static files.

Furthermore, with an exhaustive catalog of HTML fragments, "serving" your site becomes a matter of putting all those fragments in object storage/browser-storage/wherever rather than hosting a long running server process.

For example, take a look at the example blog: a FastAPI + HTMX app in examples/blog, frozen and published on GitHub Pages.

With HTMX we are not confined to monolithic HTML pages either. In fact, pages can be dynamically (on the client) composed from static HTML fragments that are swapped in and out of the DOM as app state changes. For example paretoparty.com is a frozen fastapi app with rich data visualizations and dynamic behavior.

OK, what do I need to do to make my app compatible?

There are some details in that earlier answer that need some explaining and will help guide how you factor a FastAPI codebase to be "freezeable".

  • You must have a REST API (in the truest sense - one that responds with HTML). Only routes that respond to GET and declare an HTML response (e.g. response_class=HTMLResponse) are freezeable.
  • State lives in the URL path, not the query string. A static host looks files up by path and ignores ?query, so fastapi-freeze freezes path parameters (/posts/{author}/{title_slug}/) and skips routes with query parameters, with a warning.

    • fastapi-freeze compiles the catalog of static HTML fragments into a directory tree that mirrors the app's URL paths. For example, the route /blog/posts/{author}/{title_slug}/ becomes a static file blog/posts/iain/hello-world/index.html. A static host serves that file for GET /blog/posts/iain/hello-world/. So any HTTP GET request and therefore any client works with the fragments: a browser, fetch, HTMX, React, curl, or a crawler etc. (we like using HTMX).
  • Responses from the API must be finite. fastapi-freeze won't work if there are a lot of dynamically generated HTML fragments flying around (more than about 20,000 in total, the default max_files limit, which you can raise), so if you're making a spreadsheet app this isn't a good fit.

    • With diligent constraining of the types in the data models of your app you can get a finite OpenAPI spec (think preferring enum over str). See Supported types. One of fastapi-freeze's key innovations is using the finite types provided by pydantic to both decide if the app can be frozen and to produce the cartesian product of route params from the types directly.
    • Where that way of thinking isn't possible (like maybe you really need the title of your blog posts (so a str) in the path of the 'get-blog-post' route) fastapi-freeze supports a @freeze_values decorator. URL generators explains this more fully.

      • Conceptually, for a route whose path parameter could take infinitely many values (e.g. a str):
      1. You write a function that lists every value for which data exists, usually with a SELECT over the database.
      2. It returns an iterable of dicts, one dict of path parameters for each URL to freeze.
      3. fastapi-freeze calls it after the app's lifespan startup, so the database is ready, and freezes exactly those URLs.
  • HTML can carry JSON, so you can also move the rendering of data-heavy views to the client, one view at a time. Put the data in a <script type="application/json"> block, or better, in a static .json file that is fetched once. Client-side sorting and filtering also take those parameters out of the URL, so they no longer multiply the number of frozen files.

Quickstart

Take a look at the example blog: a FastAPI + HTMX app generated from the source at examples/blog.