score:2
i know this question is for webpack-dev-server, but for anyone who uses webpack-serve 2.0. with webpack 4.16.5; webpack-serve allows add-ons.you'll need to create serve.config.js
:
const serve = require('webpack-serve');
const argv = {};
const config = require('./webpack.config.js');
const history = require('connect-history-api-fallback');
const convert = require('koa-connect');
serve(argv, { config }).then((result) => {
server.on('listening', ({ server, options }) => {
options.add: (app, middleware, options) => {
// historyapifallback
const historyoptions = {
// ... configure options
};
app.use(convert(history(historyoptions)));
}
});
});
you will need to change the dev script from webpack-serve
to node serve.config.js
.
score:5
you can enable historyapifallback
to serve the index.html
instead of an 404 error when no other resource has been found at this location.
let devserver = new webpackdevserver(compiler, {
historyapifallback: true,
});
if you want to serve different files for different uris, you can add basic rewriting rules to this option. the index.html
will still be served for other paths.
let devserver = new webpackdevserver(compiler, {
historyapifallback: {
rewrites: [
{ from: /^\/page1/, to: '/page1.html' },
{ from: /^\/page2/, to: '/page2.html' },
{ from: /^\/page3/, to: '/page3.html' },
]
},
});
score:5
for me i had dots "." in my path e.g. /orgs.csv
so i had to put this in my webpack confg.
devserver: {
historyapifallback: {
disabledotrule: true,
},
},
score:6
if you choose to use webpack-dev-server
, you should not use it to serve your entire react app. you should use it to serve your bundle.js
file as well as the static dependencies. in this case, you would have to start 2 servers, one for the node.js entry points, that are actually going to process routes and serve the html, and another one for the bundle and static resources.
if you really want a single server, you have to stop using the webpack-dev-server
and start using the webpack-dev-middleware within your app-server. it will process bundles "on the fly" (i think it supports caching and hot module replacements) and make sure your calls to bundle.js
are always up to date.
score:7
i agree with the majority of existing answers.
one key thing i wanted to mention is if you hit issues when manually reloading pages on deeper paths where it keeps the all but the last section of the path and tacks on the name of your js
bundle file you probably need an extra setting (specifically the publicpath
setting).
for example, if i have a path /foo/bar
and my bundler file is called bundle.js
. when i try to manually refresh the page i get a 404 saying /foo/bundle.js
cannot be found. interestingly if you try reloading from the path /foo
you see no issues (this is because the fallback handles it).
try using the below in conjunction with your existing webpack
config to fix the issue. output.publicpath
is the key piece!
output: {
filename: 'bundle.js',
publicpath: '/',
path: path.resolve(__dirname, 'public')
},
...
devserver: {
historyapifallback: true
}
score:14
my situation was a little different, since i am using the angular cli with webpack and the 'eject' option after running the ng eject command. i modified the ejected npm script for 'npm start' in the package.json to pass in the --history-api-fallback flag
"start": "webpack-dev-server --port=4200 --history-api-fallback"
"scripts": {
"ng": "ng",
"start": "webpack-dev-server --port=4200 --history-api-fallback",
"build": "webpack",
"test": "karma start ./karma.conf.js",
"lint": "ng lint",
"e2e": "protractor ./protractor.conf.js",
"prepree2e": "npm start",
"pree2e": "webdriver-manager update --standalone false --gecko false --quiet",
"startold": "webpack-dev-server --inline --progress --port 8080",
"testold": "karma start",
"buildold": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail"},
score:19
works for me like this
devserver: {
contentbase: "./src",
hot: true,
port: 3000,
historyapifallback: true
},
working on riot app
score:39
adding public path to config helps webpack to understand real root (/
) even when you are on subroutes, eg. /article/uuid
so modify your webpack config and add following:
output: {
publicpath: "/"
}
devserver: {
historyapifallback: true
}
without publicpath
resources might not be loaded properly, only index.html.
tested on webpack 4.6
larger part of config (just to have better picture):
entry: "./main.js",
output: {
publicpath: "/",
path: path.join(__dirname, "public"),
filename: "bundle-[hash].js"
},
devserver: {
host: "domain.local",
https: true,
port: 123,
hot: true,
contentbase: "./public",
inline: true,
disablehostcheck: true,
historyapifallback: true
}
score:107
historyapifallback option on official documentation for webpack-dev-server explains clearly how you can achieve either by using
historyapifallback: true
which simply falls back to index.html when the route is not found
or
// output.publicpath: '/foo-app/'
historyapifallback: {
index: '/foo-app/'
}
score:201
i found the easiest solution to include a small config:
devserver: {
port: 3000,
historyapifallback: {
index: 'index.html'
}
}
i found this by visiting: pushstate with webpack-dev-server.
Source: stackoverflow.com
Related Query
- How to tell webpack dev server to serve index.html for any route
- How to make the webpack dev server run on port 80 and on 0.0.0.0 to make it publicly accessible?
- How to configure webpack dev server with react router dom v4?
- How to serve static assets like json file in webpack dev server?
- How to get Webpack Dev Server to display images stored locally?
- How to connect webpack dev server to express?
- How to fix delay browser reload in webpack dev server with express
- how can I fix this problem I tried search for the match route but it doesn't show any thing?
- How to serve a React page from Hapijs server regardless of route
- how to define base URL in the dev server for react-scripts start call
- Express static server returns html for bundle.js for react build from webpack
- webpack dev server CORS issue
- how to use webpack to load CDN or external vendor javascript lib in js file, not in html file
- How to setup apache server for React route?
- How do I change webpack dev server's default port from 8080 to a different port?
- React tutorial - how do I start the node server for a reactJs application?
- React Native, there is no route defined for index undefined
- How to update webpack config for a react project created using create-react-app?
- Using the Webpack dev server with a PHP application
- Webpack 4 and react loadable does not seems to create correct chunk for server side rendering
- React-Router: how to wait for an async action before route transition
- How to display 404 when URL doesn't match any route and when the URL contains /#/ using ReactJS
- How would I test for a react router route param in react-testing-library?
- Webpack Dev Server (webpack-dev-server) Hot Module Replacement (HMR) Not Working
- Webpack dev server React Content Security Policy error
- webpack dev server not able to find node_modules
- How to export Component for server side rendering in React
- React-router auto redirect any route to root "/" except for "/main"
- how to make webpack typescript react webpack-dev-server configuration for auto building and reloading page
- Webpack dev server - run multiple apps on multiple ports
More Query from same tag
- Router problem. Page position stay the last page position when I change any router in react-router version 6
- React: Is there something similar to node.textContent?
- How to upload images into database using reactjs and php?
- React JS grouping a cart
- Open modal with ReactJS (es6) and siblings components
- Why are my dimensions not updating on resize when using React Hooks
- Getting the server error message from the fetch api when status is 500 not working
- Maximum update depth exceeded when updating react state on componentDidUpdate
- Change hash suffix on build using react
- ReactJS post increment does not work in setState
- Issue with giving margin to a jsx element inline
- Javascript Parser Error While Trying To Include SVG Element as Functional React Component
- Selecting Typescript, D3.js function overload and return type
- PrevState typing in TypeScript
- How to send requests to a nodejs backend from a React app served by nginx with ssl configured
- React Warning: Unknown prop `valueAsNumber` on <input> tag
- Difference between jest.fn(implementationCallback) and jest.fn().mockImplementation(implementationCallback)
- How to use Google Analytics with React?
- How to trigger animation on state change with styled-components in ReactJs
- Issue in create-react-app : The term 'create-react-app' is not recognized as the name of a cmdlet, function, script file, or operable program
- How can I access a value of a variable inside a map function?
- ReactJS useEffect() is running every time on page load
- Update Multiple Properties of an object with spread operator
- Test prevProps in componentDidUpdate in React via Jest
- PropTypes doesn't work in React
- React re-rendering limit issues
- Props in react seems to not be usable right away
- how to stop response for each input event
- Meteor: sortablejs method not being called
- Failed prop type: Invalid prop `count` of type `string` supplied to `ForwardRef(TablePagination)`, expected `number`