Skip to content

Commit

Permalink
chore: add troubleshoot option to force cache flush (#30819)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok authored Oct 31, 2023
1 parent bd9d2b7 commit 180d400
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
20 changes: 12 additions & 8 deletions apps/meteor/app/cors/server/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,24 @@ declare module 'meteor/webapp' {
}
}

// These routes already handle cache control on their own
const cacheControlledRoutes: Array<RegExp> = ['/assets', '/custom-sounds', '/emoji-custom', '/avatar', '/file-upload'].map(
(route) => new RegExp(`^${route}`, 'i'),
);
let cachingVersion = '';
settings.watch<string>('Troubleshoot_Force_Caching_Version', (value) => {
cachingVersion = String(value).trim();
});

// @ts-expect-error - accessing internal property of webapp
WebAppInternals.staticFilesMiddleware = function (
staticFiles: StaticFiles,
req: http.IncomingMessage,
res: http.ServerResponse,
req: http.IncomingMessage & { cookies: Record<string, string> },
res: http.ServerResponse & { cookie: (cookie: string, value: string) => void },
next: NextFunction,
) {
res.setHeader('Access-Control-Allow-Origin', '*');
const { arch, path, url } = WebApp.categorizeRequest(req);

if (Meteor.isProduction && path.match(/\.(js|css|map)$/i) && !cacheControlledRoutes.some((regexp) => regexp.test(path))) {
res.setHeader('Cache-Control', 'public, max-age=31536000');
if (cachingVersion && req.cookies.cache_version !== cachingVersion) {
res.cookie('cache_version', cachingVersion);
res.setHeader('Clear-Site-Data', '"cache"');
}

// Prevent meteor_runtime_config.js to load from a different expected hash possibly causing
Expand All @@ -126,7 +127,10 @@ WebAppInternals.staticFilesMiddleware = function (
res.writeHead(404);
return res.end();
}

res.setHeader('Cache-Control', 'public, max-age=3600');
}

return _staticFilesMiddleware(staticFiles, req, res, next);
};

Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -5230,6 +5230,8 @@
"Troubleshoot_Disable_Sessions_Monitor_Alert": "This setting stops the processing of user sessions causing the statistics to stop working correctly!",
"Troubleshoot_Disable_Teams_Mention": "Disable Teams mention",
"Troubleshoot_Disable_Teams_Mention_Alert": "This setting disables the teams mention feature. User's won't be able to mention a Team by name in a message and get its members notified.",
"Troubleshoot_Force_Caching_Version": "Force browsers to clear networking cache based on version change",
"Troubleshoot_Force_Caching_Version_Alert": "If the value provided is not empty and different from previous one the browsers will try to clear the cache. This setting should not be set for a long period since it affects the browser performance, please clear it as soon as possible.",
"True": "True",
"Try_now": "Try now",
"Try_searching_in_the_marketplace_instead": "Try searching in the Marketplace instead",
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/server/settings/troubleshoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ export const createTroubleshootSettings = () =>
hidden: true,
readonly: true,
});

await this.add('Troubleshoot_Force_Caching_Version', '', {
type: 'string',
i18nDescription: 'Troubleshoot_Force_Caching_Version_Alert',
});
});

0 comments on commit 180d400

Please sign in to comment.