framework icon indicating copy to clipboard operation
framework copied to clipboard

Error When Fast Navigating "Failed to execute 'insertBefore' on 'Node'"

Open viandwi24 opened this issue 3 years ago • 33 comments

Environment



Reproduction

Using Minimal Nuxt 3 Template : https://codesandbox.io/s/loving-feather-utoky

Using My Template : https://codesandbox.io/s/github/viandwi24/nuxt3-awesome-starter

I tested on both templates above the results are the same

Describe the bug

when navigating too fast an error appears like the log below and the application stops working.

for example when I navigate to the "about" page then quickly navigate again to the "home" page.

like the gif below: preview preview2

Additional context

No response

Logs

### **CHROME & BRAVE**

VM946 vue.js:7086 Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
    at insert (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:7086:12)
    at move (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:5432:9)
    at move (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:5405:7)
    at Object.activeBranch.transition.afterLeave (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:2273:15)
    at performRemove (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:5509:20)
    at el._leaveCb (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:2874:9)
    at finishLeave (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:7713:13)
    at resolve2 (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:7743:30)
    at whenTransitionEnds (https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:7825:12)
    at https://utoky.sse.codesandbox.io/_nuxt/node_modules/.vite/vue.js?v=71a85d57:7751:11



### **SAFARI**

[Error] NotFoundError: The object can not be found here.
	insertBefore (vue.js:7086)
	insert (vue.js:7086)
	move (vue.js:5432)
	move (vue.js:5405)
	(anonymous function) (vue.js:2273)
	performRemove (vue.js:5509)
	(anonymous function) (vue.js:2874)
	finishLeave (vue.js:7713)
	(anonymous function) (vue.js:7751)

viandwi24 avatar Feb 09 '22 18:02 viandwi24

I can confirm this bug exists on the latest version v3.0.0-27414150.3589a2d

I have also created a minimal reproduction here: https://github.com/olivervorasai/nuxt3-app-transition-error

For me, the error only occurs when page transitions are defined via page- transition css classes

olivervorasai avatar Feb 14 '22 23:02 olivervorasai

Happens for me as well. Even without the page- transition classes.

thplat avatar Feb 19 '22 19:02 thplat

It happens for me too

zenojunior avatar Mar 06 '22 02:03 zenojunior

Bug still present on 3.0.0-27489107.7379184

joshistoast avatar Apr 07 '22 19:04 joshistoast

Bug still present in latest build:



psycongaroo avatar Apr 08 '22 13:04 psycongaroo

Bug still present on latest: 3.0.0-27496606.e43ba6e

SkySor44 avatar Apr 13 '22 03:04 SkySor44

Just to be clear, this is an upstream vue bug with nested <Suspense> and until it is fixed there it will be present in Nuxt: follow https://github.com/vuejs/core/issues/5513

danielroe avatar Apr 13 '22 12:04 danielroe

We needed to get a presentation-like application tested and testers&customer got stuck spamming the next button. So for the time being, while hoping for a fix, we just reload the page when this error appears.

It's a dirty trick 👀 and not recommended, but for now it does the trick to make the application not unresponsive. You can run the following code as client-only code somewhere.

window.onerror = function(e){
    if(e.includes("NotFoundError:")){
        document.location.reload()
        return true;
    }
   
}

vanling avatar May 29 '22 11:05 vanling

A lot of time has passed. Are there any solutions?

AkioSarkiz avatar Jun 19 '22 21:06 AkioSarkiz

A lot of time has passed. Are there any solutions?

@AkioSarkiz Check this link for a temporary solution: https://stackoverflow.com/questions/70396414/nuxtlink-is-updating-route-in-nuxt-3-app-but-not-rendering-contents/70404764

yalondpsi avatar Jun 20 '22 13:06 yalondpsi

Strange hyperlink given above, this one should be better: https://stackoverflow.com/questions/70396414/nuxtlink-is-updating-route-in-nuxt-3-app-but-not-rendering-contents/70404764

kissu avatar Jun 22 '22 00:06 kissu

@kissu yes, but I think the problem is different than the one in this issue :) The solution in that link is about using a single root element which has nothing to do with this issue.

vanling avatar Jun 22 '22 14:06 vanling

for now I'm overcoming this error with the @vanling example above. at least this way ensures the client doesn't have a bad experience when an error occurs.

  const messages = [
    `Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.`, // chromium based
    `NotFoundError: The object can not be found here.`, // safari
  ]
  if (typeof window !== 'undefined') {
    window.addEventListener('error', (ev) => {
      if (messages.includes(ev.message)) {
        ev.preventDefault()
        window.location.reload()
      }
    })
  }

https://github.com/viandwi24/nuxt3-awesome-starter/blob/d8b1c3bba15cb6fa42573b8c04eaa0b62edfe94c/utils/app.ts#L30-L41

viandwi24 avatar Jul 02 '22 12:07 viandwi24

它也发生在我身上

and me

15617664182 avatar Aug 03 '22 11:08 15617664182

Console error, blank page

15617664182 avatar Aug 03 '22 11:08 15617664182

try setting a key to the top level NuxtPage in app.vue like so:

<template>
  <NuxtLayout>
    <NuxtPage :key="$route.fullPath"/>
  </NuxtLayout>
</template>

genu avatar Aug 05 '22 16:08 genu

@genu's solution is working for me. Thanks. Incase you don't want to use app.vue, putting a key in the root element of a layout seems to work.

john-prutton avatar Aug 08 '22 10:08 john-prutton

try setting a key to the top level NuxtPage in app.vue like so:

<template>
  <NuxtLayout>
    <NuxtPage :key="$route.fullPath"/>
  </NuxtLayout>
</template>

Thank you omg, this works, but why. I worked on my website, came back the next day and somehow I got this bug and apparently the key trick fixed it? How did this happen in the first place?

Edit: Nevermind, it didn't really fix it. Every time I navigate now it reloads all icons and images again and it glitches a little bit. :(

MaxLikesCode avatar Aug 10 '22 21:08 MaxLikesCode

try setting a key to the top level NuxtPage in app.vue like so:

<template>
  <NuxtLayout>
    <NuxtPage :key="$route.fullPath"/>
  </NuxtLayout>
</template>

Thank you omg, this works, but why. I worked on my website, came back the next day and somehow I got this bug and apparently the key trick fixed it? How did this happen in the first place?

Edit: Nevermind, it didn't really fix it. Every time I navigate now it reloads all icons and images again and it glitches a little bit. :(

Try also not using <NuxtPage/> (maybe <NuxtLayut/> as well?) as a top-level component. So, use div as a top-level, like so:

<template>
  <div>
    <NuxtLayout>
      <NuxtPage :key="$route.fullPath"/>
    </NuxtLayout>
   </div
</template>

Its possible that this breaks regardless of the key present because <NuxtPage/> doesn't render anything.

<template>
    <NuxtPage :key="$route.fullPath" />
</template>

genu avatar Aug 11 '22 15:08 genu

@genu your solution worked to get rid of the error. but the animations transition page is not working again.

the transition I use is like my reproduction code above.

.page-enter-active {
  transition: all 0.1s ease-out;
}
.page-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.page-enter-from,
.page-leave-to {
  transform: translateY(20px);
  opacity: 0;
}

.layout-enter-active {
  transition: all 0.1s ease-out;
}
.layout-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}
.layout-enter-from,
.layout-leave-to {
  transform: translateY(-20px);
  opacity: 0;
}

and it becomes a problem as @olivervorasai explained above, that in his case this problem will arise when he defines the transitions for the page.

viandwi24 avatar Aug 12 '22 09:08 viandwi24

@genu your solution worked to get rid of the error. but the animations transition page is not working again.

@viandwi24 So I seem to have found a temporary fix for this. By still using the :key method like @genu suggests but instead wrapping a <Transition> with a keyed div around the <NuxtPage />.

<NuxtLayout>
  <Transition name="page" mode="out-in">
    <div :key="$route.fullPath">
      <NuxtPage />
    </div>
  </Transition>
</NuxtLayout>

Willadaygo avatar Aug 18 '22 17:08 Willadaygo

@genu your solution worked to get rid of the error.

but the animations transition page is not working again.

@viandwi24 So I seem to have found a temporary fix for this. By still using the :key method like @genu suggests but instead wrapping a <Transition> with a keyed div around the <NuxtPage />.


<NuxtLayout>

  <Transition name="page" mode="out-in">

    <div :key="$route.fullPath">

      <NuxtPage />

    </div>

  </Transition>

</NuxtLayout>

🩼🩼🩼

AkioSarkiz avatar Aug 18 '22 20:08 AkioSarkiz

for now I'm overcoming this error with the @vanling example above. at least this way ensures the client doesn't have a bad experience when an error occurs.

  const messages = [
    `Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.`, // chromium based
    `NotFoundError: The object can not be found here.`, // safari
  ]
  if (typeof window !== 'undefined') {
    window.addEventListener('error', (ev) => {
      if (messages.includes(ev.message)) {
        ev.preventDefault()
        window.location.reload()
      }
    })
  }

viandwi24/nuxt3-awesome-starter@d8b1c3b/utils/app.ts#L30-L41

Thanks for this snippet!

Instead of performing a hard reload, I managed to just hot reload the application by keying the layout (basically remounted the content). This allows transitions to keep working (they would just be skipped when navigating too fast) but also to keep the application state.

<script setup lang="ts">
// TODO: Remove when https://github.com/vuejs/core/issues/5513 fixed
const key = ref(0);
const messages = [
	`Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.`, // chromium based
	`NotFoundError: The object can not be found here.`, // safari
];
if (typeof window !== "undefined") {
	// @ts-expect-error using arbitrary window key
	if (!window.__vue5513) {
		window.addEventListener("error", (event) => {
			if (messages.includes(event.message)) {
				event.preventDefault();
				console.warn(
					"Rerendering layout because of https://github.com/vuejs/core/issues/5513",
				);
				key.value++;
			}
		});
	}

	// @ts-expect-error using arbitrary window key
	window.__vue5513 = true;
}
</script>

<template>
	<div :key="key">
		<slot />
	</div>
</template>

Same disclaimer as above, it's fragile, use at your own risk.

lihbr avatar Aug 25 '22 15:08 lihbr

@lihbr awesome!

vanling avatar Aug 25 '22 19:08 vanling

<template>
  <NuxtLayout>
      <NuxtPage />
  </NuxtLayout>
</template>

Changing my app.vue content to the above solved it for me. Somehow this solved my problem, I don't know the reason. Hope it helps you

ayitinya avatar Oct 09 '22 06:10 ayitinya

I was not using an App.vue file and I was facing this issue when navigating from a page with disabled layout option to a page with layout enabled.

https://github.com/nuxt/framework/issues/7038#issuecomment-1251680876

I created the App.vue file just like this and seems to work:

<template>
  <NuxtLayout>
    <NuxtPage :key="$route.fullPath" />
  </NuxtLayout>
</template>

Don't really know what's happening here but seems to work.

IsraelOrtuno avatar Oct 12 '22 13:10 IsraelOrtuno

Seems like fixed in rc-12 release. This can be closed @danielroe

IsraelOrtuno avatar Oct 19 '22 07:10 IsraelOrtuno

Just tried with a rc12 deployment and it still ran into the same same issues. Are you sure @IsraelOrtuno?

warflash avatar Oct 19 '22 09:10 warflash

Also need to add, that such <NuxtPage :key="$route.fullPath" /> workaround broke component reuse. F.e. when you pagination across one component with different data, because it re-render it's completely. On small examples you don't see that, but if on something bigger it's took quite a long time.

Nobi322 avatar Oct 20 '22 13:10 Nobi322

nuxt is coming release at 3 day, but this bug is not resolved

AkioSarkiz avatar Oct 22 '22 15:10 AkioSarkiz