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

Bug with a hard reload on navigation #32

Open
vladfedoriuk opened this issue Nov 21, 2023 · 2 comments
Open

Bug with a hard reload on navigation #32

vladfedoriuk opened this issue Nov 21, 2023 · 2 comments
Labels
bug Something isn't working frontend

Comments

@vladfedoriuk
Copy link
Collaborator

When we use the Next.js router navigation (even just to update the query parameters), the hard reload is happening and the table is flickering (first, the unfiltered table is shown, then the table re-renders and the actual filtered result is displayed).

Perhaps, related to netlify/next-runtime#2089

Also, upgrading Next.js might be helpful: #26

Overall, it should be investigated how the table flickering on reload can be prevented.

@vladfedoriuk vladfedoriuk added bug Something isn't working frontend labels Nov 21, 2023
@julia-script
Copy link

yep, it's related to that, even updating query params messes it up..I had to come up with a hack to work with search params

it only works with search params though, and I didn't implement the scroll flag cause I don't need it

"use client";
import { useSyncExternalStore } from "react";

const isServer = typeof window === "undefined";
class History {
  listeners = new Set<() => void>();

  constructor() {
    if (isServer) {
      return;
    }
    window.addEventListener("popstate", () => {
      this.emit();
    });
  }
  emit() {
    this.listeners.forEach((cb) => cb());
  }
  subscribe = (cb: () => void) => {
    this.listeners.add(cb);
    return () => {
      this.listeners.delete(cb);
    };
  };
  push = (url: string, _: { scroll: boolean }) => {
    if (isServer) {
      return;
    }
    window.history.pushState({}, "", url);
    this.emit();
  };
  replace = (url: string, _: { scroll: boolean }) => {
    if (isServer) {
      return;
    }
    window.history.replaceState({}, "", url);
    this.emit();
  };
  getSnapshot = () => {
    return new URL(window.location.href).searchParams.toString();
  };
}
const customHistory = new History();
export const _useRouter = () => {
  const searchParams = useSyncExternalStore(
    customHistory.subscribe,
    customHistory.getSnapshot,
    () => ""
  );

  return {
    searchParams,
    push: (url: string, options: { scroll: boolean }) => {
      customHistory.push(url, options);
    },
    replace: (url: string, options: { scroll: boolean }) => {
      customHistory.replace(url, options);
    },
  };
};

@vladfedoriuk
Copy link
Collaborator Author

@julia-script Thanks for the input!
I believe a custom history could be a solution for that.
I see there is also some progress on vercel/next.js#48110, so maybe a fix will be delivered shortly in some future release.
I have recently found a package: https://github.com/47ng/next-usequerystate
What caught my eye is: https://github.com/47ng/next-usequerystate#shallow

Note: the app router doesn't vercel/next.js#48110 have this capability natively, but next-usequerystate does, by bypassing the router on shallow updates.

So, I think using this package until a proper fix is introduced in Next.js could be worth trying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working frontend
Projects
None yet
Development

No branches or pull requests

2 participants