Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/evershop/src/lib/middlewares/publicStatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ export default async function publicStatic(request, response, next) {
// Get the request path
const { path } = request;
try {
// If no file extension, skip to next middleware early
if (!path.includes('.')) {
throw new Error('No file extension');
return next();
}
// Asynchoronously check if the path is a file and exists in the public folder
const test = await fs.stat(join(CONSTANTS.ROOTPATH, 'public', path));
if (test.isFile()) {

// Use promises API instead of callback-based fs.stat
const { promises: fsp } = fs;
const filePath = join(CONSTANTS.ROOTPATH, 'public', path);
const stat = await fsp.stat(filePath);

if (stat.isFile()) {
// If it is a file, serve it
staticMiddleware(join(CONSTANTS.ROOTPATH, 'public'))(
return staticMiddleware(join(CONSTANTS.ROOTPATH, 'public'))(
request,
response,
next
);
}

// Not a file, continue
return next();
} catch (e) {
// If the path is not a file or does not exist in the public folder, call next
next();
return next();
}
}