🐛 Fixed default sort for the content API posts endpoint with included relations (#20333)

ref https://linear.app/tryghost/issue/CFR-31/
- when relations were included, erroneous logic resulted in the model's
default sort being applied
- the model default sort is not intended for the content API and
needlessly slowed down responses
- there's a change for users here that should be incredibly unlikely to
be hit; default sort is `published_at desc` which will be secondarily
sorted by `id desc` instead of `published_at desc, updated_at desc, id
desc`

This is a very significant performance improvement for content API
requests with includes for sites with a significant amount of data,
which will primarily impact those using Ghost as a CMS or theme {{#get}}
helpers.
This commit is contained in:
Steve Larson 2024-06-05 13:26:10 -05:00 committed by GitHub
parent 9ca1f3ce24
commit e49021b7ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 97 additions and 104 deletions

View File

@ -92,18 +92,11 @@ function defaultRelations(frame) {
}
function setDefaultOrder(frame) {
let includesOrderedRelations = false;
if (frame.options.withRelated) {
const orderedRelations = ['author', 'authors', 'tag', 'tags'];
includesOrderedRelations = _.intersection(orderedRelations, frame.options.withRelated).length > 0;
}
if (!frame.options.order && !includesOrderedRelations && frame.options.filter) {
if (!frame.options.order && frame.options.filter) {
frame.options.autoOrder = slugFilterOrder('posts', frame.options.filter);
}
if (!frame.options.order && !frame.options.autoOrder && !includesOrderedRelations) {
if (!frame.options.order && !frame.options.autoOrder) {
frame.options.order = 'published_at desc';
}
}

View File

@ -786,100 +786,6 @@ Object {
},
},
"posts": Array [
Object {
"access": true,
"authors": Array [
Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "1",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Joe Bloggs",
"profile_image": "https://example.com/super_photo.jpg",
"slug": "joe-bloggs",
"twitter": null,
"url": "http://127.0.0.1:2369/author/joe-bloggs/",
"website": null,
},
Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "6193c685e792de832cd08141",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Slimer McEctoplasm",
"profile_image": null,
"slug": "slimer-mcectoplasm",
"twitter": null,
"url": "http://127.0.0.1:2369/404/",
"website": null,
},
],
"canonical_url": null,
"codeinjection_foot": null,
"codeinjection_head": null,
"comment_id": "618ba1ffbe2896088840a6e7",
"comments": false,
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"custom_excerpt": null,
"custom_template": null,
"email_subject": null,
"excerpt": " * Lorem
* Aliquam
* Tortor
* Morbi
* Praesent
* Pellentesque
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
1234abcdefghijkl
Definition listConsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim venia",
"feature_image": null,
"feature_image_alt": null,
"feature_image_caption": null,
"featured": true,
"frontmatter": null,
"html": "<!--kg-card-begin: markdown--><p><nav><ul><li><a href=\\"#nowhere\\" title=\\"Anchor URL\\">Lorem</a></li><li><a href=\\"http://127.0.0.1:2369/about#nowhere\\" title=\\"Relative URL\\">Aliquam</a></li><li><a href=\\"//somewhere.com/link#nowhere\\" title=\\"Protocol Relative URL\\">Tortor</a></li><li><a href=\\"http://somewhere.com/link?ref=127.0.0.1#nowhere\\" title=\\"Absolute URL\\">Morbi</a></li><li><a href=\\"#nowhere\\" title=\\"Praesent dapibus, neque id cursus faucibus\\">Praesent</a></li><li><a href=\\"#nowhere\\" title=\\"Pellentesque fermentum dolor\\">Pellentesque</a></li></ul></nav><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p><table><thead><tr><th>1</th><th>2</th><th>3</th><th>4</th></tr></thead><tbody><tr><td>a</td><td>b</td><td>c</td><td>d</td></tr><tr><td>e</td><td>f</td><td>g</td><td>h</td></tr><tr><td>i</td><td>j</td><td>k</td><td>l</td></tr></tbody></table><dl><dt>Definition list</dt><dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</dd><dt>Lorem ipsum dolor sit amet</dt><dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</dd></dl><ul><li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li><li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li><li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li><li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li></ul></p><!--kg-card-end: markdown-->",
"id": "618ba1ffbe2896088840a6e7",
"meta_description": null,
"meta_title": null,
"og_description": null,
"og_image": null,
"og_title": null,
"primary_author": Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "1",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Joe Bloggs",
"profile_image": "https://example.com/super_photo.jpg",
"slug": "joe-bloggs",
"twitter": null,
"url": "http://127.0.0.1:2369/author/joe-bloggs/",
"website": null,
},
"published_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"reading_time": 1,
"slug": "not-so-short-bit-complex",
"title": "Not so short, bit complex",
"twitter_description": null,
"twitter_image": null,
"twitter_title": null,
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"url": "http://127.0.0.1:2369/not-so-short-bit-complex/",
"uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
"visibility": "public",
},
Object {
"access": true,
"authors": Array [
@ -1386,6 +1292,100 @@ Most successful subscription businesses publish a mix of free and paid posts to
"uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
"visibility": "public",
},
Object {
"access": true,
"authors": Array [
Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "1",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Joe Bloggs",
"profile_image": "https://example.com/super_photo.jpg",
"slug": "joe-bloggs",
"twitter": null,
"url": "http://127.0.0.1:2369/author/joe-bloggs/",
"website": null,
},
Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "6193c685e792de832cd08141",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Slimer McEctoplasm",
"profile_image": null,
"slug": "slimer-mcectoplasm",
"twitter": null,
"url": "http://127.0.0.1:2369/404/",
"website": null,
},
],
"canonical_url": null,
"codeinjection_foot": null,
"codeinjection_head": null,
"comment_id": "618ba1ffbe2896088840a6e7",
"comments": false,
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"custom_excerpt": null,
"custom_template": null,
"email_subject": null,
"excerpt": " * Lorem
* Aliquam
* Tortor
* Morbi
* Praesent
* Pellentesque
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
1234abcdefghijkl
Definition listConsectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim venia",
"feature_image": null,
"feature_image_alt": null,
"feature_image_caption": null,
"featured": true,
"frontmatter": null,
"html": "<!--kg-card-begin: markdown--><p><nav><ul><li><a href=\\"#nowhere\\" title=\\"Anchor URL\\">Lorem</a></li><li><a href=\\"http://127.0.0.1:2369/about#nowhere\\" title=\\"Relative URL\\">Aliquam</a></li><li><a href=\\"//somewhere.com/link#nowhere\\" title=\\"Protocol Relative URL\\">Tortor</a></li><li><a href=\\"http://somewhere.com/link?ref=127.0.0.1#nowhere\\" title=\\"Absolute URL\\">Morbi</a></li><li><a href=\\"#nowhere\\" title=\\"Praesent dapibus, neque id cursus faucibus\\">Praesent</a></li><li><a href=\\"#nowhere\\" title=\\"Pellentesque fermentum dolor\\">Pellentesque</a></li></ul></nav><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p><table><thead><tr><th>1</th><th>2</th><th>3</th><th>4</th></tr></thead><tbody><tr><td>a</td><td>b</td><td>c</td><td>d</td></tr><tr><td>e</td><td>f</td><td>g</td><td>h</td></tr><tr><td>i</td><td>j</td><td>k</td><td>l</td></tr></tbody></table><dl><dt>Definition list</dt><dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</dd><dt>Lorem ipsum dolor sit amet</dt><dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</dd></dl><ul><li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li><li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li><li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li><li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li></ul></p><!--kg-card-end: markdown-->",
"id": "618ba1ffbe2896088840a6e7",
"meta_description": null,
"meta_title": null,
"og_description": null,
"og_image": null,
"og_title": null,
"primary_author": Object {
"bio": "bio",
"cover_image": null,
"facebook": null,
"id": "1",
"location": "location",
"meta_description": null,
"meta_title": null,
"name": "Joe Bloggs",
"profile_image": "https://example.com/super_photo.jpg",
"slug": "joe-bloggs",
"twitter": null,
"url": "http://127.0.0.1:2369/author/joe-bloggs/",
"website": null,
},
"published_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"reading_time": 1,
"slug": "not-so-short-bit-complex",
"title": "Not so short, bit complex",
"twitter_description": null,
"twitter_image": null,
"twitter_title": null,
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000\\\\\\+\\\\d\\{2\\}:\\\\d\\{2\\}/,
"url": "http://127.0.0.1:2369/not-so-short-bit-complex/",
"uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
"visibility": "public",
},
Object {
"access": true,
"authors": Array [

View File

@ -159,7 +159,7 @@ describe('Posts Content API', function () {
const jsonResponse = res.body;
assert.equal(jsonResponse.posts[0].slug, 'not-so-short-bit-complex', 'The API orders by number of matched authors');
assert.equal(jsonResponse.posts[0].slug, 'welcome', 'The API orders by number of matched authors, then by published_at desc, then by id desc');
const primaryAuthors = jsonResponse.posts.map((post) => {
return post.primary_author.slug;