Skip to content
This repository was archived by the owner on Sep 24, 2025. It is now read-only.

Commit 5280259

Browse files
authored
fix: create files path map to populate document driven pages (#160)
1 parent 0357fb0 commit 5280259

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/runtime/composables/useStudio.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const defu = createDefu((obj, key, value) => {
2222
export const useStudio = () => {
2323
const nuxtApp = useNuxtApp()
2424
const { studio: studioConfig, content: contentConfig } = useRuntimeConfig().public
25+
const contentPathMap = {} as Record<string, ParsedContent>
2526

2627
// App config (required)
2728
const initialAppConfig = useDefaultAppConfig()
@@ -52,7 +53,10 @@ export const useStudio = () => {
5253

5354
// Handle content files
5455
await Promise.all(
55-
files.map(item => contentStorage.setItem(`${previewToken}:${item.parsed!._id}`, JSON.stringify(item.parsed)))
56+
files.map((item) => {
57+
contentPathMap[item.parsed!._path!] = item.parsed!
58+
return contentStorage.setItem(`${previewToken}:${item.parsed!._id}`, JSON.stringify(item.parsed))
59+
})
5660
)
5761
}
5862

@@ -167,19 +171,35 @@ export const useStudio = () => {
167171
if (!content) {
168172
content = content = await storage.value?.getItem(path)
169173
}
174+
175+
// try finding content from contentPathMap
176+
if (!content) {
177+
content = contentPathMap[path || '/']
178+
}
179+
170180
return content as ParsedContent
171181
}
172182

173183
const updateContent = (content: PreviewFile) => {
174184
const previewToken = window.sessionStorage.getItem('previewToken')
175185
if (!storage.value) { return }
176186

187+
contentPathMap[content.parsed!._path!] = content.parsed!
177188
storage.value.setItem(`${previewToken}:${content.parsed?._id}`, JSON.stringify(content.parsed))
178189
}
179190

180191
const removeContentWithId = async (path: string) => {
181192
const previewToken = window.sessionStorage.getItem('previewToken')
193+
const content = await findContentWithId(path)
182194
await storage.value?.removeItem(`${previewToken}:${path}`)
195+
196+
if (content) {
197+
delete contentPathMap[content._path!]
198+
const nonDraftContent = await findContentWithId(content._id)
199+
if (nonDraftContent) {
200+
contentPathMap[nonDraftContent._path!] = nonDraftContent
201+
}
202+
}
183203
}
184204

185205
const requestRerender = async () => {
@@ -188,11 +208,7 @@ export const useStudio = () => {
188208
const { pages } = callWithNuxt<any>(nuxtApp, useContentState)
189209

190210
const contents = await Promise.all(Object.keys(pages.value).map(async (key) => {
191-
if (!pages.value[key]) {
192-
return null
193-
}
194-
195-
return await findContentWithId(pages.value[key]._id)
211+
return await findContentWithId(pages.value[key]?._id ?? key)
196212
}))
197213

198214
pages.value = contents.reduce((acc, item, index) => {

0 commit comments

Comments
 (0)