Skip to content

Conversation

@noelwelsh
Copy link
Contributor

In development mode we don't want the web browser to cache assets, such as JS or CSS, that are under development. This PR implements this feature, which consists of a type of Route for managing assets. It:

  • sets up a directory watcher for a given directory, calculating hashes for each file in the directory and recalculating the hash on change.
  • implements pathTo by replaces file names with the file name + hash
  • serves files by removing the hash and serving the underlying file

A Handler is now a description, whilst the actual handling is carried
out by a RouteHandler. This allows a Handler to construct Resources when
it builds a RouteHandler. This will be used by the asset pipeline to
construct file watchers.

This requires the Runtime be factored into two parts, one available
before the http4s server is constructed, and one available after it is
constructed.
This will store the cache busting asset handler
- Rename `assets` to `asset`, following convention of singular, not
plural, package names.

- Extract `HashingFileWatcher` from `AssetRoute` and start adding tests

- Create type for hex encoded strings
- Add ScalaDoc

- Use a normal ADT for events, instead of trying to be clever with a
  union type
This is caused by our dependency on HexFormat, but Scala is moving
towards this as a minimum JDK dependency so it shouldn't cause us a problem.
Still work to do, but this fixes compilation errors.
Test is more robust and runs a bit faster
- Allow adjusting polling interval so that MacOS can increase the
interval

- Make test more robust by halting when all events have been observed,
instead of simple sampling in a duration.
Use FileNameHasher for more testable code
Not sure I want to go this route, but will keep it for now.
- the BaseRuntime can accumulate resources that will be built when the
KropRuntime is made

- Supplying a key allows these resources to fetched from the KropRuntime
This is what we want for asset serving
- Add module name so it gets published under a useful name
- Remove unused dependencies
- AssetRoute can actually be used in an application :-)
- Refactoring of Route & Handler types to be more ergonomic
This test is intermittently failing on CI. Give it longer to run.
This is preventing CI from completing
Handlers are context functions that has access to the KropRuntime, so
they can access the KropRuntime if they want to.
- Add documentation to asset method

- Returned path includes the AssetRoute's base, and so is suitable for
use as an href.

- Small amount of cleanup

- Add convenience constructor
CI continues to fail on this test, so give it longer to run
So asset documentation can compile
Only fails on Github Actions!
@noelwelsh noelwelsh marked this pull request as ready for review December 8, 2025 13:57
@noelwelsh noelwelsh merged commit 96098e5 into main Dec 8, 2025
9 checks passed
@noelwelsh noelwelsh deleted the feature/cache-busting branch December 8, 2025 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants