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

Make static-serve inherit the "etag" application setting when used with Express #64

Closed
wants to merge 1 commit into from

Conversation

sjanuary
Copy link

From the Express 5.0 roadmap expressjs/express#2237 and expressjs/express#2317

If the etag setting is not explicitly set for serve-static and it is being used with Express, serve-static will inherit the etag setting from Express.

I have written tests for this PR and run them, but I have written them against the Express project rather than serve-static because the scenario requires both. Tests look like this:

it('should override static-serve etag setting if not set', function (done) {
      var fixtures = __dirname + '/fixtures';
      var app = express();

      app.disable('etag');
      app.use(express.static(fixtures));

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(!('etag' in res.headers), 'should not have etag header')
      })
      .expect(200);

      app.set('etag', true)

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(('etag' in res.headers), 'should have etag header')
      })
      .expect(200, done);
    })

    it('should not override static-serve etag setting if set', function (done) {
      var fixtures = __dirname + '/fixtures';
      var app = express();

      app.disable('etag');
      app.use(express.static(fixtures, {'etag' : true}));

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(('etag' in res.headers), 'should not have etag header')
      })
      .expect(200);

      app.set('etag', true)

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(('etag' in res.headers), 'should have etag header')
      })
      .expect(200, done);
    })

    it('should not override static-serve etag setting if disabled', function (done) {
      var fixtures = __dirname + '/fixtures';
      var app = express();

      app.disable('etag');
      app.use(express.static(fixtures, {'etag' : false}));

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(!('etag' in res.headers), 'should not have etag header')
      })
      .expect(200);

      app.set('etag', true)

      request(app)
      .get('/name.txt')
      .expect(function (res) {
        assert.ok(!('etag' in res.headers), 'should have etag header')
      })
      .expect(200, done);
    })

@dougwilson
Copy link
Contributor

Middleware are self-contained. This change needs to be done in Express, not this module.

@sjanuary
Copy link
Author

@dougwilson ok, I understand the principle that serve-static shouldn't know about Express, but it doesn't seem like there's any way to change the options after staticServe() has been called. Would either a PR that looks for the etag setting on the request object or a PR that allows the options to be changed be more likely to be accepted in this module? I don't think it's possible to do this entirely in Express.

@dougwilson
Copy link
Contributor

No PR for this would be accepted here, as even your suggestion wouldn't work, because of race conditions.

If you're not sure how to approach this change, you may want to discuss in our Gitter room or over in Express to better understand the requirements or how to approach the problem before tackling it. Currently I have a backlog of PRs to review, so will not have time to personally discuss this for a few weeks.

@sjanuary
Copy link
Author

Ok, thanks for the feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants