Skip to content
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

Demo for making backend code know browser's locale and timezone #21443

Closed
wants to merge 1 commit into from

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Oct 13, 2022

A demo for

Then backend code can render contents with correct locale and timezone, there will be no UI flicker on frontend.

Try it in a private window.


Some backgrounds and discussions: #21429

In short, at the moment, backend code doesn't know user's timezone, so rendered date/time would be in wrong timezone.

There could be 2 approaches:

  1. Re-render the date/time by JS again, which may cause some flickers on UI, to avoid flicker the code would be somewhat hacky and IMO not stable (PR Replace inline script for critical JS with standalone script #21429).
  2. Make backend know the browser's timezone (This PR, for demo), which will make a second request for user's first visit. Or we can choose to only render correct timezone for later visits.

Which way should we go?

@silverwind
Copy link
Member

Hmm, this could work, but would only be active on second page load, but that's not a big issue. If we wanna go the backend route (and I would certainly prefer), we should probably reject #21440 now.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 13, 2022
@silverwind
Copy link
Member

The real question is thought: How does go's date formatting hold up to JS Intl? Is it "good enough"?

@wxiaoguang
Copy link
Contributor Author

The real question is thought: How does go's date formatting hold up to JS Intl? Is it "good enough"?

It is something still needs to be investigated, and I think we can wait for more time to collect more voices from other maintainers about the locale/date format problem.

@silverwind
Copy link
Member

silverwind commented Oct 13, 2022

Yes, we need a decision between this PR and #21429 (comment).

This PR:

  • does not work on first page load or for users who block cookies
  • would need to rely on golang intl which may be "weaker" than JS intl

My PR:

  • has non-straightforward initialization via requestAnimationFrame
  • requires one additional fetch to the server for a tiny script

if (window.config.pageData.browserSyncJson != null && window.config.pageData.browserSyncJson !== browserSyncJson) {
document.cookie = '_gitea_bsj=' + encodeURIComponent(browserSyncJson) + '; path=/'; // tell the backend about locale and timezone
document.body.style.setProperty('display', 'none', 'important'); // hide the page to prevent flash during reload
setTimeout(() => window.location.reload(), 1000); // use a setTimeout to simulate a slow network
Copy link
Member

@silverwind silverwind Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not do this reload, just live with the fact that it works only on second page load. There is certainly the danger of a reload loop when something goes wrong otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I could also agree so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ps: cookie won't be a problem, the CSRF token and Gitea session cookie are all there, unless only the document.cookie doesn't work ....

@@ -47,3 +47,12 @@ If you introduce mistakes in it, Gitea JavaScript code wouldn't run correctly.
{{/* in case some pages don't render the pageData, we make sure it is an object to prevent null access */}}
window.config.pageData = window.config.pageData || {};
</script>

<script type="module">
const browserSyncJson = JSON.stringify({lc: window.navigator.language, tz: new Date().getTimezoneOffset()});
Copy link
Member

@silverwind silverwind Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to communicate window.navigator.language, as we already have the language from the chosen language setting (reflected in document.documentElement.lang).

Copy link
Contributor Author

@wxiaoguang wxiaoguang Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's another question needs to be discussed. window.navigator.language is user browser's default language, which is different from the user chosen language for Gitea document.documentElement.lang.

So I just report the navigator language as it is, then backend can know: browser's default language, user chosen language.


Indeed the navigator.language could be removed if it's useless in the future.

Copy link
Member

@silverwind silverwind Oct 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only place we should rely on window.navigator.language is to determine initial language for users that have set none, e.g. a auto language could be added that defaults to window.navigator.language, similar to the auto theme.

@silverwind
Copy link
Member

silverwind commented Oct 14, 2022

My opinion: I still prefer #21429 because JS Intl APIs are more powerful and because it works on first page load too. And secondly, because I'm more fluent in JS than golang.

@wxiaoguang
Copy link
Contributor Author

I could also agree with #21429 if there would be a stable solution for the case:

<script init.js />

<time id=time1 />

<!-- 
very long content, which need a few seconds to transfer, 
then browser will partially render the content above,
while the remaining content will be rendered a few seconds later.
 -->

<time id=time2 />

<script index.js />

@silverwind
Copy link
Member

I'm sure there is a solution, but it can't be doing requestAnimationFrame indefinitely, some way to exit the recursion must be there (at worst, it could run into the call stack limit of JS).

@wxiaoguang wxiaoguang closed this Oct 30, 2022
@wxiaoguang wxiaoguang deleted the backend-locale-timezone branch November 5, 2022 08:51
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants