Api
useSkewProtection()
Last updated by
Harlan Wilton
in doc: sync. The main composable for interacting with Nuxt Skew Protection.
Usage
const skew = useSkewProtection()
Returns
manifest
- Type:
Ref<NuxtAppManifestMeta | null>
The current build manifest from builds/latest.json.
const { manifest } = useSkewProtection()
console.log(manifest.value?.id) // Current buildId
console.log(manifest.value?.timestamp) // Build timestamp
currentVersion
- Type:
Ref<string>
The user's current version (from cookie or buildId).
const { currentVersion } = useSkewProtection()
console.log(currentVersion.value) // "abc123"
latestVersion
- Type:
ComputedRef<string | null>
The latest deployed version from the manifest.
const { latestVersion } = useSkewProtection()
console.log(latestVersion.value) // "def456"
isOutdated
- Type:
ComputedRef<boolean>
Whether the user's version is outdated.
const { isOutdated } = useSkewProtection()
if (isOutdated.value) {
console.log('User is on an old version')
}
checkForUpdates()
- Type:
() => Promise<void>
Manually trigger an update check.
const { checkForUpdates } = useSkewProtection()
// Check for updates
await checkForUpdates()
onCurrentChunksOutdated()
- Type:
(callback: (payload: ChunksOutdatedPayload) => void) => void
Register a callback for when loaded chunks become outdated.
const { onCurrentChunksOutdated } = useSkewProtection()
onCurrentChunksOutdated((payload) => {
console.log('Deleted chunks:', payload.deletedChunks)
console.log('Invalidated modules:', payload.invalidatedModules)
console.log('Passed releases:', payload.passedReleases)
// Show notification
alert('Please reload to get the latest version')
})
Type Definitions
interface NuxtAppManifestMeta {
id: string
timestamp: number
skewProtection?: {
current: string
versions: Record<string, VersionInfo>
}
}
interface VersionInfo {
timestamp: string
expires: string
assets: string[]
deletedChunks?: string[]
}
interface ChunksOutdatedPayload {
deletedChunks: string[]
invalidatedModules: string[]
passedReleases: string[]
}
Examples
Custom Invalidation Handler
const { onCurrentChunksOutdated } = useSkewProtection()
onCurrentChunksOutdated(({ invalidatedModules, passedReleases }) => {
// Track in analytics
analytics.track('chunks_outdated', {
count: invalidatedModules.length,
modules: invalidatedModules,
releases: passedReleases
})
// Show custom UI
showUpdateDialog({
title: 'Update Required',
message: `${invalidatedModules.length} modules need updating (${passedReleases.length} releases passed)`
})
})
Version Information Display
<script setup>
const { currentVersion, latestVersion, manifest } = useSkewProtection()
const buildDate = computed(() => {
if (!manifest.value?.timestamp)
return null
return new Date(manifest.value.timestamp).toLocaleString()
})
</script>
<template>
<div class="version-info">
<p>Your version: {{ currentVersion }}</p>
<p>Latest version: {{ latestVersion }}</p>
<p>Build date: {{ buildDate }}</p>
</div>
</template>
Programmatic Update Check
Note: Only relevant if you're using polling.
const { checkForUpdates, isOutdated } = useSkewProtection()
async function handleUserAction() {
// Check for updates before critical action
await checkForUpdates()
if (isOutdated.value) {
const shouldReload = confirm('A new version is available. Reload now?')
if (shouldReload) {
window.location.reload()
}
}
else {
// Proceed with action
await performCriticalAction()
}
}
Did this page help you?