swyxio / ssg

Svelte Site Generator. why try this? because sapper has a lot of setup, and isn't great at pipelining data:
https://www.swyx.io/writing/bad-ways-to-get-data/
MIT License
163 stars 7 forks source link

[feature] programmatic page creation #19

Open m4rrc0 opened 4 years ago

m4rrc0 commented 4 years ago

So I've spent a little bit of time trying to see if I could make ssg work like Gatsby for page creation (with the createPage node API hook).

My approach is currently

  1. Fetch data from my CMS in createIndex and cache it
  2. Use this.ssgData in the preload function of my [slug].svelte component

The main problem with this approach is that the route matching is dependent on file structure. My [slug].svelte component can only match routes at level 1. If I want sub-pages I need to create a component at [one]/[two].svelte. The problem is I would like to be able to create an indefinite number of nested routes on my external CMS without having to worry about how many sub-folders I created in the source code.

Another problem (arguably) that is obvious with the way Sapper create files on export is it only creates files for routes that can be crawled from the index page. We cannot create 'hidden' pages or landing pages for example.

Is it something you are interested to handle with ssg @sw-yx ? For now, ssg seems to be focused on having your data along with your source code. If we want to enable programmable page creation it seems we are going to diverge from Sapper's core principles in major ways.

m4rrc0 commented 4 years ago

About the second problem, I just discovered the (new?) --entry option that I think allows us to specify entry points for our app to be crawled from. If we implement programmatic routes creation, we could use this option to specify all created routes...

swyxio commented 4 years ago

hey! yes. my intention is to depart from Sapper sooner or later. Quite frankly i have been dragging my feet because it will be a lot of work. but yeah a programmatic createPages api makes sense.

if you have a way to implement it in this project and need it to get your stuff done, be my guest. i can't promise i'll get to the ssg rewrite soon since it is just a hobby project

m4rrc0 commented 4 years ago

Sure! I understand. To be honest I wouldn't know where to begin to do that with Sapper. I have a feeling that a new svelte ssg implementation might be around the corner with your recent experiments and the guys active on the svelvet projet (for reference). If I get to the point where I really need this, I think I will just hack around it with nested folders like I mention above for now.

swyxio commented 4 years ago

yes, exactly. too early to do this now. it would actually NOT be that hard, if you read the Sapper source code you can figure out where to insert it but it takes time and energy haha. lets just make sure we have this api when we do the rewrite