mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
docs(ComponentExample): automatically read code (#789)
This commit is contained in:
27
docs/composables/useContentExamplesCode.ts
Normal file
27
docs/composables/useContentExamplesCode.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
const useContentExamplesCodeState = () => useState('content-examples-code', () => ({}))
|
||||
|
||||
export async function fetchContentExampleCode (name?: string) {
|
||||
if (!name) return
|
||||
const state = useContentExamplesCodeState()
|
||||
|
||||
if (state.value[name]?.then) {
|
||||
await state.value[name]
|
||||
return state.value[name]
|
||||
}
|
||||
if (state.value[name]) { return state.value[name] }
|
||||
|
||||
// add to nitro prerender
|
||||
if (process.server) {
|
||||
const event = useRequestEvent()
|
||||
event.node.res.setHeader(
|
||||
'x-nitro-prerender',
|
||||
[event.node.res.getHeader('x-nitro-prerender'), `/api/content-examples-code/${name}.json`].filter(Boolean).join(',')
|
||||
)
|
||||
}
|
||||
state.value[name] = $fetch(`/api/content-examples-code/${name}.json`).then((data) => {
|
||||
state.value[name] = data
|
||||
})
|
||||
|
||||
await state.value[name]
|
||||
return state.value[name]
|
||||
}
|
||||
Reference in New Issue
Block a user