Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/yummy-apples-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/plugin-data-loader': patch
---

fix: static resource requests should not be valid loader requests.
fix: 静态资源请求不应该是合法的 loader 请求
19 changes: 19 additions & 0 deletions packages/cli/plugin-data-loader/src/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ function convertModernRedirectResponse(headers: Headers, basename: string) {
});
}

export function hasFileExtension(pathname: string): boolean {
const lastSegment = pathname.split('/').pop() || '';
const dotIndex = lastSegment.lastIndexOf('.');

if (dotIndex === -1) {
return false;
}

const extension = lastSegment.substring(dotIndex).toLowerCase();
return extension !== '.html';
}

export const handleRequest: ServerLoaderBundle['handleRequest'] = async ({
request,
serverRoutes,
Expand All @@ -61,6 +73,13 @@ export const handleRequest: ServerLoaderBundle['handleRequest'] = async ({
}): Promise<Response | void> => {
const url = new URL(request.url);
const routeId = url.searchParams.get(LOADER_ID_PARAM) as string;

// Check if pathname has file extension (excluding .html)
// Reject requests like /three/user/profile.js but allow /three/user/profile
if (hasFileExtension(url.pathname)) {
return;
}

const entry = matchEntry(url.pathname, serverRoutes);
// LOADER_ID_PARAM is the indicator for CSR data loader request.
if (!routeId || !entry) {
Expand Down