-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add default time for time element #5212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Any ideas on how to handle or if we want to handle when server and client are in different time-zones. Here is the current behaviour. If when they both are in different time-zones. Should we take into consideration of the clients time-zone in server during SSR process ? Because, on canvas we use clients timezone. On server it is server based. Again on client side from deployed site. It shows the client side timezone with a flicker. The hydration issue with server and client time-zone is quite evident on deployed URL. |
another option as opposed to suppressing and potentially rendering a different date could be reading the date from the dom and using it to construct the new Date object |
Yes doing the same once the component is fully hydrated. But still there will be a flicker from the server side as we render on server with 0 seconds or any seconds for that matter as they change. I think the big bug would be with the flicker with timezones between server and client. |
Can't you set the seconds/ms/tz to a single value when its the default value so that its always matching and then read it? |
If there is no default by user. Component is using Date.now() inside server. So, basically it picks the server timezone. https://github.com/webstudio-is/webstudio/pull/5212/files Looks like there are ways to avoid such behaviours remix-run/remix#9039 (comment) Will try few things out and update the PR tonight 👍🏽 |
I am not sure what you mean, but can you avoid creating a new date on the client entirely and use the server-rendered date fully? e.g. supply the original date in an attribute because text is formatted so it may not have parts of the date |
We can, but the catch is while server rendering it. It needs to be aware of where the client is located in. As the SSR uses the server time. So, if both client and server are in entirely different timezones. You will only see server time and miss the client timezone if not rendered again on client side. One easy way to do this is marking the component as client component. But we will loose ssr benefit for it. |
Let it user server time, use the same time on the client |
Making it pure client-side rendered date could be an option too, I don't know if SSR is actually important here, but if its used in the hero section, it will jump around |
Updated the component now with the default time behaviour. This will no reflect the current time This the current behaviour from the The only side effect is, this will currently throws an error with The only other options is
|
Why can't we use server-rendered date including everything, including timezone? |
If there is a tz provided by user - it should be used, otherwise a timezone on the server should be used for both server and client-side, I still don't see the problem |
@kof i thought of that too. But, it still doesn't cover all the cases. For example, If users don't provide any time related data in the string like So, if we are ok with hydration errors in console. It's pretty much straight forward. It's only a issue when we want to avoiding those hydration error messages 😅 That's why i am not leaned towards taking |
If something isn't provided, use the default server-side, then read it from html on the client |
Yes yes, i am ok with that. But it will produce hydration error on console logs. As the date changes. Is that fine, that's what i wanted to know |
Why is it going to cause a hydration error if you are reading the date string from the dom and your client doesn't try to render anything different? |
Like if users pick full time, there is a change is seconds. And currently without any third party lib or regex approach we can't pick tz from a string. Because the inbuilt getTimezoneOffset always returns w.r.t to env and not the string. So need to come up with some custom solution. Will give a shot and update PR. Got only some time, so taking long on PR. Apologies for that. |
Parsing a time string including timezone can't be a big deal. Please don't write a regex for that. Find a tested solution. |
@kof i tried a couple of npm libraries, looks like almost all of them parses the timezone from string based on the execution environment. Means, they always extracting timezone w.r.t to UTC of the env and not just the timezone that is in the string. So, when we re-construct the time string back. It doesn't reflect the browser time again. Should we just mark this as client component / stick with the error message after the hydration ? We can use |
Weird! In that case either just client-only rendering and accepting the potential visible rerender on hydration or reducing this thing to just a year and month so that timezone doesn't matter |
I quickly asked gpt, it suggested https://github.com/taylorhakes/fecha/blob/master/src/fecha.ts |
Another one it suggested is https://github.com/iamkun/dayjs |
First one is regex heavy and days standalone doesn’t help much. It can to some extent with days extended something like that. That’s like multiple packages we are pulling again. Will try multiple options tomorrow. There is one more luxon. But didn’t work as expected. |
Regex or not if its well tested I guess it can work? (by no RegExp I meant no custom made regex which will certainly have bugs, but something heavily tested is ok on my watch) |
Yup, will check package sizes etc tomorrow |
@kof i gave multiple shots at this. Even with different approaches. Almost most of them get effected with the browser or the server env. The fast easy way could be to have a fixed height to reduce the jump if we mark this as client component. |
@JayaKrishnaNamburu that's not going to work, if this issue is too hard, pls don't waste your time. |
Ok, feel free to close this one. I will start looking into other issues that are on me 👍🏽 |
fixes #3510
Description
Adding a new default
new Date.now()
support to the time component. So, users can just drop and use the component whenever they want to show the current time. Rendering the default time using seconds as0
on the server side. And then replacing the value on the client side. Things that need to be taken care ofnew Date()
and respect all the configs on the component.Steps for reproduction
Currently there is a small flicker for seconds when the project is hydrated since we render with

0
seconds on the server side and replace with proper seconds on the client side. The flicker is expected IMO, as we are resetting and using it for render for space concerns.This is the project on staging
https://p-5212e8bc-eb36-4bee-9218-77885cf13e01-dot-default-time.staging.webstudio.is/?authToken=3fd8ae81-9c0a-4874-a5eb-59854dbfea86&mode=preview
Deployed URL
https://jk-time-element-ck8hm.wstd.work/
Code Review
Before requesting a review
Before merging
.env
file