You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For performance it can be helpful to memoize the result of expensive operations within the module scope of an imported script. Lifecycle hooks on the front end (like onUnmounted) allow for this kind of cleanup, but currently there is no lifecycle hook to perform this operation on the server. Take for example:
<script setup>
import { onUnmounted } from'vue'import { performExpensiveOperation } from'./expensive'constprops=defineProps(['info'])const [value, cleanUp] =performExpensiveOperation(props.info)// onUnmounted doesn’t run on the server so GC is never run.onUnmounted(cleanUp)
</script>
<template>
{{ value }}
</template>
What does the proposed API look like?
Proposal: onDestroyed
A lifecycle hook that is universally called after server and client render. Perhaps called onDestroyed().
<script setup>
import { onDestroyed } from'vue'import { performExpensiveOperation } from'./expensive'constprops=defineProps(['info'])const [value, cleanUp] =performExpensiveOperation(props.info)onDestroyed(cleanUp). // 👀 Called on the client and the server
</script>
<template>
{{ value }}
</template>
Depending on the mechanics of the server, it may necessary to indicate the finality of an app’s lifecycle to reliably perform the onDestroyed hook:
// server.mjsimport{createSSRApp}from'vue'import{renderToString,destroy}from'@vue/server-renderer'constserver=http.createServer((req,res)=>{// Creates a new app on each request for context isolation:constapp=createSSRApp({template: '<ExpensiveComponent />',})renderToString(app).then((html)=>{destroy(app)res.statusCode=200res.setHeader('Content-Type','text/html')res.end(`<!DOCTYPE /> <html>${html} </html>`)})})
On the client this hook would be executed after all existing lifecycle hooks.
Possible alternative?
Node 14.6 added the FinalizationRegistry API which could theoretically be used server side to detect when an object within a component’s scope is garbage collected and trigger the appropriate cleanup operation.
The text was updated successfully, but these errors were encountered:
What problem does this feature solve?
For performance it can be helpful to memoize the result of expensive operations within the module scope of an imported script. Lifecycle hooks on the front end (like
onUnmounted
) allow for this kind of cleanup, but currently there is no lifecycle hook to perform this operation on the server. Take for example:The
ExpensiveComponent.vue
file:What does the proposed API look like?
Proposal:
onDestroyed
A lifecycle hook that is universally called after server and client render. Perhaps called
onDestroyed()
.Depending on the mechanics of the server, it may necessary to indicate the finality of an app’s lifecycle to reliably perform the
onDestroyed
hook:On the client this hook would be executed after all existing lifecycle hooks.
Possible alternative?
Node 14.6 added the
FinalizationRegistry
API which could theoretically be used server side to detect when an object within a component’s scope is garbage collected and trigger the appropriate cleanup operation.The text was updated successfully, but these errors were encountered: