File tree Expand file tree Collapse file tree 4 files changed +33
-3
lines changed
fixtures/content-layer/src/pages Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' astro ' : patch
3+ ---
4+
5+ Fixes a case where the Content Layer ` glob() ` loader would not update when renaming or deleting an entry
Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ export function glob(globOptions: GlobOptions): Loader {
101101 const existingEntry = store . get ( id ) ;
102102
103103 const digest = generateDigest ( contents ) ;
104+ const filePath = fileURLToPath ( fileUrl ) ;
104105
105106 if ( existingEntry && existingEntry . digest === digest && existingEntry . filePath ) {
106107 if ( existingEntry . deferredRender ) {
@@ -112,11 +113,10 @@ export function glob(globOptions: GlobOptions): Loader {
112113 store . addAssetImports ( existingEntry . assetImports , existingEntry . filePath ) ;
113114 }
114115
116+ fileToIdMap . set ( filePath , id ) ;
115117 return ;
116118 }
117119
118- const filePath = fileURLToPath ( fileUrl ) ;
119-
120120 const relativePath = posixRelative ( fileURLToPath ( config . root ) , filePath ) ;
121121
122122 const parsedData = await parseData ( {
Original file line number Diff line number Diff line change @@ -323,5 +323,26 @@ describe('Content Layer', () => {
323323 assert . equal ( res . status , 500 ) ;
324324 assert . ok ( text . includes ( 'RenderUndefinedEntryError' ) ) ;
325325 } ) ;
326+
327+ it ( 'update the store when a file is renamed' , async ( ) => {
328+ const rawJsonResponse = await fixture . fetch ( '/collections.json' ) ;
329+ const initialJson = devalue . parse ( await rawJsonResponse . text ( ) ) ;
330+ assert . equal ( initialJson . numbers . map ( ( e ) => e . id ) . includes ( 'src/data/glob-data/three' ) , true ) ;
331+
332+ const oldPath = new URL ( './data/glob-data/three.json' , fixture . config . srcDir ) ;
333+ const newPath = new URL ( './data/glob-data/four.json' , fixture . config . srcDir ) ;
334+
335+ await fs . rename ( oldPath , newPath ) ;
336+ await fixture . onNextDataStoreChange ( ) ;
337+
338+ try {
339+ const updatedJsonResponse = await fixture . fetch ( '/collections.json' ) ;
340+ const updated = devalue . parse ( await updatedJsonResponse . text ( ) ) ;
341+ assert . equal ( updated . numbers . map ( ( e ) => e . id ) . includes ( 'src/data/glob-data/three' ) , false ) ;
342+ assert . equal ( updated . numbers . map ( ( e ) => e . id ) . includes ( 'src/data/glob-data/four' ) , true ) ;
343+ } finally {
344+ await fs . rename ( newPath , oldPath ) ;
345+ }
346+ } ) ;
326347 } ) ;
327348} ) ;
Original file line number Diff line number Diff line change @@ -19,6 +19,9 @@ export async function GET() {
1919 const increment = await getEntry ( 'increment' , 'value' ) ;
2020
2121 const images = await getCollection ( 'images' ) ;
22+
23+ const numbers = await getCollection ( 'numbers' ) ;
24+
2225 return new Response (
2326 devalue . stringify ( {
2427 customLoader,
@@ -29,7 +32,8 @@ export async function GET() {
2932 entryWithImagePath,
3033 referencedEntry,
3134 increment,
32- images
35+ images,
36+ numbers,
3337 } )
3438 ) ;
3539}
You can’t perform that action at this time.
0 commit comments