Skip to content

Support filtering files by extension #145

@ghost

Description

🚀 Feature Proposal

Support filtering files by extension.

Motivation

Sometimes you may want to keep your JavaScript or CSS files alongside a component. For those not using build steps, it'd be great to have the ability to filter what files to serve by pointing root at the directory, but only serving certain files based off extension (or more generically could be pattern matching in general).

Example

Assume we have the following directory structure:

src/
├── components/
│   ├── cool-feature
│   │   ├── index.pug
│   │   ├── index.js
│   │   └── index.css

We'd register our plugin like so (assume PWD is src):

app.register(require('fastify-static'), {
  root: path.join(__dirname, 'components')],
  prefix: '/static',
  extensions: ['.js', '.css'],
});

Example of general pattern instead:

app.register(require('fastify-static'), {
  root: path.join(__dirname, 'components')],
  prefix: '/static',
  filter: /\.(?:js|css)$/,
});

Examples of access:

/static/cool-feature/index.js -> SUCCESS
/static/cool-feature/index.css -> SUCCESS
/static/cool-feature/index.pug -> 404

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerssemver-minorIssue or PR that should land as semver minor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions