File-Based Routing

Flat files in src/app directly create URL routes without needing a folder.

Overview

Bini.js supports file-based routing - a file directly inside src/app/ becomes a route. No folder needed. For example about.tsx creates /about.

This is different from folder-based routing where you need about/page.tsx. File-based is faster for single pages.

File PathURLCreates URL?
app/about.tsx/aboutYes - flat file creates URL
app/contact.tsx/contactYes - flat file creates URL
app/blog.tsx/blogYes - flat file creates URL
app/page.tsx/Yes - root file creates URL

Basic File Routing

A file in src/app directly maps to a URL. The file must export a default React component.

app
page.tsx
about.tsx
contact.tsx
pricing.tsx
blog.tsx
/Routable
/aboutRoutable
/contactRoutable
/pricingRoutable
/blogRoutable
app/about.tsx
export default function AboutPage() {
  return <h1>About Us</h1>
}
app/contact.tsx
export default function ContactPage() {
  return <h1>Contact Us</h1>
}
File-based routing creates a URL directly from the file name. No folder with page.tsx needed.

Flat Files vs Folders

Both achieve the same URL, but file-based is shorter for simple pages.

ApproachFile PathURLCreates URL?
File-basedapp/about.tsx/aboutYes - file creates URL
Folder-basedapp/about/page.tsx/aboutYes - folder + page creates URL
File-basedapp/dashboard.tsx/dashboardYes - file creates URL
Folder-basedapp/dashboard/page.tsx/dashboardYes - folder + page creates URL

File-based

app
about.tsx
contact.tsx
/aboutRoutable
/contactRoutable

Folder-based

app
about
page.tsx
contact
page.tsx
/aboutRoutable
/contactRoutable
Use file-based for simple single pages. Use folder-based when you need co-located layouts, loading, or private folders like _components inside the route.

Index Files

index.tsx inside a folder maps to that folder's URL. This is file-based routing inside a folder.

app
dashboard
index.tsx
settings
index.tsx
blog
index.tsx
authors
index.tsx
page.tsx
/dashboardRoutable
/dashboard/settingsRoutable
/blogRoutable
/blog/authorsRoutable
/Routable
File PathURLCreates URL?
app/dashboard/index.tsx/dashboardYes - index creates URL
app/blog/index.tsx/blogYes - index creates URL
app/blog/authors/index.tsx/blog/authorsYes - nested index creates URL
index.tsx is equivalent to page.tsx inside that folder - both create the same URL. Choose one pattern and stick to it.

File Extensions & Priority

Supported extensions for file-based routes and their priority when the same name exists.

.tsx > .jsx > .ts > .js > .mdx > .md
app
about.tsx
about.jsx
about.mdx
blog.tsx
contact.md
/aboutRoutable
(ignored)Not Routable
(ignored)Not Routable
/blogRoutable
/contactRoutable
ExtensionCreates URL?Description
.tsxYesHighest priority - creates URL
.jsxYesSecond priority - creates URL if no .tsx
.tsYesThird priority
.jsYesFourth priority
.mdxYesMDX page - creates URL
.mdYesMarkdown page - creates URL

Reserved Names

These file names are never treated as flat file routes - they are special files for structure, not routes.

File NameCreates URL?Reason
page.tsxYes - but via folderSpecial - defines folder route
layout.tsxNoSpecial file - layout, not route
loading.tsxNoSpecial file - loading UI
error.tsxNoSpecial file - error UI
not-found.tsxNoSpecial file - 404 UI
template.tsxNoSpecial file - template
default.tsxNoSpecial file - parallel route fallback
global-error.tsxNoSpecial file - global error
app
about.tsx
contact.tsx
layout.tsx
loading.tsx
page.tsx
dashboard.tsx
/aboutRoutable
/contactRoutable
-Not Routable
-Not Routable
/Routable
/dashboardRoutable
Only regular file names like about.tsx, contact.tsx create URLs. Reserved names like layout.tsx, loading.tsx, error.tsx do not create URLs - they are special files.

Complete Example

File-based routing only - flat files that create URLs without folders.

app
page.tsx
about.tsx
contact.tsx
pricing.tsx
blog.tsx
dashboard.tsx
faq.tsx
dashboard
index.tsx
settings.tsx
blog
index.tsx
layout.tsx
/Routable
/aboutRoutable
/contactRoutable
/pricingRoutable
/blogRoutable
/dashboardRoutable
/faqRoutable
/dashboardRoutable
/dashboard/settingsRoutable
/blogRoutable
-Not Routable
File PathURLCreates URL?
app/page.tsx/Yes - creates URL
app/about.tsx/aboutYes - file based creates URL
app/contact.tsx/contactYes - file based creates URL
app/blog.tsx/blogYes - file based creates URL
app/dashboard/index.tsx/dashboardYes - index file creates URL
app/dashboard/settings.tsx/dashboard/settingsYes - nested file creates URL
app/layout.tsx-No - reserved name, no URL
app/_components/Header.tsx-No - private folder, no URL
File-based routing: flat files like about.tsx create URLs directly. Folder + page.tsx also creates URLs but is folder-based. Reserved names like layout.tsx never create URLs. Each valid file creates a URL.
Was this helpful?