#Tryin to using getStaticPaths only once

36 messages · Page 1 of 1 (latest)

arctic hull
#

On my, what seems to be a neverending, journey to create this Astro directory listing, I was wondering the following.

I create a directory called /pages/companies/ and inside I have a [...page].astro which is my archive page where I list all companies. I'm using the getStaticPaths() function here.

Now I want to create pages for each company, so I created [slug].astro, but is it really necessary to generate the routes again with the same function? Can't it 'inherit' the routes I already set in ...page.astro?

The routes are generated based on rows in a supabase DB, so I prefer not to have to make the query twice everytime I build. (using static output)

What's an efficient solution for this?

jagged tusk
#

You're concerned about making 2 calls to a DB when you do a build?

You might be able to create a static endpoint that returns your data as json and fetch that from both routes. Not something I've tried.

strange parcel
#

Why do you have a ...page and a slug?

jagged tusk
#

[...page].astro for a list of companies and [slug].astro for each individual company page.

strange parcel
#

Could you just use a single slug and conditionally render the one page with the list of companies differently?

jagged tusk
#

Yeah I guess, you could just have [...slug].astro, where if slug is undefined you render the list.
Unless you want pagination...

strange parcel
#

I haven't messed with the pagination yet (wish it worked in SSR) so I definitely don't know how it would effect that haha

arctic hull
#

Yeah I need pagination for sure

arctic hull
arctic hull
#

Is it a good practice to fetch from a DB, then store in a json file and use that for the pages?

#

There's no sensitive info in them

arctic hull
#

also wondering if there are any advantages of using a content collection instead of just dynamically generated pages

#

I can define the collection once, based on the rows in the DB. And then use getCollection/getEntry etc to get the content

jagged tusk
#

You could also look at the new Content Layer (currently experimental) coming in Astro 5. You can define a collection based on your DB.

arctic hull
#

oh nice

#

kapa.ai said content collections are better suited for files

#

and dynamic pages for remote content

jagged tusk
arctic hull
#

oof

meager olive
jagged tusk
#

If you're using SSR or the new Content Layer to access data directly from your DB, then your site will always be up to date.

arctic hull
#

yep, reading that right now

#

yeah I've been going back and forth on static/server

#

but there's also the build caching for content collections: experimental.contentCollectionCache

#

and incremental building (you sent me a link a few weeks back I think)

#

so if a company is added or edited, I should be able to just build the changes

#

(I hope)

jagged tusk
#

From the article:

Astro components and pages can then use getCollection or getEntry to query the data. The data is immutable at that point, so the pages are all querying the same snapshot, compiled at build time. This applies whether the page is being prerendered at build time, or server rendered on-demand.
so you would only have to query your DB once to build the collection, then use that collection.

arctic hull
#

Right, but when a new company registers, I have to rebuild. Same when a company edits their details, like address

#

Isn't that solved by ISR?

#

meh, it's not really an issue to worry about right now. Site hasn't even launched!

jagged tusk
#

If companies are self registering/editing, then I'd go SSR. Then you only need to build when you make changes.

arctic hull
#

so entire site static, except for the company pages then?

#

I'll disable prerending on the [slug].astro file

#

or I could use a server island in Astro 5

#

I don't want to be that guy, but is there an ETA for v5 yet?