Skip to content

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

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

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

Conversation

@EnesKartal

Copy link
Copy Markdown

Closes #217.

Problem

app.use('/foo/bar', handler) never matches incoming requests that should hit it (such as /foo/bar/main.js), resulting in an unexpected 404.

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 was in handler(): incoming request paths were truncated down to their first segment (via value()) before checking bwares/apps, while those maps are keyed by the full base string passed to use(). Consequently, /foo/bar/main.js was reduced to /foo for lookup, failing to match /foo/bar.

Solution

  • Added a matchBase(path, keys) helper that selects the longest registered key that is a full-segment prefix of path, ensuring multi-segment base prefixes are properly matched without false-matching siblings (e.g. /foo will not match /foobar).
  • Updated handler() to use matchBase for looking up both bwares and apps.
  • Updated add() to use matchBase when checking against mounted apps sub-applications.
  • Added comprehensive tests for multi-segment base middleware and sub-applications in tests/polka.js.

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