Programmatic site generation #118
-
Hey @ElMassimo, Thanks for your work and such an awesome project! I saw Astro a while ago and thought it was great but wanted it in Vue land so this is incredible that you've made it happen. I'm not sure if the title is right but I thought I'd ask anyway. In our project we build sites by loading some JSON which has all the content and meta data for the pages, and crucially it enables/disables the pages visibility. An example structure...
We have a few issues I was hoping you might be able to help with please :)
Hope that makes sense. Thanks again, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi Giles! I'll provide a quick answer now, if you share a repo with an example I can be more specific.
Although A great place to do that is const data = await fetchSiteJSON()
export default {
title: data.name, // Used by default to render meta tags and page titles
...data,
} This data would be available as To avoid fetching many times during dev, I'd recommend caching in local storage in an SSR friendly way: const siteJSON = await withCache('siteJSON', fetchSiteJSON) This would ensure that if you are fetching from a remote API service, you only need to wait for the request the first time, and the example utility I linked provides a If you want specific pages to access this data in an idiomatic way, you could instead fetch it in extendFrontmatter (frontmatter, filename) {
const dataForPage = extractFromSiteJSON(filename) // a function you can define or import in `iles.config.ts`
Object.assign(frontmatter, dataForPage)
}, This will be run only in Node.js (not the browser), so you can use a simpler in-memory caching strategy during development.
If you plan to define pages programmatically, a good place to "drive" the site is to use extendRoutes (routes) {
return routes.filter(shouldRouteBeRendered) // a function you can define or import in `iles.config.ts`
}, The hook receives the routes inferred from Since that hook will only be called once during build, it would only require a single API call based on what you mentioned. |
Beta Was this translation helpful? Give feedback.
-
Hey @ElMassimo, Thank you for getting back to me and for the detailed response!! It's been super helpful so far and pretty much got us where we need to be! I'll update the thread if anything else crops up. Looking forward to seeing how the project progresses. Thanks again, |
Beta Was this translation helpful? Give feedback.
Hi Giles! I'll provide a quick answer now, if you share a repo with an example I can be more specific.
Although
enhanceApp
will be called every time the Vue app is created (standard practice when doing SSR is to create a fresh app for each render), you can leverage ES modules to ensure you fetch the data outside the hook, ensuring it runs only once.A great place to do that is
src/site.ts
:This data would be available as
$site
in templates, ors…