Skip to content

fix: support multi-segment base paths in use() - #227

Closed
afonsojanu wants to merge 1 commit into
lukeed:mainfrom
afonsojanu:fix/multi-segment-use-base
Closed

afonsojanu wants to merge 1 commit into
lukeed:mainfrom
afonsojanu:fix/multi-segment-use-base

Conversation

@afonsojanu

Copy link
Copy Markdown

Closes #217.

app.use('/foo/bar', handler) never actually gets hit, even for requests that should match it — it always 404s. Here's the repro from the issue:

const app = Polka();
app.use('/baz', Sirv('./static/script'));       // works: /baz/main.js
app.use('/foo/bar', Sirv('./static/script'));   // 404:   /foo/bar/main.js

The cause is in handler(): the incoming request path is truncated down to just its first "folder" (via value()) before being looked up in bwares/apps, but those tables are keyed by the full base string passed to use(). So /foo/bar/main.js gets reduced to /foo for the lookup, which never matches the /foo/bar key it was actually stored under, and the middleware/sub-app silently never runs. Single-segment bases (/baz) happen to work only because there's nothing to truncate.

This adds a small matchBase() helper that scans the registered keys and picks the longest one that's a full path-segment prefix of the request path (so /foo won't accidentally match a request for /foobar), and uses it for both the bwares and apps lookups instead of the single truncated segment. Single-segment bases keep behaving exactly as before.

Added two tests: one covering a multi-segment middleware base (plus making sure a sibling single-segment path under the same first folder isn't affected), and one covering use(base, subApp) with a multi-segment base.

Ran the full existing suite before and after with git stash — same single pre-existing failure both times (polka/url :: usage — throws if no input, unrelated to this change, looks like a Node version difference in the url.parse() deprecation path). Everything else, including the 2 new tests, passes clean.

app.use('/foo/bar', handler) never actually matched incoming requests
because the request path was truncated down to just its first segment
before being looked up against the (correctly, full-path-keyed) bwares
and apps tables, so anything mounted on a base with more than one
"folder" silently 404'd.

Replaces that truncated lookup with a small helper that scans the
registered base keys and picks the longest one that's a full-segment
prefix of the request path, so both middleware bases and sub-app
mounts work regardless of how many segments they have. Single-segment
bases keep working exactly as before.
@afonsojanu

Copy link
Copy Markdown
Author

Closing this for now as part of a clean slate — stepping back from everything I had open across these repos.

@afonsojanu afonsojanu closed this Oct 5, 2026
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.

Cannot use app.use with a base that contains more than one slash

1 participant