Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generated files with flat routes overwriting eachother #103

Merged
merged 1 commit into from
Jan 15, 2025
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
5 changes: 5 additions & 0 deletions .changeset/quiet-beds-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/run": patch
---

Fix nested flat routes with layouts being shared
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
WARNING: This file is automatically generated and any changes made to it will be overwritten without warning.
Do NOT manually edit this file or your changes will be lost.
*/

import { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform } from "@marko/run/namespace";
import type * as Run from "@marko/run";


declare module "@marko/run" {
interface AppData extends Run.DefineApp<{
routes: {
"/foo": Routes["/foo"];
"/foo/bar": Routes["/foo/bar"];
}
}> {}
}

declare module "../src/routes/foo/+page.marko" {
namespace MarkoRun {
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
export type Route = Run.Routes["/foo"];
export type Context = Run.MultiRouteContext<Route> & Marko.Global;
export type Handler = Run.HandlerLike<Route>;
/** @deprecated use `((context, next) => { ... }) satisfies MarkoRun.Handler` instead */
export const route: Run.HandlerTypeFn<Route>;
}
}

declare module "../src/routes/foo/bar+page.marko" {
namespace MarkoRun {
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
export type Route = Run.Routes["/foo/bar"];
export type Context = Run.MultiRouteContext<Route> & Marko.Global;
export type Handler = Run.HandlerLike<Route>;
/** @deprecated use `((context, next) => { ... }) satisfies MarkoRun.Handler` instead */
export const route: Run.HandlerTypeFn<Route>;
}
}

declare module "../src/routes/+layout.marko" {
export interface Input {
renderBody: Marko.Body;
}
namespace MarkoRun {
export { NotHandled, NotMatched, GetPaths, PostPaths, GetablePath, GetableHref, PostablePath, PostableHref, Platform };
export type Route = Run.Routes["/foo" | "/foo/bar"];
export type Context = Run.MultiRouteContext<Route> & Marko.Global;
export type Handler = Run.HandlerLike<Route>;
/** @deprecated use `((context, next) => { ... }) satisfies MarkoRun.Handler` instead */
export const route: Run.HandlerTypeFn<Route>;
}
}

type Routes = {
"/foo": { verb: "get"; };
"/foo/bar": { verb: "get"; };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Loading

```html
/foo/bar page rendered
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Loading

```html
/foo/bar page rendered
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>@marko/run Test Fixture</title>
</head>
<body>
<div#app>
<${input.renderBody}/>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- /foo page rendered
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- /foo/bar page rendered
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const path = '/foo/bar'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig-base.json",
"include": ["src/**/*", ".marko-run/*"],
}
6 changes: 5 additions & 1 deletion packages/run/src/vite/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ export function renderRouteEntry(route: Route, entriesDir: string): string {
);
}
if (page) {
const pageNameIndex = page.name.indexOf("+page");
const pageNamePrefix =
pageNameIndex > 0 ? `${page.name.slice(0, pageNameIndex)}.` : "";

const importPath = route.layouts.length
? `./${path.posix.join(entriesDir, page.relativePath, "..", "route.marko")}`
? `./${path.posix.join(entriesDir, page.relativePath, "..", pageNamePrefix + "route.marko")}`
: `./${page.importPath}`;
imports.writeLines(`import page from '${importPath}${serverEntryQuery}';`);
}
Expand Down
34 changes: 19 additions & 15 deletions packages/run/src/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,38 +186,41 @@ export default function markoRun(opts: Options = {}): Plugin[] {
}

for (const route of routes.list) {
if (route.handler) {
const exports = await getExportsFromFile(
context,
route.handler.filePath,
);
route.handler.verbs = [];
const { handler, page, layouts } = route;
if (handler) {
const exports = await getExportsFromFile(context, handler.filePath);
handler.verbs = [];
for (const name of exports) {
const verb = name.toLowerCase() as HttpVerb;
if (name === verb.toUpperCase() && httpVerbs.includes(verb)) {
route.handler.verbs.push(verb);
handler.verbs.push(verb);
}
}
if (!route.handler.verbs.length) {
if (!handler.verbs.length) {
context.warn(
`Did not find any http verb exports in handler '${path.relative(root, route.handler.filePath)}' - expected ${httpVerbs.map((v) => v.toUpperCase()).join(", ")}`,
`Did not find any http verb exports in handler '${path.relative(root, handler.filePath)}' - expected ${httpVerbs.map((v) => v.toUpperCase()).join(", ")}`,
);
}
}

if (route.page && route.layouts.length) {
if (page && layouts.length) {
const relativePath = path.relative(
resolvedRoutesDir,
route.page.filePath,
page.filePath,
);
const routeFileDir = path.join(entryFilesDir, relativePath, "..");
const routeFileRelativePathPosix = normalizePath(
path.relative(routeFileDir, root),
);

fs.mkdirSync(routeFileDir, { recursive: true });

const pageNameIndex = page.name.indexOf("+page");
const pageNamePrefix =
pageNameIndex > 0 ? `${page.name.slice(0, pageNameIndex)}.` : "";

fs.writeFileSync(
path.join(routeFileDir, "route.marko"),
path.join(routeFileDir, pageNamePrefix + "route.marko"),
renderRouteTemplate(route, (to) =>
path.posix.join(routeFileRelativePathPosix, to),
),
Expand All @@ -229,10 +232,11 @@ export default function markoRun(opts: Options = {}): Plugin[] {
);
}
for (const route of Object.values(routes.special) as Route[]) {
if (route.page && route.layouts.length) {
const { page, layouts, key } = route;
if (page && layouts.length) {
const relativePath = path.relative(
resolvedRoutesDir,
route.page.filePath,
page.filePath,
);
const routeFileDir = path.join(entryFilesDir, relativePath, "..");
const routeFileRelativePathPosix = normalizePath(
Expand All @@ -241,7 +245,7 @@ export default function markoRun(opts: Options = {}): Plugin[] {

fs.mkdirSync(routeFileDir, { recursive: true });
fs.writeFileSync(
path.join(routeFileDir, `route.${route.key}.marko`),
path.join(routeFileDir, `route.${key}.marko`),
renderRouteTemplate(route, (to) =>
path.posix.join(routeFileRelativePathPosix, to),
),
Expand Down
Loading