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

Function callback

packages/next/src/client/app-link-gc.ts:4–64  ·  view source on GitHub ↗
(mutationList: MutationRecord[])

Source from the content-addressed store, hash-verified

2 // TODO-APP: Remove this logic when Float has GC built-in in development.
3 if (process.env.NODE_ENV !== 'production') {
4 const callback = (mutationList: MutationRecord[]) => {
5 for (const mutation of mutationList) {
6 if (mutation.type === 'childList') {
7 for (const node of mutation.addedNodes) {
8 if (
9 'tagName' in node &&
10 (node as HTMLLinkElement).tagName === 'LINK'
11 ) {
12 const link = node as HTMLLinkElement
13 if (link.dataset.precedence?.startsWith('next')) {
14 const href = link.getAttribute('href')
15 if (href) {
16 const [resource, version] = href.split('?v=', 2)
17 if (version) {
18 const currentOrigin = window.location.origin
19 const allLinks = [
20 ...document.querySelectorAll(
21 'link[href^="' + resource + '"]'
22 ),
23 // It's possible that the resource is a full URL or only pathname,
24 // so we need to remove the alternative href as well.
25 ...document.querySelectorAll(
26 'link[href^="' +
27 (resource.startsWith(currentOrigin)
28 ? resource.slice(currentOrigin.length)
29 : currentOrigin + resource) +
30 '"]'
31 ),
32 ] as HTMLLinkElement[]
33
34 for (const otherLink of allLinks) {
35 if (otherLink.dataset.precedence?.startsWith('next')) {
36 const otherHref = otherLink.getAttribute('href')
37 if (otherHref) {
38 const [, otherVersion] = otherHref.split('?v=', 2)
39 if (!otherVersion || +otherVersion < +version) {
40 // Delay the removal of the stylesheet to avoid FOUC
41 // caused by `@font-face` rules, as they seem to be
42 // a couple of ticks delayed between the old and new
43 // styles being swapped even if the font is cached.
44 setTimeout(() => {
45 otherLink.remove()
46 }, 5)
47 const preloadLink = document.querySelector(
48 `link[rel="preload"][as="style"][href="${otherHref}"]`
49 )
50 if (preloadLink) {
51 preloadLink.remove()
52 }
53 }
54 }
55 }
56 }
57 }
58 }
59 }
60 }
61 }

Callers 15

readyFunction · 0.70
createObserverFunction · 0.70
HeadFunction · 0.70
RootFunction · 0.70
externalHandlerFunction · 0.50
processTurbopackMessageFunction · 0.50
transformMethod · 0.50
_updateMethod · 0.50
handlerFunction · 0.50
withExecuteRevalidatesFunction · 0.50
addCallbackMethod · 0.50
_writeMethod · 0.50

Calls 6

startsWithMethod · 0.80
getAttributeMethod · 0.80
splitMethod · 0.80
setTimeoutFunction · 0.50
sliceMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected