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

🐛 Fixed 404 in collection index page if using data.slug #10718

Merged
merged 2 commits into from
Apr 30, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions core/server/data/meta/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ function trimSchema(schema) {
function trimSameAs(data, context) {
var sameAs = [];

if (context === 'post') {
if (data.post.primary_author.website) {
sameAs.push(escapeExpression(data.post.primary_author.website));
if (context === 'post' || context === 'page') {
naz marked this conversation as resolved.
Show resolved Hide resolved
if (data[context].primary_author.website) {
sameAs.push(escapeExpression(data[context].primary_author.website));
}
if (data.post.primary_author.facebook) {
sameAs.push(social.urls.facebook(data.post.primary_author.facebook));
if (data[context].primary_author.facebook) {
sameAs.push(social.urls.facebook(data[context].primary_author.facebook));
}
if (data.post.primary_author.twitter) {
sameAs.push(social.urls.twitter(data.post.primary_author.twitter));
if (data[context].primary_author.twitter) {
sameAs.push(social.urls.twitter(data[context].primary_author.twitter));
}
} else if (context === 'author') {
if (data.author.website) {
Expand All @@ -69,6 +69,8 @@ function getPostSchema(metaData, data) {
var description = metaData.excerpt ? escapeExpression(metaData.excerpt) : null,
schema;

const context = data.page ? 'page' : 'post';
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work with v0.1? v0.1 had no differentiation for posts & pages.
getPostSchema is not only used for the "data" property. It's also used if you serve a post or a page.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would default to post in that case, but will double check 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verified and works as expected. Merging these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was thinking of adding a parameter context which would be for the caller - getSchema to specify, but didn't go that route as it's not really guaranteed what the properties would be present if e.g. amp condition kicks in. Additionally, the logic below depends on the post/page property being present and the 'context' check - _.includes(context, 'post') is not directly guaranteeing that I think.

These ifs for post or page are another sign of issues with meta layer 🤷‍♂️


schema = {
'@context': 'https://schema.org',
'@type': 'Article',
Expand All @@ -79,12 +81,12 @@ function getPostSchema(metaData, data) {
},
author: {
'@type': 'Person',
name: escapeExpression(data.post.primary_author.name),
name: escapeExpression(data[context].primary_author.name),
image: schemaImageObject(metaData.authorImage),
url: metaData.authorUrl,
sameAs: trimSameAs(data, 'post'),
description: data.post.primary_author.metaDescription ?
escapeExpression(data.post.primary_author.metaDescription) :
sameAs: trimSameAs(data, context),
description: data[context].primary_author.metaDescription ?
escapeExpression(data[context].primary_author.metaDescription) :
null
},
headline: escapeExpression(metaData.metaTitle),
Expand Down
10 changes: 9 additions & 1 deletion core/server/services/routing/helpers/fetch-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const defaultQueryOptions = {
}
};

const defaultDataQueryOptions = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kirrg001 changed the config to be more explicit. Think it's pretty readable this way

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks okay as a hot fix, but IMO the includes shouldn't be here at all. They should be part of the target api version config (long-term).

post: _.cloneDeep(defaultQueryOptions),
page: _.cloneDeep(defaultQueryOptions),
tag: null,
author: null
};

const defaultPostQuery = _.cloneDeep(queryDefaults);
defaultPostQuery.options = defaultQueryOptions.options;

Expand Down Expand Up @@ -97,7 +104,8 @@ function fetchData(pathOptions, routerOptions, locals) {

// CASE: fetch more data defined by the router e.g. tags, authors - see TaxonomyRouter
_.each(routerOptions.data, function (query, name) {
props[name] = processQuery(query, pathOptions.slug, locals);
const dataQueryOptions = _.merge(query, defaultDataQueryOptions[name]);
props[name] = processQuery(dataQueryOptions, pathOptions.slug, locals);
});

return Promise.props(props)
Expand Down
100 changes: 100 additions & 0 deletions core/test/unit/data/meta/schema_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,106 @@ describe('getSchema', function () {
done();
});

it('should return page schema if context starts with page', function (done) {
var metadata = {
blog: {
title: 'Blog Title',
url: 'http://mysite.com',
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg',
dimensions: {
width: 500,
height: 500
}
}
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg',
dimensions: {
width: 500,
height: 500
}
},
authorFacebook: 'testuser',
creatorTwitter: '@testuser',
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Page Title',
url: 'http://mysite.com/post/my-page-slug/',
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: {
url: 'http://mysite.com/content/image/mypagecoverimage.jpg',
dimensions: {
width: 500,
height: 500
}
},
keywords: ['one', 'two'],
metaDescription: 'Post meta description',
excerpt: 'Custom excerpt for description'
}, data = {
context: ['page'],
page: {
primary_author: {
name: 'Page Author',
website: 'http://myblogsite.com/',
bio: 'My author bio.',
facebook: 'testuser',
twitter: '@testuser'
}
}
},
schema = getSchema(metadata, data);

should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Article',
author: {
'@type': 'Person',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/me.jpg',
width: 500,
height: 500
},
name: 'Page Author',
sameAs: [
'http://myblogsite.com/',
'https://www.facebook.com/testuser',
'https://twitter.com/testuser'
],
url: 'http://mysite.com/author/me/'
},
dateModified: '2016-01-21T22:13:05.412Z',
datePublished: '2015-12-25T05:35:01.234Z',
description: 'Custom excerpt for description',
headline: 'Page Title',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/content/image/mypagecoverimage.jpg',
width: 500,
height: 500
},
keywords: 'one, two',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': 'http://mysite.com'
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/logo.jpg',
width: 500,
height: 500
}
},
url: 'http://mysite.com/post/my-page-slug/'
});
done();
});

it('should return post schema if context starts with amp', function (done) {
var metadata = {
blog: {
Expand Down