Last active
March 3, 2020 21:38
-
-
Save maxired/28c7ac781a24eef9e60eeef61bb654e9 to your computer and use it in GitHub Desktop.
Strapi 3 custom middleware for partial content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"partial": { | |
"enabled": true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"timeout": 100, | |
"load": { | |
"before": [ | |
"responseTime", | |
"logger", | |
"cors", | |
"responses", | |
"gzip" | |
], | |
"order": [ | |
"partial", "public" | |
], | |
"after": [ | |
"parser", | |
"router" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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