MCPcopy
hub / github.com/vercel/next.js / linkClicked

Function linkClicked

packages/next/src/client/link.tsx:217–286  ·  view source on GitHub ↗
(
  e: React.MouseEvent,
  router: NextRouter | AppRouterInstance,
  href: string,
  as: string,
  replace?: boolean,
  shallow?: boolean,
  scroll?: boolean,
  locale?: string | false,
  onNavigate?: OnNavigateEventHandler
)

Source from the content-addressed store, hash-verified

215}
216
217function linkClicked(
218 e: React.MouseEvent,
219 router: NextRouter | AppRouterInstance,
220 href: string,
221 as: string,
222 replace?: boolean,
223 shallow?: boolean,
224 scroll?: boolean,
225 locale?: string | false,
226 onNavigate?: OnNavigateEventHandler
227): void {
228 const { nodeName } = e.currentTarget
229
230 // anchors inside an svg have a lowercase nodeName
231 const isAnchorNodeName = nodeName.toUpperCase() === 'A'
232
233 if (
234 (isAnchorNodeName && isModifiedEvent(e)) ||
235 e.currentTarget.hasAttribute('download')
236 ) {
237 // ignore click for browser’s default behavior
238 return
239 }
240
241 if (!isLocalURL(href)) {
242 if (replace) {
243 // browser default behavior does not replace the history state
244 // so we need to do it manually
245 e.preventDefault()
246 location.replace(href)
247 }
248
249 // ignore click for browser’s default behavior
250 return
251 }
252
253 e.preventDefault()
254
255 const navigate = () => {
256 if (onNavigate) {
257 let isDefaultPrevented = false
258
259 onNavigate({
260 preventDefault: () => {
261 isDefaultPrevented = true
262 },
263 })
264
265 if (isDefaultPrevented) {
266 return
267 }
268 }
269
270 // If the router is an NextRouter instance it will have `beforePopState`
271 const routerScroll = scroll ?? true
272 if ('beforePopState' in router) {
273 router[replace ? 'replace' : 'push'](href, as, {
274 shallow,

Callers 1

onClickFunction · 0.70

Calls 5

isLocalURLFunction · 0.90
toUpperCaseMethod · 0.80
isModifiedEventFunction · 0.70
navigateFunction · 0.70
replaceMethod · 0.65

Tested by

no test coverage detected