Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxired/28c7ac781a24eef9e60eeef61bb654e9 to your computer and use it in GitHub Desktop.
Save maxired/28c7ac781a24eef9e60eeef61bb654e9 to your computer and use it in GitHub Desktop.
Strapi 3 custom middleware for partial content
{
"timeout": 100,
"load": {
"before": [
"responseTime",
"logger",
"cors",
"responses",
"gzip"
],
"order": [
"partial", "public"
],
"after": [
"parser",
"router"
]
}
}
const koaPart = require('koa-partial-content');
const path = require('path');
const basePath = __dirname + '../../../public';
const part = new koaPart(basePath);
module.exports = strapi => {
return {
// can also be async
initialize() {
strapi.app.use(async (ctx, next) => {
const absolutePath = path.join(basePath, ctx.path);
if (!part.isMedia(absolutePath)) {
return next();
}
try {
await part.sendResponse(ctx);
} catch (e) {
console.log(e);
return await next();
}
});
},
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment