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

Allows setting content type header via middleware #47

Merged
merged 3 commits into from
Mar 13, 2023
Merged

Allows setting content type header via middleware #47

merged 3 commits into from
Mar 13, 2023

Conversation

sguter90
Copy link

Currently the "Content-Type" header is automatically set to the value returned by mime.getType.
If the "Content-Type" header was already set by a middleware then this header will be overwritten.

Within this change the "Content-Type" header is only set if it is not already defined.
The getFileContentType method is added to split Content-Type detection from rendering logic.
If the Content-Type can't be detected, then no header is sent.

An example middleware which I use for a website which I migrated from PHP to 11ty:

function (req, res, next) {
        if (/.*\.php$/.test(req.url)) {
          res.setHeader('Content-Type', 'text/html; charset=utf-8');
        }

        next();
}

@zachleat
Copy link
Member

I think I’m on board here but it looks like you checked in the sample middleware to cli.js by accident, no? That seems like test code

cli.js Outdated Show resolved Hide resolved
@zachleat zachleat merged commit 256c0d2 into 11ty:main Mar 13, 2023
@zachleat zachleat added this to the Eleventy Dev Server v1.0.4 milestone Mar 13, 2023
@zachleat
Copy link
Member

Shipping with v1.0.4!

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

Successfully merging this pull request may close these issues.

2 participants