Server Side Rendering (SSR) support
Hello there! We were trying to use pretext to measure text in a server component, but there doesn’t seem to be a straightforward way to do that? We’re using NextJS, have canvas installed, and just wanted to get some measurements done on the server side for pre-rendering.
Is there a straightforward way to measure text in the SSR context of a NextJS (or any SSR) application?
Done in #126
Sorry if I'm missing the point entirely, but pretext already has to do a fair amount of nontrivial work to align canvas and DOM metrics within a single browser environment. Moving that measurement to a different OS, runtime, text stack, and canvas implementation seems to make that alignment problem materially harder.
Sorry if I'm missing the point entirely, but pretext already has to do a fair amount of nontrivial work to align canvas and DOM metrics within a single browser environment. Moving that measurement to a different OS, runtime, text stack, and canvas implementation seems to make that alignment problem materially harder.
The point is pretty straightforward – to get a measurement of text during pre-rendering to prevent layout shift on the frontend. Never said it was going to be simple. It may be a matter of a "best guess" situation and then tweaks on mount on the client side.
One of the solutions could be not to have canvas on the server:
Instead think of general prepare for all the alphabet on the client at development - time, then the result is saved and loaded to use later
That may require significant changes, but would allow this package to have precomuted things and math would work both on the client and server anyway the same way.
If we can make a best guess based on user agent or something, the idea is to be able to get a reasonable measurement on the server side.
Why was #126 closed?
Why should this be a problem PreText solve ? Shouldn't this problem be solved by the different server rendering libraries either via a middleware or something similar? The canvas APIs calculation is already very browser specific so using the user agent to spin up that environment on the server sounds like a whole new Pandora's box that should potentially be a never ending change. When browser versions change, server rendering that the right canvas sounds like a headless browser branch. I'll use headless-Safari-X for user A and headless-Safari-Y for user B. On the client you have none of these issues.
😂 you're right, I just fell into the "Gimme SSR" pit, without really thinking it through 😅
Gonna try it out now 🤓
❤️
Update: Love it so far, we took a progressive-enhancement approach and calculate dynamically only when the relevant part is about to enter the viewport. Not yet tested at larger text, but we'll see 🙌🏼
So depending on plans, readme might need an update "Allows rendering to DOM, Canvas, SVG and soon, server-side." Which was also why I "fell into that pit" 😅
So following up my own thought above I kinda built my own solution: prefont
It is not a replacement of pretext since my version is only about text width as it was my use-case;
However, I find it a good demo of a 'server' mode that pretext can get
Not supported today. Without OffscreenCanvas or a document, prepare() throws. Server-side is still an open question for me: a server canvas uses a different font stack, so its widths won't match a real browser, and Pretext's rules depend on which browser the text is shown in. Measuring on the client just before content scrolls into view, as mentioned above, is the right approach for now. Keeping this open while I decide.