<Link>
Visits a page without a full reload. A GET link renders an <a>; a link with any other method renders a <button>.
import { Link } from 'inertia-ember';
<template>
<Link @href='/posts'>Posts</Link>
</template>Arguments
Most arguments are Inertia visit options, passed to router.visit as they are. See Inertia's links, manual visits and prefetching docs for what each one does in depth.
The request
| Argument | Type | Default | Description |
|---|---|---|---|
@href | string | UrlMethodPair | '' | Where to go. An { url, method } pair (what generated route helpers return) also sets the method. |
@method | 'get' | 'post' | 'put' | 'patch' | 'delete' | 'query' | 'get' | The HTTP method. Ignored when @href is a pair. Anything but 'get' renders a <button>. |
@data | Record<string, FormDataConvertible> | FormData | {} | Data to send. For GET it's merged into the URL's query string (and into the <a>'s href); otherwise it's the request body. |
@headers | Record<string, string> | {} | Extra request headers. |
@queryStringArrayFormat | 'brackets' | 'indices' | 'brackets' | How arrays in @data are written in a GET query string: tags[]=a or tags[0]=a. |
@as | string | 'a' | The tag for a GET link: 'a', 'button' or any other, such as 'li' (see Element and attributes). |
What the visit keeps
| Argument | Type | Default | Description |
|---|---|---|---|
@replace | boolean | false | Replace the current history entry instead of pushing a new one. |
@preserveScroll | boolean | 'errors' | (page) => boolean | false | Keep the scroll position. |
@preserveState | boolean | 'errors' | (page) => boolean | false for GET, true otherwise | Keep the page component's local state when the server answers with the same component. |
@preserveUrl | boolean | false | Keep the current URL in the address bar. |
@viewTransition | boolean | (viewTransition) => void | false | Run the page swap inside a view transition. |
@async | boolean | false | An async visit doesn't cancel other visits, and they don't cancel it. |
Partial reloads
| Argument | Type | Default | Description |
|---|---|---|---|
@only | string[] | [] | Ask the server for these props only. See partial reloads. |
@except | string[] | [] | Ask for every prop but these. |
@reset | string[] | [] | Props the server marks as merged (appended page after page) that should be replaced this time instead of appended to. |
Prefetching
| Argument | Type | Default | Description |
|---|---|---|---|
@prefetch | boolean | 'hover' | 'click' | 'mount' | Array<'hover' | 'click' | 'mount'> | false | Load the page before it's visited. true means 'hover' (after a 75 ms hover); 'click' fetches on mousedown and visits on mouseup; 'mount' fetches right away. |
@cacheFor | number | string | Array<number | string> | config's prefetch.cacheFor (30 s); 0 when @prefetch is only 'click' | How long a prefetched page stays fresh: milliseconds, or a string such as '30s' or '1m'. A [fresh, stale] pair keeps serving it while it revalidates. |
@cacheTags | string | string[] | [] | Tags for the prefetched response, so a later visit or form can invalidate it. |
Instant visits
| Argument | Type | Default | Description |
|---|---|---|---|
@component | string | null | A page component to show right away, while the request is in flight. The server's answer then replaces it. |
@instant | boolean | false | Take that component from @href when it's a pair that names one. |
@pageProps | Record<string, unknown> | (currentProps, sharedProps) => Record<string, unknown> | null | Props for that instant page. Without them it gets the current page's shared props. |
Callbacks
Each callback is Inertia's visit callback of the same name.
| Argument | Called with | Description |
|---|---|---|
@onBefore | (visit) | Before the visit starts. Return false to cancel it. |
@onBeforeUpdate | (page) | The new page arrived, before it renders. |
@onStart | (visit) | The request was sent. |
@onProgress | (progress) | Request progress, for uploads. |
@onSuccess | (page) | The new page rendered without validation errors. |
@onError | (errors) | The response had validation errors. |
@onFinish | (visit) | After success, error or cancel. |
@onCancel | () | The visit was cancelled. |
@onCancelToken | (token) | Gives you a token whose cancel() cancels the visit. |
@onHttpException | (response) | The server answered with a non-Inertia error response. |
@onNetworkError | (error) | The request failed without a response. |
@onFlash | (flash) | The response carried flash data. |
@onPrefetching | (visit) | A prefetch started. |
@onPrefetched | (response, visit) | A prefetch finished. |
Blocks
The default block is the link's content. It yields nothing.
Element and attributes
| When | Element |
|---|---|
| GET (the default) | <a href="…"> |
@method isn't GET, or @as='button' | <button type="button"> |
GET with any other @as, e.g. 'li' | <li role="link" tabindex="0"> |
...attributesgoes on that element, soclass,aria-*,data-*and modifiers such as{{on}}work.- The
<a>'shrefis the final URL, with GET@datain its query string, so "open in new tab" works. - While a visit is in flight the element has a
data-loadingattribute. - Clicks with a modifier key, a middle click, or a click on an
<a target="_blank">are left to the browser. - Every callback also fires as a DOM event on the element:
before,start,progress,finish,cancel,beforeUpdate,success,error,httpException,networkError,flash,cancel-token,prefetchingandprefetched. The callback's argument is inevent.detail(detail.page,detail.errors,detail.visit, …). CallingpreventDefault()onbeforecancels the visit. - Every tag takes every argument. Prefer the
<a>for navigation, so "open in new tab" and crawlers work.
Examples
Prefetch the next page
Hovering for 75 ms loads the page, so the click shows it at once:
import { Link } from 'inertia-ember';
<template>
<nav>
<Link @href='/' @prefetch={{true}}>Home</Link>
<Link @href='/posts' @prefetch={{true}} @cacheFor='1m'>Posts</Link>
<Link @href='/settings' @prefetch='mount'>Settings</Link>
</nav>
</template>Delete with a confirmation
A non-GET link renders a <button>. Returning false from @onBefore cancels the visit:
import Component from '@glimmer/component';
import { Link } from 'inertia-ember';
export default class DeletePost extends Component<{ Args: { id: number } }> {
confirm = () => window.confirm('Delete this post?');
<template>
<Link @href='/posts/{{@id}}' @method='delete' @preserveScroll={{true}} @onBefore={{this.confirm}}>
Delete
</Link>
</template>
}Filter a list without losing the page
The server sends a posts prop. These links put the filter in the query string, reload only posts, and keep the page's state and scroll position:
import type { TOC } from '@ember/component/template-only';
import { array, hash } from '@ember/helper';
import { Link, type PageSignature } from 'inertia-ember';
interface Post {
id: number;
title: string;
}
export default <template>
<nav>
{{#each (array 'draft' 'published') as |status|}}
<Link
@href='/posts'
@data={{hash status=status}}
@only={{array 'posts'}}
@preserveState={{true}}
@preserveScroll={{true}}
@replace={{true}}
>{{status}}</Link>
{{/each}}
</nav>
<ul>
{{#each @props.posts key='id' as |post|}}
<li>{{post.title}}</li>
{{/each}}
</ul>
</template> satisfies TOC<PageSignature<{ posts: Post[] }>>;The first link's href is /posts?status=draft.
Show that a visit is in flight
Style the data-loading attribute, or listen for the visit's DOM events with {{on}}:
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { Link } from 'inertia-ember';
export default class ArchiveLink extends Component {
archived = (event: Event) => console.log('Now on', (event as CustomEvent).detail.page.url);
<template>
<Link @href='/archive' @method='post' class='button' {{on 'success' this.archived}}>Archive</Link>
</template>
}.button[data-loading] {
opacity: 0.5;
pointer-events: none;
}Show the next page instantly
@component renders the target page right away, with @pageProps, and swaps in the server's answer when it arrives (Inertia's instant visits):
import { hash } from '@ember/helper';
import { Link } from 'inertia-ember';
<template>
<Link @href='/posts/new' @component='Posts/Create' @pageProps={{hash draft=null}}>New post</Link>
</template>Notes
- To turn an element of your own into a link, use the
{{inertia}}modifier, which takes the same options:<a href='/posts' {{inertia prefetch='mount'}}>. See Your own element. - During server rendering modifiers don't run: the link renders with its final
href(GET@dataincluded) and starts handling clicks once the page hydrates. - To visit from code, use
router(re-exported from@inertiajs/core):router.visit('/posts', { only: ['posts'] }).
Related: Links, router, config for the prefetch defaults, <InfiniteScroll>.