fix: ONLYOFFICE cache-busting - use file mtime in doc key + cache-buster param on download URL
This commit is contained in:
Binary file not shown.
@@ -5,6 +5,7 @@
|
||||
"filename": "433a0593-d3cf-414e-8763-ab5f87cbc75c.docx",
|
||||
"originalName": "master_cv_template.docx",
|
||||
"path": "/root/workspace/cv-app/renderer/carbone-templates/433a0593-d3cf-414e-8763-ab5f87cbc75c.docx",
|
||||
"uploadedAt": "2026-07-16T09:03:29.601Z"
|
||||
"uploadedAt": "2026-07-16T09:03:29.601Z",
|
||||
"updatedAt": "2026-07-16T11:09:51.737Z"
|
||||
}
|
||||
]
|
||||
@@ -109,8 +109,12 @@ function verifyToken(token) {
|
||||
* ONLYOFFICE uses this to identify document sessions and for caching.
|
||||
* Must be unique per document + version (so re-edits get fresh sessions).
|
||||
*/
|
||||
function generateDocKey(templateId) {
|
||||
return `cvtemplate_${templateId}_${Date.now()}_${crypto.randomBytes(4).toString('hex')}`;
|
||||
function generateDocKey(templateId, fileMtime) {
|
||||
// Include the file's modification time in the key so that
|
||||
// when the file is updated via callback, the next edit session
|
||||
// gets a completely new key and ONLYOFFICE fetches the fresh file
|
||||
const mtime = fileMtime || Date.now();
|
||||
return `cvtemplate_${templateId}_${mtime}_${crypto.randomBytes(4).toString('hex')}`;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
@@ -131,14 +135,16 @@ router.get('/:templateId/config', (req, res) => {
|
||||
return res.status(404).json({ error: 'Template file missing' });
|
||||
}
|
||||
|
||||
const docKey = generateDocKey(templateId);
|
||||
// Get file modification time for cache-busting doc key
|
||||
const fileStats = fs.statSync(template.path);
|
||||
const fileMtime = fileStats.mtimeMs;
|
||||
const docKey = generateDocKey(templateId, fileMtime);
|
||||
const fileName = template.originalName || template.name + '.docx';
|
||||
|
||||
// Build the editor config
|
||||
// The document URL must be accessible from ONLYOFFICE's perspective.
|
||||
// In Docker: http://cv-backend:8771/api/onlyoffice/<id>/download
|
||||
// In dev: http://<host>:8771/api/onlyoffice/<id>/download
|
||||
const documentUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/download`;
|
||||
// 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}`;
|
||||
const callbackUrl = `${BACKEND_PUBLIC_URL}/api/onlyoffice/${templateId}/callback`;
|
||||
|
||||
const config = {
|
||||
|
||||
Reference in New Issue
Block a user