fix: clear ONLYOFFICE internal cache after save + mtime-based download URL

This commit is contained in:
root
2026-07-16 11:22:43 +00:00
parent ec97256e17
commit 9e3c296427
4 changed files with 19 additions and 7 deletions

View File

@@ -146,10 +146,11 @@ router.get('/:templateId/config', (req, res) => {
const docKey = generateDocKey(templateId, fileMtime);
const fileName = template.originalName || template.name + '.docx';
// Add cache-busting timestamp to the document URL so ONLYOFFICE
// always fetches the latest version from our backend
const cacheBuster = Date.now();
const documentUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/download?cb=${cacheBuster}`;
// Add file mtime as cache-buster to the document URL so ONLYOFFICE
// always fetches the latest version from our backend.
// The mtime changes every time the file is saved, so the URL is
// different after each save, forcing ONLYOFFICE to re-download.
const documentUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/download?mtime=${fileMtime}&cb=${Date.now()}`;
const callbackUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/callback`;
const config = {
@@ -316,6 +317,17 @@ router.post('/:templateId/callback', async (req, res) => {
console.log(`Template "${template.name}" updated successfully (${response.data.length} bytes)`);
// Clear ONLYOFFICE's internal document cache for this template
// so the next edit session fetches the fresh file
try {
const { execSync } = require('child_process');
execSync(`docker exec onlyoffice-server find /var/lib/onlyoffice/documentserver/App_Data/cache -name "cvtemplate_${templateId}*" -type d -exec rm -rf {} + 2>/dev/null || true`, { timeout: 5000 });
console.log('ONLYOFFICE cache cleared for template:', templateId);
} catch (cacheErr) {
// Non-fatal — cache will expire naturally
console.log('Cache clear skipped:', cacheErr.message.substring(0, 100));
}
// ONLYOFFICE expects a JSON response acknowledging the save
return res.json({
error: 0,