简体   繁体   English

在服务器和客户端上渲染时应该如何处理日期字符串?

[英]How should date strings be handled when rendering on both server and client?

I am creating a blog using NextJS and luxon .我正在使用 NextJS 和luxon创建一个博客。

  1. I want to include the post creation date in HTML when a post is rendered by the server, so that the Google crawler has that information.当服务器呈现帖子时,我想在 HTML 中包含帖子创建日期,以便 Google 抓取工具拥有该信息。
  2. I also want the creation date to display in a suitable format for the end user.我还希望创建日期以适合最终用户的格式显示。 I am using the luxon package which formats to the user's timezone and locale automatically我正在使用luxon package ,它会自动格式化为用户的时区和语言环境

The server does not know where the end user's request is coming from - I don't want to format the date to the user's locale during SSR服务器不知道最终用户的请求来自哪里——我不想在 SSR 期间将日期格式化为用户的语言环境

...but if there is a difference between the server rendered content and the client rendered content, I get a react-hydration-error ...但是如果服务器呈现的内容和客户端呈现的内容之间存在差异,我会收到react-hydration-error

What is the recommended approach for rendering date strings on both server and client?在服务器和客户端上呈现日期字符串的推荐方法是什么?

You can format the date the server and pass it to the client-side as a prop, then format it again to your desired format using luxon.您可以格式化服务器的日期并将其作为道具传递给客户端,然后使用 luxon 将其再次格式化为您想要的格式。 With this method, you can use the formatted date in your HTML for the Google crawler as well as the user, without any difference in the information received by either.使用此方法,您可以将 HTML 中的格式化日期用于 Google 爬虫和用户,两者收到的信息没有任何差异。 This should also avoid the hydration error.这也应该避免水合作用错误。

Here is an example:这是一个例子:

// server-side:
import { DateTime } from 'luxon';

export async function getServerSideProps() {
  const creationDate = DateTime.utc().toISO();

  return {
    props: {
      creationDate,
    },
  };
}

// client-side:
import { DateTime } from 'luxon';

const Post = ({ creationDate }) => {
  const formattedCreationDate = DateTime.fromISO(creationDate)
    .setLocale('en-US')
    .toLocaleString(DateTime.DATE_FULL);

  return (
    <>
      <p>{formattedCreationDate}</p>
    </>
  );
};

export default Post;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在服务器端和客户端渲染之后如何操作 DOM? - How to manipulate DOM both after server-side and client-side rendering? 客户端和服务器端呈现的模板语言 - Templating language for both client-side and server-side rendering 使用nodejs使用服务器和客户端渲染 - using both server and client side rendering using nodejs 即使两个服务器/客户端都在相同的时区,日期输入提要也是错误的 - Date entry feed is wrong even when both Server/Client are in same timezone 我应该如何将“ es6 import”与来自客户端服务器或两者的高级方法Meteor软件包一起使用? - how should I used “es6 import” with advanced methods Meteor package from client server or both? 如何使用服务器端代码和客户端代码编写node.js Web应用程序? - How should I go about writing a node.js web application with both server and client side code? 当客户端和服务器都为本地主机时会发生CORS? - CORS when both client & server are localhost? 使用redux时,如何在反应组件中处理取消订阅? - How should unsubscribe be handled in a react component when using redux? 当客户端和服务器位置均为动态时,如何通过TLS运行c#websockets? - How to run c# websockets over TLS when both client and server location are dynamic? 结果到达客户端时,Next JS 服务器端渲染如何工作? - How does Next JS server side rendering works when result hits client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM