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

feat: Add historyApiFallback option #930

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ The public path that the middleware is bound to.

_Best Practice: use the same `publicPath` defined in your webpack config. For more information about `publicPath`, please see [the webpack documentation](https://webpack.js.org/guides/public-path)._

### historyApiFallback

type: `Boolean`
Default: `false`

When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 responses. Enable historyApiFallback by setting it to true

### stats

Type: `Boolean|String|Object`
Expand Down
4 changes: 4 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
}
]
},
"historyApiFallback": {
"description": "When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 responses. Enable historyApiFallback by setting it to true",
"type": "boolean"
},
"stats": {
"description": "Stats options object or preset name.",
"link": "https://github.com/webpack/webpack-dev-middleware#stats",
Expand Down
8 changes: 7 additions & 1 deletion src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ function getFilenameFromUrl(context, url) {
filename = path.join(outputPath, querystring.unescape(pathname));
}

let fsStats;
if (
options.historyApiFallback &&
!context.outputFileSystem.existsSync(filename)
) {
filename = path.join(outputPath);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When pathname is empty, with this option we will also try find index.html at correct folder

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-akait This PR.


let fsStats;
try {
fsStats =
/** @type {import("fs").statSync} */
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/validation-options.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ exports[`validation should throw an error on the "headers" option with "true" va
* options.headers should be an instance of function."
`;

exports[`validation should throw an error on the "historyApiFallback" option with "10" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.historyApiFallback should be a boolean.
-> When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 responses. Enable historyApiFallback by setting it to true"
`;

exports[`validation should throw an error on the "historyApiFallback" option with "foo" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.historyApiFallback should be a boolean.
-> When using the HTML5 History API, the index.html page will likely have to be served in place of any 404 responses. Enable historyApiFallback by setting it to true"
`;

exports[`validation should throw an error on the "index" option with "{}" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.index should be one of these:
Expand Down
21 changes: 21 additions & 0 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3040,6 +3040,27 @@ describe.each([
});
});

describe("historyApiFallback option", () => {
describe("index.html page will likely have to be served in place of any 404 responses", () => {
beforeAll((done) => {
const compiler = getCompiler(webpackConfig);

instance = middleware(compiler, { historyApiFallback: true });

app = framework();
app.use(instance);

listen = listenShorthand(done);
});

afterAll(close);

it('should return the "200" code for the "GET" request', (done) => {
request(app).get("/foo/bar/baz").expect(200, done);
});
});
});

describe("serverSideRender option", () => {
let locals;

Expand Down
4 changes: 4 additions & 0 deletions test/validation-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ describe("validation", () => {
success: ["/foo", "", "auto", () => "/public/path"],
failure: [false],
},
historyApiFallback: {
success: [true],
failure: ["foo", 10],
},
serverSideRender: {
success: [true],
failure: ["foo", 0],
Expand Down