Files
directus/api/src/controllers/extensions.ts
Nicola Krumschmidt ba2f8750e9 Set no-cache header on extension sources (#9186)
This should hopefully fix caching issues some users were experiencing.
2021-10-27 12:31:57 -04:00

57 lines
1.4 KiB
TypeScript

import { Router } from 'express';
import asyncHandler from '../utils/async-handler';
import { RouteNotFoundException } from '../exceptions';
import { getExtensionManager } from '../extensions';
import { respond } from '../middleware/respond';
import { depluralize, isAppExtension } from '@directus/shared/utils';
import { Plural } from '@directus/shared/types';
const router = Router();
router.get(
'/:type',
asyncHandler(async (req, res, next) => {
const type = depluralize(req.params.type as Plural<string>);
if (!isAppExtension(type)) {
throw new RouteNotFoundException(req.path);
}
const extensionManager = getExtensionManager();
const extensions = extensionManager.listExtensions(type);
res.locals.payload = {
data: extensions,
};
return next();
}),
respond
);
router.get(
'/:type/index.js',
asyncHandler(async (req, res) => {
const type = depluralize(req.params.type as Plural<string>);
if (!isAppExtension(type)) {
throw new RouteNotFoundException(req.path);
}
const extensionManager = getExtensionManager();
const extensionSource = extensionManager.getAppExtensions(type);
if (extensionSource === undefined) {
throw new RouteNotFoundException(req.path);
}
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Vary', 'Origin, Cache-Control');
res.end(extensionSource);
})
);
export default router;