I have the following problem: I have a page in my Next.js Project and use getStaticProps for this page to preload data. Since this takes some seconds, I created a fallback page that is shown during this process. If I enter the link to this page directly as a URL in the browser, it works perfectly. First the loading page is shown until the data is completly loaded. Then it switches to the new page. But if I want to navigate there from another page via useRouter(), it doesn't work. Instead, I just stay on the old page until the new page finishes loading and then I get to see the new page directly, without seeing the loading page in between. Any ideas why it wont show the loading page?
Below I have copied the few important code passages relevant for this from the whole code.
function NewPage(props) {
// Show while fetching the data
if (!props.data) {
return <LoadingPage></LoadingPage>;
}
// After loading the regular page is shown
return (
xyz......
)
}
export async function getStaticProps(context) {
const data= await someLoadingFunction();
if (!data) {
// Showing loading screen
return {};
}
return {
props: {
data:data
},
};
}
This is the page from which I would like to redirect to the new page:
import { useRouter } from "next/router";
function OldPage(props) {
const router = useRouter();
function redirect (path) {
router.push({
pathname: path,
query: { xyz }
});
return (
<button onClick={redirect}>Redirect</button>
)
}