0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

🐛 Fixed empty amp page

closes #10547

- TODO: raise issue
This commit is contained in:
kirrg001 2019-02-27 17:50:20 +01:00
parent f33dacaf64
commit f8d1e38290
5 changed files with 57 additions and 7 deletions

View file

@ -64,7 +64,8 @@ function getPostData(req, res, next) {
} }
// @NOTE: amp is not supported for static pages // @NOTE: amp is not supported for static pages
helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {controller: 'posts', resource: 'posts'}}, res.locals) // @TODO: AMP should make an HTTP request to the Content API {version}
helpers.entryLookup(urlWithoutSubdirectoryWithoutAmp, {permalinks, query: {controller: 'postsPublic', resource: 'posts'}}, res.locals)
.then((result) => { .then((result) => {
if (result && result.entry) { if (result && result.entry) {
req.body.post = result.entry; req.body.post = result.entry;

View file

@ -39,10 +39,12 @@ function entryLookup(postUrl, routerOptions, locals) {
let options = { let options = {
include: 'author,authors,tags' include: 'author,authors,tags'
}; };
if (config.get('enableDeveloperExperiments')) { if (config.get('enableDeveloperExperiments')) {
options.context = {member: locals.member}; options.context = {member: locals.member};
} }
return api[routerOptions.query.controller]
return (api[routerOptions.query.controller] || api[routerOptions.query.resource])
.read(_.extend(_.pick(params, 'slug', 'id'), options)) .read(_.extend(_.pick(params, 'slug', 'id'), options))
.then(function then(result) { .then(function then(result) {
const entry = result[routerOptions.query.resource][0]; const entry = result[routerOptions.query.resource][0];

View file

@ -4,6 +4,7 @@ const should = require('should'),
cheerio = require('cheerio'), cheerio = require('cheerio'),
testUtils = require('../../utils'), testUtils = require('../../utils'),
configUtils = require('../../utils/configUtils'), configUtils = require('../../utils/configUtils'),
appsService = require('../../../server/services/apps'),
settingsService = require('../../../server/services/settings'), settingsService = require('../../../server/services/settings'),
themeService = require('../../../server/services/themes'), themeService = require('../../../server/services/themes'),
siteApp = require('../../../server/web/parent-app'); siteApp = require('../../../server/web/parent-app');
@ -20,7 +21,7 @@ describe('Integration - Web - Site', function () {
describe('default routes.yaml', function () { describe('default routes.yaml', function () {
before(function () { before(function () {
testUtils.integrationTesting.defaultMocks(sinon); testUtils.integrationTesting.defaultMocks(sinon, {amp: true, apps: true});
testUtils.integrationTesting.overrideGhostConfig(configUtils); testUtils.integrationTesting.overrideGhostConfig(configUtils);
return testUtils.integrationTesting.initGhost() return testUtils.integrationTesting.initGhost()
@ -30,6 +31,9 @@ describe('Integration - Web - Site', function () {
app = siteApp({start: true}); app = siteApp({start: true});
return testUtils.integrationTesting.urlService.waitTillFinished(); return testUtils.integrationTesting.urlService.waitTillFinished();
})
.then(() => {
return appsService.init();
}); });
}); });
@ -64,6 +68,22 @@ describe('Integration - Web - Site', function () {
}); });
}); });
it('serve amp', function () {
const req = {
secure: true,
method: 'GET',
url: '/html-ipsum/amp/',
host: 'example.com'
};
return testUtils.mocks.express.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
response.template.should.match(/amp\.hbs/);
response.body.should.match(/<h1>HTML Ipsum Presents<\/h1>/);
});
});
it('post not found', function () { it('post not found', function () {
const req = { const req = {
secure: true, secure: true,
@ -1749,7 +1769,7 @@ describe('Integration - Web - Site', function () {
describe('default routes.yaml', function () { describe('default routes.yaml', function () {
before(function () { before(function () {
testUtils.integrationTesting.urlService.resetGenerators(); testUtils.integrationTesting.urlService.resetGenerators();
testUtils.integrationTesting.defaultMocks(sinon); testUtils.integrationTesting.defaultMocks(sinon, {amp: true, apps: true});
testUtils.integrationTesting.overrideGhostConfig(configUtils); testUtils.integrationTesting.overrideGhostConfig(configUtils);
return testUtils.integrationTesting.initGhost() return testUtils.integrationTesting.initGhost()
@ -1759,6 +1779,9 @@ describe('Integration - Web - Site', function () {
app = siteApp({start: true}); app = siteApp({start: true});
return testUtils.integrationTesting.urlService.waitTillFinished(); return testUtils.integrationTesting.urlService.waitTillFinished();
})
.then(() => {
return appsService.init();
}); });
}); });
@ -1793,6 +1816,22 @@ describe('Integration - Web - Site', function () {
}); });
}); });
it('serve amp', function () {
const req = {
secure: true,
method: 'GET',
url: '/html-ipsum/amp/',
host: 'example.com'
};
return testUtils.mocks.express.invoke(app, req)
.then(function (response) {
response.statusCode.should.eql(200);
response.template.should.match(/amp\.hbs/);
response.body.should.match(/<h1>HTML Ipsum Presents<\/h1>/);
});
});
it('post not found', function () { it('post not found', function () {
const req = { const req = {
secure: true, secure: true,

View file

@ -122,7 +122,7 @@ describe('Unit - apps/amp/lib/router', function () {
urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/');
helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}) helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}})
.resolves({ .resolves({
entry: post entry: post
}); });
@ -139,7 +139,7 @@ describe('Unit - apps/amp/lib/router', function () {
urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/');
helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}).resolves({ helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}}).resolves({
entry: post entry: post
}); });
@ -154,7 +154,7 @@ describe('Unit - apps/amp/lib/router', function () {
urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/'); urlService.getPermalinkByUrl.withArgs('/welcome/').returns('/:slug/');
helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'posts', resource: 'posts'}}) helpers.entryLookup.withArgs('/welcome/', {permalinks: '/:slug/', query: {controller: 'postsPublic', resource: 'posts'}})
.rejects(new common.errors.NotFoundError()); .rejects(new common.errors.NotFoundError());
ampController.getPostData(req, res, function (err) { ampController.getPostData(req, res, function (err) {

View file

@ -1052,6 +1052,14 @@ module.exports = {
cacheStub.withArgs('permalinks').returns('/:slug/'); cacheStub.withArgs('permalinks').returns('/:slug/');
cacheStub.withArgs('labs').returns({publicAPI: true}); cacheStub.withArgs('labs').returns({publicAPI: true});
if (options.amp) {
cacheStub.withArgs('amp').returns(true);
}
if (options.apps) {
cacheStub.withArgs('active_apps').returns([]);
}
sandbox.stub(api.clients, 'read').returns(Promise.resolve({ sandbox.stub(api.clients, 'read').returns(Promise.resolve({
clients: [ clients: [
{slug: 'ghost-frontend', secret: 'a1bcde23cfe5', status: 'enabled'} {slug: 'ghost-frontend', secret: 'a1bcde23cfe5', status: 'enabled'}