UNPKG

@rsc-kit/mcp

Version:

An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.

178 lines (131 loc) • 6.63 kB
# Navigation > Moving between pages without a reload - visit, prefetch, progress, refresh, and measuring a click. `Link` moves to another page without reloading the document. Only the part below the layouts both pages share is fetched and rendered: ```tsx import Link from '@rsc-kit/core/Link' <Link href="/about">About</Link> ``` The `Link` props are listed in [Routing](/guides/routing#linking-between-pages). This page covers navigating from code, prefetching, progress, refreshing, and measuring a navigation. ## Navigating from code ```tsx 'use client' import { visit } from '@rsc-kit/core/router' <button onClick={() => visit('/checkout')}>Checkout</button> ``` ```ts await visit('/dashboard', { replace: true }) // no new history entry ``` A default export is available too, if you prefer `router.visit('/about')`. ## What a navigation keeps Going from `/posts/one` to `/posts/two`, the root layout and the posts layout are already on screen, so only the page is replaced. That has two effects: - **State in a layout survives.** A sidebar's scroll position, an open menu or a half-typed search box in a layout is left alone, because the layout isn't rendered again. - **A page you go back to is still there.** The last few pages visited are kept mounted and hidden, so Back restores a half-filled form as it was left. ## Prefetching A `<Link>` loads its page before it is clicked, so the click usually finds the page ready. To prefetch a page you know someone is going to next: ```tsx 'use client' import { prefetch } from '@rsc-kit/core/router' useEffect(() => { prefetch('/step-2') }, []) useEffect(() => { prefetch('/step-2', undefined, { intent: true }) }, []) // and decode it now ``` The second argument is how long the payload stays usable, in milliseconds. `intent: true` also decodes it as it lands, which loads the page's client code: use it for a page the visitor is about to open, not one they may never open. ### What a click finds already there A link prefetches in steps, because each costs differently: - **The page's data, as the link comes into view.** The first dozen links on screen, on every device, when the browser is idle, at low priority so a real navigation goes first. A link past the dozen is fetched on touch or on a hover. The data is kept for thirty seconds for a page rendered per visitor, and five minutes for a page the build made; `cacheFor` on the link sets its own. A link asks again when it comes back into view, or the tab does, only if what it holds has expired. Nothing is fetched on a timer, so a page left open fetches nothing. - **The images, as the data arrives.** The first six images the page shows at once (not `loading="lazy"`) are requested at low priority, and again at high priority when the visitor shows intent. - **The client code, as the data arrives.** The code for the first eight client components the page renders is loaded while the browser is idle. - **The decode, on intent.** The page is decoded and rendered hidden when a pointer rests on the link for 100 ms, or on a press or a touch. Each of those comes 60-300 ms before the click, so the click shows a page that is already rendered. With Save-Data on, links don't prefetch as they come into view; hover and touch still do. A link to a guarded page - "Sign in" to an app that sends a signed-out visitor to `/login` - prefetches the destination too, so the tap asks only the guard, which answers before anything renders. It asks the guard for real because a guard also remembers where the visitor was going, and a prefetch is never allowed to. ## Showing progress `useLinkStatus` reports whether the nearest `Link` is loading: ```tsx 'use client' import { useLinkStatus } from '@rsc-kit/core/useLinkStatus' function Spinner() { const { pending } = useLinkStatus() return pending ? <span>Loading…</span> : null } ``` A `Link` also has a `data-pending` attribute while it loads. For what the new page shows while its data arrives, see [showing a loading state](/guides/routing#showing-a-loading-state). ## Refreshing the current page ```tsx 'use client' import { refresh } from '@rsc-kit/core/router' <button onClick={() => refresh()}>Reload</button> ``` ```ts refresh() // the page, leaving the layouts mounted refresh('all') // the whole document, layouts included refresh('orders') // one section or parallel slot ``` The scroll positions are restored afterwards: the window's, and any element that scrolls on its own, such as a sidebar. Refreshing the page or `'all'` also forgets the pages kept for Back and anything a hover prefetched, since they were fetched before the data moved. Refreshing one section keeps them, so a page with a polled or live section still goes Back without a request and with its forms as they were left. ## Measuring a navigation Every navigation leaves marks on the browser's timeline, which the Performance panel of any dev tools shows: | Mark | When | | --- | --- | | `rsc-kit:navigate:start` | the click | | `rsc-kit:navigate:decoded` | the page decoded, and its client code loaded | | `rsc-kit:navigate:applied` | handed to React | | `rsc-kit:navigate` (measure) | from the click to the page on screen | In a console, after a few clicks: ```js performance.getEntriesByName('rsc-kit:navigate').map((m) => Math.round(m.duration)) ``` - **A gap before `decoded`** is the network or the code: the page wasn't prefetched, or was never decoded, as with a scripted click or `prefetch={false}`. - **A gap after `applied`** is rendering the page on that device. On an iPhone, Safari's Web Inspector (Develop → your iPhone → the page) shows the same marks in its Timelines tab. When the router does a full document load instead - a `route.ts`, a URL in `backendPaths`, another site, a newer build, a file the deploy no longer serves - it says why first, with a `rsc-kit:document-load` event: ```js window.addEventListener('rsc-kit:document-load', (e) => console.log(e.detail)) ``` ## Clicks before the page has loaded its JavaScript A tap on a `<Link>` before the page's JavaScript is running is held, and navigated the moment the router is ready. If it never becomes ready, the browser gets the tap back after four seconds as a normal page load. Only `<Link>`s are held: a plain `<a>`, a `route.ts`, a file and `mailto:` behave as usual. A form whose action is a server action is held the same way and submitted once the page is ready. If the page isn't ready after three seconds, the form posts the way a form with no JavaScript does. A tap on a link after submitting wins.