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

Feature request: support optional rewrites #10

Closed
KnisterPeter opened this issue May 14, 2020 · 3 comments
Closed

Feature request: support optional rewrites #10

KnisterPeter opened this issue May 14, 2020 · 3 comments

Comments

@KnisterPeter
Copy link

I would need something like optiona/conditional rewrites. That could be similar as the spa conditionals --spa.asset-test-fs by checking if a file is available in the fs, then serve it, otherwise rewrite the request.

@75lb
Copy link
Member

75lb commented May 14, 2020

I'm not clear what you are asking me to do..

Maybe you could demonstrate what you are looking for with a simple middleware example. Please have a play with creating a simple middleware module: https://github.com/lwsjs/local-web-server/wiki/Creating-middleware.

@KnisterPeter
Copy link
Author

This is it:

const Rewrite = require('lws-rewrite');
const path = require('path');
const fs = require('fs');

class ConditionalRewrite {
  constructor() {
    this.rewrite = new Rewrite();
  }

  optionDefinitions() {
    return this.rewrite.optionDefinitions();
  }

  middleware(options, lws) {
    const middlewares = this.rewrite.middleware(options, lws);
    const directory = path.resolve(options.directory);

    return middlewares.map((middleware) => {
      return async (ctx, next) => {
        const { url } = ctx.request;
        const fullPath = path.join(directory, url);

        try {
          const stats = await fs.promises.stat(fullPath);
          if (stats.isFile()) {
            return next();
          }
        } catch {
          //
        }
        return middleware(ctx, next);
      };
    });
  }
}

module.exports = ConditionalRewrite;

So basically, if a file is at the requested static location serve that and do not rewrite the request to something else.

@75lb
Copy link
Member

75lb commented May 15, 2020

I see, thanks.. This would introduce an I/O performance cost since it touches the filesystem for every incoming request.. this cost would be noticeable for users building an SPA (which involves a lot of URLs which do not map to the filesystem, e.g. /user/3 etc) since this middleware would try to find /user/3 and every other URL on the filesystem before responding..

It's not something I would consider adding to the default stack (unless there's genuine demand for it) but feel free to publish and share this middleware plugin so others can use it, if required.

@75lb 75lb closed this as completed May 15, 2020
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

No branches or pull requests

2 participants