NEXT JS SSR LOADING TOO SLOW

Viewed 906

I'm using next js to call API.If i call API on SSR the page loading too slow.but on CSR it works normally

Please help me. MY code on SSR :

        function Homepage(props) {
          return (
            <Layout>
              <Home {...props} />
            </Layout>
          )
        }

        async function getSubject(ctx) {
          const data = {
            ctx,
            serviceCode: 1,
          };
          const res = await NewsService.getSubject(data);
          return res?.errorCode === 0 ? res.result : [];
        }

        Homepage.getInitialProps = async (ctx) => {
          const allList = await getSubject(ctx);
          const freeList = allList.filter(item => item.code === "S01")[0]?.contents || [];
          return {
            freeList,
            namespacesRequired: ['common'],
          }
          return {};
        }

My Code on CSR:

          useEffect(() => {
            getSubject();
          }, [])

          async function getSubject(ctx) {
            const data = {
              ctx,
              serviceCode: 1,
              pageSize: 3,
            };
            const res = await NewsService.getSubject(data);
            if (res?.errorCode === 0) {
              transformData(res.result) // just to filter result
            }
          }

Delay 5s

Now, my code is in build mode. Follow me:I have an example link: "http://khiem.com". If access this then access "http://khiem.com/slug". it's too fast. But if direct access "http://khiem.com/slug". it's delay :((

0 Answers
Related