mirror of
https://github.com/withastro/astro.git
synced 2025-03-24 23:21:57 -05:00
Fix getCollection return new instance in prod (#8022)
This commit is contained in:
parent
86bee28121
commit
c23377caaf
4 changed files with 45 additions and 1 deletions
5
.changeset/smart-nails-push.md
Normal file
5
.changeset/smart-nails-push.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Always return a new array instance from `getCollection` in prod
|
|
@ -69,7 +69,8 @@ export function createGetCollection({
|
||||||
// Cache `getCollection()` calls in production only
|
// Cache `getCollection()` calls in production only
|
||||||
// prevents stale cache in development
|
// prevents stale cache in development
|
||||||
if (import.meta.env.PROD && cacheEntriesByCollection.has(collection)) {
|
if (import.meta.env.PROD && cacheEntriesByCollection.has(collection)) {
|
||||||
entries = cacheEntriesByCollection.get(collection)!;
|
// Always return a new instance so consumers can safely mutate it
|
||||||
|
entries = [...cacheEntriesByCollection.get(collection)!]
|
||||||
} else {
|
} else {
|
||||||
entries = await Promise.all(
|
entries = await Promise.all(
|
||||||
lazyImports.map(async (lazyImport) => {
|
lazyImports.map(async (lazyImport) => {
|
||||||
|
|
|
@ -168,6 +168,22 @@ describe('Content Collections - render()', () => {
|
||||||
expect(h2).to.have.a.lengthOf(1);
|
expect(h2).to.have.a.lengthOf(1);
|
||||||
expect(h2.attr('data-components-export-applied')).to.equal('true');
|
expect(h2.attr('data-components-export-applied')).to.equal('true');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('getCollection should return new instances of the array to be mutated safely', async () => {
|
||||||
|
const app = await fixture.loadTestAdapterApp();
|
||||||
|
|
||||||
|
let request = new Request('http://example.com/sort-blog-collection');
|
||||||
|
let response = await app.render(request);
|
||||||
|
let html = await response.text();
|
||||||
|
let $ = cheerio.load(html);
|
||||||
|
expect($('li').first().text()).to.equal('With Layout Prop');
|
||||||
|
|
||||||
|
request = new Request('http://example.com/');
|
||||||
|
response = await app.render(request);
|
||||||
|
html = await response.text();
|
||||||
|
$ = cheerio.load(html);
|
||||||
|
expect($('li').first().text()).to.equal('Hello world');
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Dev - SSG', () => {
|
describe('Dev - SSG', () => {
|
||||||
|
|
22
packages/astro/test/fixtures/content/src/pages/sort-blog-collection.astro
vendored
Normal file
22
packages/astro/test/fixtures/content/src/pages/sort-blog-collection.astro
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
---
|
||||||
|
import { getCollection } from 'astro:content';
|
||||||
|
|
||||||
|
const blog = await getCollection('blog');
|
||||||
|
|
||||||
|
// Sort descending by title, make sure mutating `blog` doesn't mutate other pages that call `getCollection` too
|
||||||
|
blog.sort((a, b) => a.data.title < b.data.title ? 1 : -1)
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Index</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Blog Posts</h1>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
{blog.map(post => (
|
||||||
|
<li>{ post.data.title }</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Reference in a new issue