Skip to content

feat(middleware): add content option (options.content.filter, options.content.transform) #336

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ module.exports = function wrapper(context) {
} catch (e) {
notFound = true;
}
const currentRequest = Object.assign({}, {
Copy link
Member

@michael-ciniawsky michael-ciniawsky Aug 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const currentRequest => const request (we don't have different/multiple request(s) in this context)

url: req.url,
method: req.method,
headers: req.headers,
filename,
notFound
});
if (context.options && context.options.content && context.options.content.filter) {
if (!context.options.content.filter(currentRequest)) {
return goNext();
}
}
if (notFound) {
return resolve(goNext());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: why is it that every other "goNext" is a regular return but this one is a return resolve(goNext())?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question but I don't know the answer, try to remove the resolve and see what's happening (if something happens). Please add a comment that this is something to investigate and clearify :)

}
Expand Down
36 changes: 36 additions & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,42 @@ describe('Server', () => {
});
});

describe('filters', () => {
before((done) => {
app = express();
const compiler = webpack(webpackConfig);
instance = middleware(compiler, {
stats: 'errors-only',
content: {
filter(currentRequest) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentRequest => res || request

let result = true;
if (/.js$/.test(currentRequest.filename)) {
result = false;
}
return result;
}
},
logLevel,
publicPath: '/public/'
});
app.use(instance);
listen = listenShorthand(done);
});
after(close);

it('GET request to bundle file with /.js$/ filtered', (done) => {
request(app).get('/public/bundle.js')
.expect(404, done);
});

it('request to image with /.js$/ filtered', (done) => {
request(app).get('/public/svg.svg')
.expect('Content-Type', 'image/svg+xml; charset=UTF-8')
.expect('Content-Length', '4778')
.expect(200, done);
});
});

describe('no index mode', () => {
before((done) => {
app = express();
Expand Down