首页 > 解决方案 > 如果仅在 Next.js SSG 中的服务器中使用,为什么将时刻 / i18 代码下载到客户端

问题描述

我们使用 moment 和 i18 进行翻译,但仅在服务器端。生成 HTML 时。为什么要下载到客户端?为什么它使用brandwith并增加“互动时间”?

一些代码

import { useEffect, useState } from "react";
import dynamic from "next/dynamic";
import {
  getDateString,
  getFormattedTimeInterval,
  countTotalBookings,
} from "../../Utility/Utility";
import { MainTableProps, EventTypeEnum, PricingOptionType } from "../../Types";
import { useTranslation } from "react-i18next";
import useWindowSize from "../../Utility/UseWindowSize";
import i18n from "i18next";
import accounting from "accounting-js";
import moment from "moment";
function MainTable(props: MainTableProps) {
  const {
    showOnlyStartEndTime,
    eventTimes,
    passes,
    pricingOptions,
    ratingAvg,
    minParticipants,
    maxParticipants,
  } = props.buyTicketData;
  const { t } = useTranslation("buyTicket");
  return (
    ...

                     <span>
                        {!props.dateRange[0].startDate ||
                        !props.dateRange[0].endDate
                          ? t("cdr34")
                          : moment(props.dateRange[0].startDate).format(
                              "YYYY.MM.DD"
                            ) +
                            " - " +
                            moment(props.dateRange[0].endDate).format(
                              "YYYY.MM.DD"
                            )}
                      </span>

标签: reactjsnext.jsmomentjsserver-side-renderingi18next

解决方案


如果您只想在服务器端进行计算,那么您应该使用 getServerSideProps 中的函数而不是组件的返回块


推荐阅读