Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support filtering files by extension #145

Closed
ghost opened this issue Oct 2, 2020 · 6 comments · Fixed by #172
Closed

Support filtering files by extension #145

ghost opened this issue Oct 2, 2020 · 6 comments · Fixed by #172
Labels
good first issue Good for newcomers semver-minor Issue or PR that should land as semver minor

Comments

@ghost
Copy link

ghost commented Oct 2, 2020

🚀 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
@mcollina
Copy link
Member

mcollina commented Oct 2, 2020

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added good first issue Good for newcomers semver-minor Issue or PR that should land as semver minor labels Oct 2, 2020
@ghost
Copy link
Author

ghost commented Oct 5, 2020

@mcollina Sure, I should be able to take a first stab. Quick question, did you have a preference for the implementation? The most flexible/generic approach would likely be supporting a filter function that would accept the URL/filename and return a boolean. This would allow users to put as simple or as complex of logic as they want. Let me know your recommendation and I can work on getting something written.

Thanks.

@mcollina
Copy link
Member

mcollina commented Oct 6, 2020

A filter function would be great.

@ghost
Copy link
Author

ghost commented Oct 11, 2020

I just realized you could pass a glob to the wildcard option. If that works the way I think it would, that would actually let you filter (as long as it was based on filename logic). It would also potentially solve or at least be a workaround for supporting multiple root paths as well, because you could point the root at the shared parent and then use the glob to restrict what child folders should be accessible.

Can you confirm if my understanding of how the wildcard/glob works?

@mcollina
Copy link
Member

Yes, it should work the way you assume.

@ghostd ghostd mentioned this issue Jan 30, 2021
4 tasks
mcollina added a commit to ghostd/fastify-static that referenced this issue Feb 18, 2021
@gajus
Copy link

gajus commented Mar 10, 2022

TLDR use allowedPath, e.g.

allowedPath: (pathName) => {
  if (environmentIsProduction) {
    return !pathName.includes('.map');
  }

  return true;
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers semver-minor Issue or PR that should land as semver minor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants