Skip to content

docs: add more details for webpack config #4677

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions docs/router/framework/react/routing/installation-with-webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
title: Installation with Webpack
---

The following configuration ensures that your dev server works seamlessly with client-side routing:

- `historyApiFallback.rewrites`: Redirects all requests to `index.html`, allowing server can correctly serve the app for any route.
- `output.publicPath`: Ensures that the web app can always get the correct path to the assets, even when the app url is with a subpath, for example, `/foo/bar`.

```ts
export default {
devServer: {
historyApiFallback: {
rewrites: [{ from: /./, to: '/index.html' }],
},
},
output: {
publicPath: '/',
},
}
```

[//]: # 'BundlerConfiguration'

To use file-based routing with **Webpack**, you'll need to install the `@tanstack/router-plugin` package.
Expand Down
42 changes: 39 additions & 3 deletions examples/react/quickstart-webpack-file-based/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import { Route as rootRouteImport } from './routes/__root'
import { Route as AboutRouteImport } from './routes/about'
import { Route as IndexRouteImport } from './routes/index'
import { Route as NestIndexRouteImport } from './routes/nest/index'
import { Route as NestFooRouteImport } from './routes/nest/foo'

const AboutRoute = AboutRouteImport.update({
id: '/about',
Expand All @@ -22,31 +24,49 @@ const IndexRoute = IndexRouteImport.update({
path: '/',
getParentRoute: () => rootRouteImport,
} as any)
const NestIndexRoute = NestIndexRouteImport.update({
id: '/nest/',
path: '/nest/',
getParentRoute: () => rootRouteImport,
} as any)
const NestFooRoute = NestFooRouteImport.update({
id: '/nest/foo',
path: '/nest/foo',
getParentRoute: () => rootRouteImport,
} as any)

export interface FileRoutesByFullPath {
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/nest/foo': typeof NestFooRoute
'/nest': typeof NestIndexRoute
}
export interface FileRoutesByTo {
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/nest/foo': typeof NestFooRoute
'/nest': typeof NestIndexRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
'/': typeof IndexRoute
'/about': typeof AboutRoute
'/nest/foo': typeof NestFooRoute
'/nest/': typeof NestIndexRoute
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '/about'
fullPaths: '/' | '/about' | '/nest/foo' | '/nest'
fileRoutesByTo: FileRoutesByTo
to: '/' | '/about'
id: '__root__' | '/' | '/about'
to: '/' | '/about' | '/nest/foo' | '/nest'
id: '__root__' | '/' | '/about' | '/nest/foo' | '/nest/'
fileRoutesById: FileRoutesById
}
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
AboutRoute: typeof AboutRoute
NestFooRoute: typeof NestFooRoute
NestIndexRoute: typeof NestIndexRoute
}

declare module '@tanstack/react-router' {
Expand All @@ -65,12 +85,28 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof IndexRouteImport
parentRoute: typeof rootRouteImport
}
'/nest/': {
id: '/nest/'
path: '/nest'
fullPath: '/nest'
preLoaderRoute: typeof NestIndexRouteImport
parentRoute: typeof rootRouteImport
}
'/nest/foo': {
id: '/nest/foo'
path: '/nest/foo'
fullPath: '/nest/foo'
preLoaderRoute: typeof NestFooRouteImport
parentRoute: typeof rootRouteImport
}
}
}

const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
AboutRoute: AboutRoute,
NestFooRoute: NestFooRoute,
NestIndexRoute: NestIndexRoute,
}
export const routeTree = rootRouteImport
._addFileChildren(rootRouteChildren)
Expand Down
16 changes: 16 additions & 0 deletions examples/react/quickstart-webpack-file-based/src/routes/__root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ function RootComponent() {
>
About
</Link>
<Link
to="/nest"
activeProps={{
className: 'font-bold',
}}
>
Nest
</Link>
<Link
to="/nest/foo"
activeProps={{
className: 'font-bold',
}}
>
Nest/Foo
</Link>
</div>
<hr />
<Outlet />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/nest/foo')({
component: RouteComponent,
})

function RouteComponent() {
return <div>nest/foo</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/nest/')({
component: RouteComponent,
})

function RouteComponent() {
return <div>Nest</div>
}