Skip to content

Commit 4fe0d72

Browse files
authored
fix: support async functions as static parameters input in file-system based router (#86)
Adds support for async functions as static parameters input in the file-system based router. ```ts // [id].static.ts export default async () => [{ id: 1 }, { id: 2 }, { id: 3 }]; ``` #81
1 parent 61cbefd commit 4fe0d72

File tree

1 file changed

+7
-11
lines changed
  • packages/react-server/lib/plugins/file-router

1 file changed

+7
-11
lines changed

packages/react-server/lib/plugins/file-router/plugin.mjs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,10 @@ export default function viteReactServerRouter(options = {}) {
771771
config.build.rollupOptions.input[join("static", hash)] =
772772
staticSrc;
773773
paths.push(async () => {
774-
const staticPaths = (await import(exportEntry)).default;
774+
let staticPaths = (await import(exportEntry)).default;
775+
if (typeof staticPaths === "function") {
776+
staticPaths = await staticPaths();
777+
}
775778
if (typeof staticPaths === "boolean" && staticPaths) {
776779
if (/\[[^\]]+\]/.test(path)) {
777780
throw new Error(
@@ -782,23 +785,16 @@ export default function viteReactServerRouter(options = {}) {
782785
}
783786
return { path };
784787
}
785-
if (typeof staticPaths === "function") {
786-
return await staticPaths();
787-
}
788788
const validPaths = await Promise.all(
789789
staticPaths.map(async (def) => {
790790
let obj = def;
791791
if (typeof def === "function") {
792792
obj = await def();
793793
}
794-
try {
795-
return { path: applyParamsToPath(path, obj) };
796-
} catch (e) {
797-
if (typeof obj.path === "string") {
798-
return { path: obj.path };
799-
}
800-
throw e;
794+
if (typeof obj.path === "string") {
795+
return { path: obj.path };
801796
}
797+
return { path: applyParamsToPath(path, obj) };
802798
})
803799
);
804800
return validPaths;

0 commit comments

Comments
 (0)