pretext icon indicating copy to clipboard operation
pretext copied to clipboard

Server Side Rendering (SSR) support

Open ericbf opened this issue 7 months ago • 10 comments

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?

ericbf avatar Apr 10 '26 20:04 ericbf

Done in #126

beeegee avatar Apr 12 '26 14:04 beeegee

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.

qb20nh avatar Apr 14 '26 05:04 qb20nh

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.

ericbf avatar Apr 14 '26 14:04 ericbf

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.

infodusha avatar Apr 16 '26 15:04 infodusha

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.

ericbf avatar Apr 19 '26 01:04 ericbf

Why was #126 closed?

CanRau avatar May 01 '26 10:05 CanRau

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.

nikilok avatar May 01 '26 10:05 nikilok

😂 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" 😅

CanRau avatar May 01 '26 12:05 CanRau

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

infodusha avatar May 08 '26 08:05 infodusha

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.

chenglou avatar Sep 13 '26 00:09 chenglou