Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

bug: directory index route root priority is overwritten when wildcard is false

Open Beginner friendly
#617 0 comments 0 reactions 0 assignees View on GitHub

@TalhaHunter101 is already working on this.

Since Oct 11, 2026.

  • #618 by @TalhaHunter101 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js
Domain
backend, web-dev

Research direction

The report points to index.js lines 206–209 and describes a reproduction with two root directories and wildcard: false. Read that index route logic and run the reproduction or the relevant existing tests. Done when directory requests keep serving the matching index file from the first root in the configured priority order.

Written by the indexing model from the issue text.

Description

Prerequisites
Describe the Bug

When wildcard: false and multiple root directories are configured (e.g. root: [root1, root2]), the documentation states that directories are searched in priority order ("first found, first served"):

An array of directories can be provided to serve multiple static directories under a single prefix. Files are served in a "first found, first served" manner, so list directories in order of priority.

However, in index.js lines 206–209:

const key = path.posix.basename(route)
if (indexes.has(key) && !indexDirs.has(key)) {
  indexDirs.set(path.posix.dirname(route), rootPath)
}

The condition checks !indexDirs.has(key), where key is the filename (e.g. 'index.html' or 'foobar.html'), but the map entries in indexDirs use directory paths (path.posix.dirname(route)) as keys!

Because the filename string is never a key in indexDirs (which contains directory paths like '/' or '/sub'), !indexDirs.has(key) always evaluates to true whenever an index file is found in subsequent roots with a different alternative index name or secondary root. As a result, subsequent lower-priority roots overwrite higher-priority roots in indexDirs.set(path.posix.dirname(route), rootPath), causing directory requests (e.g. / or /<dir>/) to serve from the wrong root!

Steps to Reproduce
const Fastify = require('fastify')
const fastifyStatic = require('@fastify/static')
const path = require('node:path')
const fs = require('node:fs')

// Setup two roots: root1 (high priority) and root2 (lower priority)
const root1 = path.join(__dirname, 'root1')
const root2 = path.join(__dirname, 'root2')
fs.mkdirSync(path.join(root1, 'sub'), { recursive: true })
fs.mkdirSync(path.join(root2, 'sub'), { recursive: true })

fs.writeFileSync(path.join(root1, 'sub', 'index.htm'), 'from root1')
fs.writeFileSync(path.join(root2, 'sub', 'index.html'), 'from root2')

const fastify = Fastify()
fastify.register(fastifyStatic, {
  root: [root1, root2],
  wildcard: false,
  index: ['index.htm', 'index.html']
})

await fastify.listen({ port: 0 })
const port = fastify.server.address().port
const res = await fetch(`http://localhost:${port}/sub/`)
console.log(await res.text())
// Expected: 'from root1'
// Actual: 'from root2' (lower-priority root overwrote higher-priority root!)
Root Cause

indexDirs is a Map from directory path to rootPath. Line 207 checks whether indexDirs contains key (the basename), rather than checking whether indexDirs already has the directory path path.posix.dirname(route).

Expected Behavior

indexDirs should check whether the directory path already has a registered root (!indexDirs.has(dir)), preserving the higher-priority root.

Dominant language
JavaScript
Stars
499
Forks
131
Avg merge
1d 18h
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fastify/fastify-static

All issues in fastify/fastify-static

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.