Skip to content
This repository was archived by the owner on Oct 11, 2026. It is now read-only.
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
24 changes: 24 additions & 0 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ A route module can export:
| `shouldRevalidate` | Function | Decides if the loader reruns (client `.tsx` only) |
| `beforeHydrate` | Function | Runs before React hydrates (root `main.tsx` only) |

Server `.ts` files can expose `loader` and `action` through direct declarations,
local named exports (including aliases), or explicit named re-exports:

```ts
function load() {
return { message: "Ready" };
}
function submit() {
return { saved: true };
}
export { load as loader, submit as action };
```

You can also re-export named handlers from a shared server module:

```ts
export { load as loader, submit as action } from "./_handlers.ts";
```

The builder reads these value exports without evaluating server modules.
Type-only declarations and exports do not advertise handlers. Use explicit named
exports: wildcard and namespace re-exports are not followed. Browser middleware
retains its [`export const middleware` convention](middleware.md).

Export `publicEnvKeys` from the root **server** module, `routes/main.ts`, to
allowlist additional environment values in hydration data. See
[configuration](configuration.md#public-environment-variables).
Expand Down
84 changes: 73 additions & 11 deletions src/_build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sortBy } from "@std/collections/sort-by";
import { walk } from "@std/fs";
import * as path from "@std/path";
import * as babel from "@babel/core";

export interface ServerFlags {
loader?: boolean;
Expand Down Expand Up @@ -106,19 +107,80 @@ export async function getServerFlags(
): Promise<ServerFlags | undefined> {
try {
const content = await Deno.readTextFile(absoluteFilePath);
const hasLoader = /export\s+(async\s+)?function\s+loader\b/.test(content) ||
/export\s+const\s+loader(\s|=|:)/.test(content);
const hasAction = /export\s+(async\s+)?function\s+action\b/.test(content) ||
/export\s+const\s+action(\s|=|:)/.test(content);

if (hasLoader || hasAction) {
const flags: ServerFlags = {};
if (hasLoader) flags.loader = true;
if (hasAction) flags.action = true;
return flags;
const ast = babel.parseSync(content, {
filename: absoluteFilePath,
babelrc: false,
configFile: false,
sourceType: "module",
parserOpts: { plugins: ["typescript", "decorators"] },
});
if (!ast) return undefined;
const values = new Set<string>();
const exported = new Set<string>();
for (const statement of ast.program.body) {
const declaration = babel.types.isExportNamedDeclaration(statement)
? statement.declaration
: statement;
if (babel.types.isImportDeclaration(declaration)) {
if (declaration.importKind === "type") continue;
for (const specifier of declaration.specifiers) {
if (
babel.types.isImportSpecifier(specifier) &&
specifier.importKind === "type"
) continue;
values.add(specifier.local.name);
}
} else if (
babel.types.isVariableDeclaration(declaration) && !declaration.declare
) {
for (const binding of declaration.declarations) {
for (
const name of Object.keys(
babel.types.getBindingIdentifiers(binding.id),
)
) {
values.add(name);
if (babel.types.isExportNamedDeclaration(statement)) {
exported.add(name);
}
}
}
} else if (
(babel.types.isFunctionDeclaration(declaration) ||
babel.types.isClassDeclaration(declaration) ||
babel.types.isTSEnumDeclaration(declaration)) &&
!declaration.declare && babel.types.isIdentifier(declaration.id)
) {
values.add(declaration.id.name);
if (babel.types.isExportNamedDeclaration(statement)) {
exported.add(declaration.id.name);
}
}
}
for (const statement of ast.program.body) {
if (
!babel.types.isExportNamedDeclaration(statement) ||
statement.exportKind === "type"
) continue;
for (const specifier of statement.specifiers) {
if (
!babel.types.isExportSpecifier(specifier) ||
specifier.exportKind === "type" ||
(!statement.source && !values.has(specifier.local.name))
) continue;
exported.add(
babel.types.isIdentifier(specifier.exported)
? specifier.exported.name
: specifier.exported.value,
);
}
}
const flags: ServerFlags = {};
if (exported.has("loader")) flags.loader = true;
if (exported.has("action")) flags.action = true;
if (flags.loader || flags.action) return flags;
} catch {
// skip
return undefined;
}
return undefined;
}
Expand Down
Loading
Loading