Custom upload endpoints #9670
cbratschi
started this conversation in
Feature Requests & Ideas
Replies: 1 comment
-
Well, ended with a rewrite rule in next.config.ts: /**
* Change rewrite rules.
*
* See https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites.
*
* - redirects upload access to Payload endpoint.
*
* @returns
*/
async rewrites() {
const collection = 'media';
const rewrites: Rewrite[] = [{
destination: `${process.env.NEXT_PUBLIC_SERVER_URL}/cms/api/${collection}/file/:path*`,
source: `/media/:path*`
}];
//debug
//console.dir(rewrites);
return {
beforeFiles: rewrites,
afterFiles: [],
fallback: []
};
}, |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Payload 3.x uses the API routes to deliver uploads whereas in Payload 2.x we used /media/ redirects to the Payload Express server. In my case apiRoute is "/cms/api" and uploads would be delivered from:
<base URL>/cms/api/<collection slug>/file/<filename>
Our storage implementation uses GridFS to store the files in MongoDB. Access to those upload URLs are cached by NGINX. Therefore we need a separate endpoint to distinguish between API access and file access. In addition, the upload URLs should be shorter:
<base URL>/<collection slug>/<filename>
I tried to add /src/app/(payload)/media/[...paths]/route.ts but several needed functions are not exported by @payload/next:
getFile() would be the correct API and routeError() to handle errors. Could those two functions be exported?
Beta Was this translation helpful? Give feedback.
All reactions