feat: Allow passing data alongside routes#39
Conversation
|
Sorry, but I'm going to need an actual PR description as the title alone is not enough to go on. I'm confused as to what you're even going for here. |
|
My bad, updated the description so it's a little bit more descriptive. |
rschristian
left a comment
There was a problem hiding this comment.
Will think on it further, though I don't think this API can land as-is. It's quite clunky & surprising, IMO.
In the immediate future, you can do this much better today by just making use of a few globals, i.e., globalThis.myDataMap in vite.config.js and reading from it later in your prerender(). However, down the line, maybe this can be passed into the user-provided prerender function? Not sure.
The complex routes are objects of the form
{ url: string, data?: any }, which is somewhat confusing and in all honesty
Yeah, that's super awkward. FWIW, PrerenderedRoute is not an exposed type (I believe), it's just a stand-in given it only had information about URL data and the like.
data as a prop within that also isn't great as it clashes w/ existing patterns:
The thing with that is that you can't have multiple routes with the same path that way, you'd still have to encode that information into the path itself
Hmm, that does seem to be suboptimal, but I'm not sure what else could be used as |
Indeed, but after more thought, I'm going to say you should be encoding that information into the URL with this plugin. At least for the server-side (prerender) portion of your app, provide distinct paths for each file you want to prerender. Doubling up with a data bag & a mandatory config field to avoid overwriting is just too awkward. File paths will be the source of truth. Now, there might be a few things we can change here to better support that (query params come to mind, I don't think we support them) but they're the blessed path. |
This allows you to pass in routes with data attached to them into the
additionalPrerenderRoutesoption andlinksfield returned from the prerender script. You can then use the data in the prerender script to render different pages on the same route with encoding the data into the route itself. You can still pass in simple routes (i.e./route) alongside "complex" ones ({ url: "/route", data: "anything here" }).The complex routes are objects of the form
{ url: string, data?: any }, which is somewhat confusing and in all honesty, it would be nice to useroutesfor, well, routes in the prerender result object andlinkfor theurlfield in the route object, butlinksis already used by the result so it would get too confusingThis also adds a
resolveRouteoption to the config that allows the user to specify how a route with data should be resolved into a path that can be used by the prerender plugin, making it possible to have situations where you define the same route multiple times with different data without them conflicting with one anotherContext (from the preact slack)