Skip to content

fix: resolve wildcard paths in encapsulated contexts#574

Merged
mcollina merged 1 commit intofastify:mainfrom
mcollina:fix/wildcard-encapsulated-prefix
Apr 18, 2026
Merged

fix: resolve wildcard paths in encapsulated contexts#574
mcollina merged 1 commit intofastify:mainfrom
mcollina:fix/wildcard-encapsulated-prefix

Conversation

@mcollina
Copy link
Copy Markdown
Member

Problem

@fastify/static v9.1.1 switched wildcard path resolution to derive the pathname from req.raw.url and the local plugin prefix.

That breaks encapsulated registrations because req.raw.url includes the outer plugin prefix while the local static prefix does not. As a result, nested wildcard mounts fall through to reply.callNotFound() and valid static assets start returning 404.

This reproduces the regression reported in fastify/fastify-swagger-ui#268.

Solution

  • derive the wildcard pathname from req.routeOptions.url, which includes the full routed prefix
  • keep the raw URL based decoding logic introduced in v9.1.1 so encoded path separators remain blocked
  • add a regression test for encapsulated contexts

Testing

  • npm test
  • npm run lint

Copy link
Copy Markdown
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 32af863 into fastify:main Apr 18, 2026
14 checks passed
@mcollina mcollina deleted the fix/wildcard-encapsulated-prefix branch April 18, 2026 14:06
lpatiny added a commit to cheminfo/surge that referenced this pull request Apr 20, 2026
@fastify/static 9.1.1 rewrote the wildcard route handler to use
getPathnameForSend(req.raw.url, prefix) where prefix is only the local
plugin prefix. When @fastify/swagger-ui registers it inside its
/documentation-prefixed encapsulated scope, the handler fails to strip
the parent prefix and returns 404 for every /documentation/static/*.css
and .js asset, so the Swagger UI page renders blank.

- Pin @fastify/static to 9.1.0 in dependencies and overrides so
  swagger-ui's transitive copy is also pinned.
- Commit package-lock.json (drop the .npmrc package-lock=false flag) so
  the pinned version is reproducible in CI and the Docker image build.
- Extract server construction into src/server.js (buildServer) so the
  app can be exercised via fastify.inject() in tests.
- Add a regression test covering GET /documentation/ and
  GET /documentation/static/index.css.

Upstream fix fastify/fastify-static#574 is
merged but not yet released; once @fastify/static 9.1.2+ ships with the
fix, this pin can be removed.

refs: fastify/fastify-static#573
refs: fastify/fastify-swagger-ui#268
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