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

Methods of router will be updated after each top to down rerendering #27689

Closed
yangmingshan opened this issue Aug 2, 2021 · 3 comments
Closed
Labels
bug Issue was opened via the bug report template.

Comments

@yangmingshan
Copy link

yangmingshan commented Aug 2, 2021

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

16.6.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Nginx

Describe the Bug

The problem

Let's say I have an order list page, it accept a optional parameter page. I want fetch the list in the useEffect, and jump to the error page if the request fails. The codes should be like this:

// pages/orders.js
import { useState, useEffect } from "react";
import { useRouter } from "next/router";
import { getOrderList } from "../api";

export default function Orders() {
  const { push } = useRouter();
  const [orders, setOrders] = useState([]);

  useEffect(() => {
    const page = new URLSearchParams(location.search).get("page") || "1";
    getOrderList(page).then(
      (response) => {
        setOrders(response);
      },
      () => {
        push("/error");
      }
    );
  }, [push]);

  // ...
}

The problem is when I visit /orders?page=2, the order list api will be requested twice, if the request did not fail.

How this happened

The effect will run after the first rendering, and cause the first request, not surprising. But next.js will call router.replace() after the first rendering, it will cause a top to down rerendering. The router will be updated after each AppContainer rerendering, so are it's properties and methods. So push is a new function in the second rendering, and the effect will run again. This is how the second request happened.

Expected Behavior

Methods of router should be stable between rerendering. Or even router itself should be a stable and mutable object, only it's properties should be immutable, like react-router.

To Reproduce

See Describe the Bug.

Another request, could we publish unminified codes to npm? Debug next.js is a disaster even with sourcemap 😞.

@yangmingshan yangmingshan added the bug Issue was opened via the bug report template. label Aug 2, 2021
@yangmingshan
Copy link
Author

Related issue: #27691.

@ijjk
Copy link
Member

ijjk commented Aug 10, 2021

Closing as a duplicate of #18127

@ijjk ijjk closed this as completed Aug 10, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants