-
Hi, I noticed that when my |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
The public directory isn't a real directory, it's a collection of routes created at build time here: it isn't really supposed to be used for persisted file storage, so the fix would be to use S3/DigitalOcean Spaces/the third party storage solution of your choice. |
Beta Was this translation helpful? Give feedback.
-
we must be able to refresh public routes, its so annoying. i cant be able to do basic file upload in next production |
Beta Was this translation helpful? Give feedback.
-
I made an api route for this purpose. Make a file on Now instead of src for Note: it will not work in subdirs inside of public. If you want that then make new api routes or modify the directory specified
|
Beta Was this translation helpful? Give feedback.
-
I created a new route in /api/assets/[...path].ts, which allows any directory depth, and one that also uses caching to reduce traffic and load. Now I can use urls like
|
Beta Was this translation helpful? Give feedback.
-
For app routers try this, a simplified version of @jasonmcaffee your api file structure should look like this import fs from 'fs';
import { NextResponse } from 'next/server';
export async function GET(req, { params }) {
const dir = params.dir.join("/");
if (!dir) {
return new NextResponse(null, { status: 500 });
}
// Prevent path traversal attacks
if (dir.indexOf('..') >= 0) {
return new NextResponse(null, { status: 400 });
}
try {
// Read and serve the file
const data = fs.readFileSync('public/' + dir,
{flag:'r'}
);
return new NextResponse(data,{status:200});
} catch (error) {
return new NextResponse(null, { status: 500 });
}
} Tags: NextJS App Router, JSX |
Beta Was this translation helpful? Give feedback.
The public directory isn't a real directory, it's a collection of routes created at build time here:
next.js/packages/next/next-server/server/next-server.ts
Line 775 in ec70096