mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 01:17:16 +00:00
2.0 KiB
2.0 KiB
title | description | links | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
onPrehydrate | Use onPrehydrate to run a callback on the client immediately before Nuxt hydrates the page. |
|
::important This composable is available in Nuxt v3.12+. ::
onPrehydrate
is a composable lifecycle hook that allows you to run a callback on the client immediately before
Nuxt hydrates the page.
::note
This is an advanced utility and should be used with care. For example, nuxt-time
and @nuxtjs/color-mode
manipulate the DOM to avoid hydration mismatches.
::
Usage
onPrehydrate
can be called directly in the setup function of a Vue component (for example, in <script setup>
), or in a plugin.
It will only have an effect when it is called on the server, and it will not be included in your client build.
Parameters
callback
: A function that will be stringified and inlined in the HTML. It should not have any external dependencies (such as auto-imports) or refer to variables defined outside the callback. The callback will run before Nuxt runtime initializes so it should not rely on the Nuxt or Vue context.
Example
<script setup lang="ts">
declare const window: Window
// ---cut---
// onPrehydrate is guaranteed to run before Nuxt hydrates
onPrehydrate(() => {
console.log(window)
})
// As long as it only has one root node, you can access the element
onPrehydrate((el) => {
console.log(el.outerHTML)
// <div data-v-inspector="app.vue:15:3" data-prehydrate-id=":b3qlvSiBeH:"> Hi there </div>
})
// For _very_ advanced use cases (such as not having a single root node) you
// can access/set `data-prehydrate-id` yourself
const prehydrateId = onPrehydrate((el) => {})
</script>
<template>
<div>
Hi there
</div>
</template>