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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fixed missing source + resized images producing rendered 404 #19869

Merged
merged 2 commits into from Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ghost/core/core/frontend/web/middleware/handle-image-sizes.js
Expand Up @@ -139,6 +139,11 @@ module.exports = function handleImageSizes(req, res, next) {
if (err.code === 'SHARP_INSTALLATION' || err.code === 'IMAGE_PROCESSING' || err.errorType === 'NoContentError') {
return redirectToOriginal();
}

if (err.errorType === 'NotFoundError') {
return next();
}

next(err);
});
};
25 changes: 25 additions & 0 deletions ghost/core/test/e2e-frontend/static-files.test.js
@@ -0,0 +1,25 @@
const assert = require('assert/strict');
const {agentProvider} = require('../utils/e2e-framework');

describe('Static files', function () {
let frontendAgent;
let ghostServer;

before(async function () {
const agents = await agentProvider.getAgentsWithFrontend();
frontendAgent = agents.frontendAgent;
ghostServer = agents.ghostServer;
});

after(async function () {
await ghostServer.stop();
});

it('serves unstyled 404 for non-existing resized + original files', async function () {
const response = await frontendAgent
.get('/content/images/size/w2000/1995/12/daniel.jpg')
.expect(404);

assert.ok(response.text.includes('NotFoundError: Image not found'));
});
});
Expand Up @@ -3,6 +3,7 @@ const sinon = require('sinon');
const storage = require('../../../../../core/server/adapters/storage');
const activeTheme = require('../../../../../core/frontend/services/theme-engine/active');
const handleImageSizes = require('../../../../../core/frontend/web/middleware/handle-image-sizes.js');
const errors = require('@tryghost/errors');
const imageTransform = require('@tryghost/image-transform');

const fakeResBase = {
Expand Down Expand Up @@ -673,5 +674,36 @@ describe('handleImageSizes middleware', function () {
done();
});
});

it('goes to next middleware with no error if source and resized image 404', function (done) {
dummyStorage.exists = async function () {
return false;
};
dummyStorage.read = async function () {
throw new errors.NotFoundError({
message: 'File not found'
});
};

const fakeReq = {
url: '/size/w1000/2020/02/test.png',
originalUrl: '/2020/02/test.png'
};

const fakeRes = {
redirect() {
done(new Error('Should not have called redirect'));
},
setHeader() {},
type: function () {}
};

handleImageSizes(fakeReq, fakeRes, function next(err) {
if (err) {
return done(err);
}
done();
});
});
});
});