boolean | HistoryApiFallbackOptions
false
historyApiFallback
is used to support routing based on the history API. When a user visits a path that does not exist, it will automatically return a specified HTML file to avoid a 404 error.
When Rsbuild's default page routing behavior cannot meet your needs, for example, if you want to be able to access main.html
when accessing /
, you can achieve this through the server.historyApiFallback
configuration.
When server.historyApiFallback
is set to true
, all HTML GET requests that do not match an actual resource will return index.html
. This ensures that routing in single-page applications works correctly.
export default {
server: {
historyApiFallback: true,
},
};
Rsbuild will change the requested location to the index you specify whenever there is a request which fulfills the following criteria:
GET
or HEAD
requesttext/html
.
(dot), meaning it is not a direct file requestserver.historyApiFallback
also supports passing an object to customize its behavior.
string
'index.html'
By setting historyApiFallback.index
to main.html
, when accessing the root path /
or other routes that may result in a 404, the page will automatically redirect to main.html
.
export default {
source: {
entry: {
main: './src/index.ts',
},
},
server: {
htmlFallback: false,
historyApiFallback: {
index: '/main.html',
},
},
};
type Rewrites = Array<{
from: RegExp;
to: string | ((context: HistoryApiFallbackContext) => string);
}>;
[]
When your application contains multiple entries, you may need to redirect different paths to different pages. In this case, you can configure more flexible redirection rules through the rewrites
option:
export default {
server: {
historyApiFallback: {
rewrites: [
{ from: /^\/$/, to: '/views/landing.html' },
{ from: /^\/subpage/, to: '/views/subpage.html' },
{ from: /./, to: '/views/404.html' },
],
},
},
};
string[]
['text/html', '*/*']
Override the default Accepts:
headers that are queried when matching HTML content requests.
export default {
server: {
historyApiFallback: {
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
},
},
};
boolean
false
By default, requests containing a dot (.
) in the path are treated as direct file requests and are not redirected.
Setting disableDotRule
to true
will disable this behavior and allow such requests to be redirected as well.
export default {
server: {
historyApiFallback: {
disableDotRule: true,
},
},
};