Skip to content

feat: Allow passing data alongside routes#39

Open
Delta-official wants to merge 4 commits into
preactjs:masterfrom
Delta-official:complex-routes
Open

feat: Allow passing data alongside routes#39
Delta-official wants to merge 4 commits into
preactjs:masterfrom
Delta-official:complex-routes

Conversation

@Delta-official
Copy link
Copy Markdown

@Delta-official Delta-official commented May 9, 2026

This allows you to pass in routes with data attached to them into the additionalPrerenderRoutes option and links field 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 use routes for, well, routes in the prerender result object and link for the url field in the route object, but links is already used by the result so it would get too confusing

This also adds a resolveRoute option 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 another

Context (from the preact slack)

@rschristian
Copy link
Copy Markdown
Member

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.

@Delta-official
Copy link
Copy Markdown
Author

My bad, updated the description so it's a little bit more descriptive.

Copy link
Copy Markdown
Member

@rschristian rschristian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

https://github.com/preactjs/create-preact/blob/2786288347a03628dd13f426e2b221f1f1e4b5e8/templates/config/prerender/src/index.jsx#L47-L49

Comment thread src/plugins/prerender-plugin.js Outdated
@Delta-official
Copy link
Copy Markdown
Author

, i.e., globalThis.myDataMap in vite.config.js and reading from it later in your prerender()

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

data as a prop within that also isn't great as it clashes w/ existing patterns:

Hmm, that does seem to be suboptimal, but I'm not sure what else could be used as data fits the field pretty well. Perhaps routes could be allowed to have arbitrary fields that are passed in as-is, but I fear that might conflict with the internals of the plugin or lead to unexpected behaviour when the user defines a field like _discoveredBy which then gets overwritten by the plugin

@rschristian
Copy link
Copy Markdown
Member

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants