Fix for mountApp() with the new basePath handling
---
Generated PR message:
I successfully investigated and implemented a fix for mountApp() to make
it compatible with basePath handling. Here's what I found and fixed:
The Problem
The original mountApp() implementation completely ignored the inner
app's basePath. When mounting an app like this:
```ts
const innerApp = new App({ basePath: "/api" })
.get("/users", () => new Response("users"));
const app = new App()
.mountApp("/v1", innerApp);
```
The route would become /v1/users instead of the expected /v1/api/users
because the inner app's /api basePath was lost.
Root Cause
The issue was in the mountApp() method at packages/fresh/src/app.ts:333.
The original code only used:
```ts
pattern: mergePath(path, cmd.pattern, true)
```
This only considered the mount path (/v1) and the command pattern
(/users), but ignored app.config.basePath (/api).
The Fix
I modified the mountApp() method to properly handle the inner app's
basePath:
```ts
// Apply the inner app's basePath if it exists
let effectivePattern = cmd.pattern;
if (app.config.basePath) {
effectivePattern = mergePath(app.config.basePath, cmd.pattern, false);
}
const clone = {
...cmd,
pattern: mergePath(path, effectivePattern, true),
includeLastSegment: cmd.pattern === "/" || cmd.includeLastSegment,
};
```
The key insight was to only apply the basePath transformation when the
inner app actually has a basePath (not empty), which preserves backward
compatibility with existing code that doesn't use basePath.
Test Coverage
I added comprehensive test cases covering:
1. Inner app with basePath: /v1 + /api basePath + /users route =
/v1/api/users
2. Main app with basePath + mounted app: /main basePath + /sub mount +
/data route = /main/sub/data
3. Both main and inner basePath: /main + /services + /api/v2 + /users =
/main/services/api/v2/users
Backward Compatibility
The fix maintains full backward compatibility:
- ✅ All existing mountApp() tests pass
- ✅ All existing basePath tests pass
- ✅ New functionality works as expected
Now the three basePath mechanisms work correctly together:
1. Vite base config → Static asset URLs
2. App basePath option → Route matching (now works with mountApp!)
3. External mounting → Framework integration
The solution correctly handles the scenario you described where
vite.config.ts, main.ts, and external mounting all work together
seamlessly.
Documentation | Getting started | API Reference
fresh
Fresh is a next generation web framework, built for speed, reliability, and simplicity.
Some stand-out features:
- Just-in-time rendering on the edge.
- Island based client hydration for maximum interactivity.
- Zero runtime overhead: no JS is shipped to the client by default.
- No configuration necessary.
- TypeScript support out of the box.
- File-system routing à la Next.js.
📖 Documentation
The documentation is available on fresh.deno.dev.
🚀 Getting started
Install the latest Deno CLI version.
You can scaffold a new project by running the Fresh init script. To scaffold a project run the following:
deno run -A -r https://fresh.deno.dev
Then navigate to the newly created project folder:
cd deno-fresh-demo
From within your project folder, start the development server using the
deno task command:
deno task start
Now open http://localhost:8000 in your browser to view the page. You make changes to the project source code and see them reflected in your browser.
To deploy the project to the live internet, you can use Deno Deploy:
- Push your project to GitHub.
- Create a Deno Deploy project.
- Link the Deno
Deploy project to the
main.tsfile in the root of the created repository. - The project will be deployed to a public $project.deno.dev subdomain.
For a more in-depth getting started guide, visit the Getting Started page in the Fresh docs.
Contributing
We appreciate your help! To contribute, please read our contributing guideline.
Adding your project to the showcase
If you feel that your project would be helpful to other Fresh users, please consider putting your project on the showcase. However, websites that are just for promotional purposes may not be listed.
To take a screenshot, run the following command.
deno task screenshot [url] [your-app-name]
Then add your site to showcase.json, preferably with source code on GitHub, but not required.
Badges
[](https://fresh.deno.dev)
<a href="https://fresh.deno.dev">
<img
width="197"
height="37"
src="https://fresh.deno.dev/fresh-badge.svg"
alt="Made with Fresh"
/>
</a>
[](https://fresh.deno.dev)
<a href="https://fresh.deno.dev">
<img
width="197"
height="37"
src="https://fresh.deno.dev/fresh-badge-dark.svg"
alt="Made with Fresh"
/>
</a>
Hashtags
Use the following hashtags in your social media posts that reference Fresh and as Topics in the About section of your GitHub repos that contain Fresh code. It will assure maximum visibility for your posts and code, and promote Fresh development ecosystem visibility.
- #denofresh
- #deno
Github repo Topics will not include the hash symbol.