yew icon indicating copy to clipboard operation
yew copied to clipboard

Cannot render <head> with SSR

Open ranile opened this issue 2 years ago • 4 comments

Background

One of the big reasons to use server side rendering is to make the site accessible to crawlers and bots, which often look at the the metadata in <head> to determine the site's contents. It is crucial to have the <head> fully rendered when HTML is returned on first render

Problem

Right now, there's no (ergonomic) way to render the <head>. Our examples only show how to render the body. Trunk also interferes because index.html is generated by it and not us.

Possible Solution

Trunk generates the artifacts like:

dist
├── index-8b7906476da3648f.css
├── index.html
├── ssr_router_hydrate-bb65b3ba22cdbb94.js
└── ssr_router_hydrate-bb65b3ba22cdbb94_bg.wasm

The server would:

  1. Read the index.html
  2. Modify the head to keep all the link tags and such (for assets) and add all the elements generated from the ServerRenderer
  3. Send the updated HTML

Drawbacks

We can't stream the response. We would need the server to obtain the entirety of HTML from ServerRenderer, parse it, take the <head> from it and modify the trunk generated index.html accordingly.

Additional Requirement

It should be possible to set the <head> from anywhere in the component tree, in a vein similar to next/head

ranile avatar Jul 02 '22 19:07 ranile

There is already solutions like Bounce Helmet that provides a similar functionality (No SSR support yet).

I can donate this implementation to Yew.

However, this implementation requires a state management logic to deduplicate / merge head state and diffing rendered elements.

  • If we do not provide the state management logic to public, we have some code that can be useful to users kept for ourselves.
  • If we provide it to public, then it will become the official state management library which will hinder any third party state management libraries (yewtil::store).

Personally, I do not see an issue with this remaining as a third party solution at the moment.

futursolo avatar Jul 03 '22 04:07 futursolo

I don't mind it staying a 3rd party library but I would like it if the ssr_router example and docs described how to set <head>. It's an important piece that is currently missing from Yew

ranile avatar Jul 03 '22 11:07 ranile

This would definitely be nice to have, has any progress been made on this addition so far or does it need someone to take the lead?

Jaffa-Cakes avatar Apr 14 '23 11:04 Jaffa-Cakes

I think Bounce Helmet already supports rendering head with SSR?

futursolo avatar Apr 14 '23 11:04 futursolo