From 94ad596819b1f9783204fbfff76a466072e01628 Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Fri, 28 Apr 2023 13:49:54 +0200 Subject: [PATCH] Run GC --- .../next-core/js/src/entry/app/hydrate.tsx | 3 ++ packages/next/src/client/app-index.tsx | 53 +------------------ packages/next/src/client/app-link-gc.ts | 53 +++++++++++++++++++ 3 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 packages/next/src/client/app-link-gc.ts diff --git a/packages/next-swc/crates/next-core/js/src/entry/app/hydrate.tsx b/packages/next-swc/crates/next-core/js/src/entry/app/hydrate.tsx index 2aecce7c0ac2..d713ab7e3157 100644 --- a/packages/next-swc/crates/next-core/js/src/entry/app/hydrate.tsx +++ b/packages/next-swc/crates/next-core/js/src/entry/app/hydrate.tsx @@ -4,6 +4,7 @@ import type { ReactElement } from 'react' import { version } from 'next/package.json' import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack/client' import { callServer } from 'next/dist/client/app-call-server' +import { linkGc } from 'next/dist/client/app-link-gc' import { HeadManagerContext } from 'next/dist/shared/lib/head-manager-context' @@ -153,6 +154,8 @@ function hydrate() { ReactDOMClient.hydrateRoot(appElement, reactEl) }) } + + linkGc() } window.next = { diff --git a/packages/next/src/client/app-index.tsx b/packages/next/src/client/app-index.tsx index 591580a56b13..77a2d17f0c52 100644 --- a/packages/next/src/client/app-index.tsx +++ b/packages/next/src/client/app-index.tsx @@ -10,6 +10,7 @@ import { GlobalLayoutRouterContext } from '../shared/lib/app-router-context' import onRecoverableError from './on-recoverable-error' import { callServer } from './app-call-server' import { isNextRouterError } from './components/is-next-router-error' +import { linkGc } from './app-link-gc' // Since React doesn't call onerror for errors caught in error boundaries. const origConsoleError = window.console.error @@ -297,55 +298,5 @@ export function hydrate() { reactRoot.render(reactEl) } - // TODO-APP: Remove this logic when Float has GC built-in in development. - if (process.env.NODE_ENV !== 'production') { - const callback = (mutationList: MutationRecord[]) => { - for (const mutation of mutationList) { - if (mutation.type === 'childList') { - for (const node of mutation.addedNodes) { - if ( - 'tagName' in node && - (node as HTMLLinkElement).tagName === 'LINK' - ) { - const link = node as HTMLLinkElement - if (link.dataset.precedence === 'next.js') { - const href = link.getAttribute('href') - if (href) { - const [resource, version] = href.split('?v=') - if (version) { - const allLinks = document.querySelectorAll( - `link[href^="${resource}"]` - ) as NodeListOf - for (const otherLink of allLinks) { - if (otherLink.dataset.precedence === 'next.js') { - const otherHref = otherLink.getAttribute('href') - if (otherHref) { - const [, otherVersion] = otherHref.split('?v=') - if (!otherVersion || +otherVersion < +version) { - otherLink.remove() - const preloadLink = document.querySelector( - `link[rel="preload"][as="style"][href="${otherHref}"]` - ) - if (preloadLink) { - preloadLink.remove() - } - } - } - } - } - } - } - } - } - } - } - } - } - - // Create an observer instance linked to the callback function - const observer = new MutationObserver(callback) - observer.observe(document.head, { - childList: true, - }) - } + linkGc() } diff --git a/packages/next/src/client/app-link-gc.ts b/packages/next/src/client/app-link-gc.ts new file mode 100644 index 000000000000..faacd087aec5 --- /dev/null +++ b/packages/next/src/client/app-link-gc.ts @@ -0,0 +1,53 @@ +export function linkGc() { + // TODO-APP: Remove this logic when Float has GC built-in in development. + if (process.env.NODE_ENV !== 'production') { + const callback = (mutationList: MutationRecord[]) => { + for (const mutation of mutationList) { + if (mutation.type === 'childList') { + for (const node of mutation.addedNodes) { + if ( + 'tagName' in node && + (node as HTMLLinkElement).tagName === 'LINK' + ) { + const link = node as HTMLLinkElement + if (link.dataset.precedence === 'next.js') { + const href = link.getAttribute('href') + if (href) { + const [resource, version] = href.split('?v=') + if (version) { + const allLinks = document.querySelectorAll( + `link[href^="${resource}"]` + ) as NodeListOf + for (const otherLink of allLinks) { + if (otherLink.dataset.precedence === 'next.js') { + const otherHref = otherLink.getAttribute('href') + if (otherHref) { + const [, otherVersion] = otherHref.split('?v=') + if (!otherVersion || +otherVersion < +version) { + otherLink.remove() + const preloadLink = document.querySelector( + `link[rel="preload"][as="style"][href="${otherHref}"]` + ) + if (preloadLink) { + preloadLink.remove() + } + } + } + } + } + } + } + } + } + } + } + } + } + + // Create an observer instance linked to the callback function + const observer = new MutationObserver(callback) + observer.observe(document.head, { + childList: true, + }) + } +}