router icon indicating copy to clipboard operation
router copied to clipboard

Similar to how `route.options.meta()` already behaves it would be nice to pass the `loaderData` and `match` to the `links()` function as well.

Open SeanCassiere opened this issue 1 year ago • 7 comments

Similar to how route.options.meta() already behaves it would be nice to pass the loaderData and match to the links() function as well.

For my specific use-case I want to generate a canonical link element that is rendered via the Meta component inside the head. To do this I need some data from the current match and loaderData. Those are available inside meta() but not inside links().

Originally posted by @lo1tuma in https://github.com/TanStack/router/discussions/2419

SeanCassiere avatar Sep 27 '24 05:09 SeanCassiere

It could be worth exploring @lo1tuma suggestion here.

The only consideration I see here is that we'd now be expecting links() to be executed only after the loader has completed. Therefore, you wouldn't be able to resolve links() into the <head> before the loader has completed its work.

SeanCassiere avatar Sep 27 '24 05:09 SeanCassiere

@SeanCassiere As a suggestion / feature request. Would it be possible to replace meta or add a head property that has the same signature as the meta function and allows for injecting meta, link and script tags in the head?

head: ({ loaderData }) => ({
    meta: [ ... ],
    links: [ ... ],
    scripts: [ ...],
),

rehos avatar Oct 03 '24 13:10 rehos

@tannerlinsley @schiller-manuel what do you think about this API change?

Also, should we maybe make loaderData be LoaderData | undefined? That way we can execute this function and allow users who don't rely on loaderData to inject the meta, links, and scripts without having to wait.

SeanCassiere avatar Oct 04 '24 03:10 SeanCassiere

I like the API suggestion to group under head.

I don't understand the loaderData part. Would we not just invoke this function with the matches?

schiller-manuel avatar Oct 04 '24 08:10 schiller-manuel

We'll be using the suggestion by @rehos. I'll look at implementation once I get some time this week. Edit: This'll be a breaking change for Start. We'll be pushing it through since it's in ALPHA.

SeanCassiere avatar Oct 04 '24 22:10 SeanCassiere

While migrating a site to @tanstack/start I stumbled on an additional requirement. To prevent CLS we already load a part of the CSS in the head in a style tag. It would be nice if the head function also supports it. I would expect the tags are output in the order meta-tags, style-tags, link-tags and script-tags.

head: ( ... ) => ({
    meta: [ ... ],
    styles: [ ... ],
    links: [ ... ],
    scripts: [ ...],
),

rehos avatar Oct 08 '24 15:10 rehos

@rehos I think I get what you mean. That probably won't go with this fix, but afterwards instead.

There's a question on the API here regarding how that works which would need a bit of addressing, but I'm not against this feature. Once the fix goes through, we can propose the idea of Tanner and see what he thinks.

SeanCassiere avatar Oct 08 '24 21:10 SeanCassiere