Issue I am facing
After creating a build when I serve the application, the dynamic import chunks are missing a /
in request URL.
Instead of loading https://example.com/adminapp_routes_Dashboards_DashboardPage_js.chunk.js
, it should request for https://example.com/admin/app_routes_Dashboards_DashboardPage_js.chunk.js
which is available on server but I'm getting 404.
In React app
Currently, I've added lazy
imports.
const DashboardPage = lazy(() => import('./Dashboards/DashboardPage'))
Which is generated as a seperate chunk in dist
called app_routes_Dashboards_DashboardPage_js.chunk.js
Webpack Configuration
const BASE_PATH = '/admin'
const distDir = path.join(root, "dist")
const srcDir = path.join(root, "app")
module.exports = {
devtool: false,
mode: 'production',
entry: {
app: [path.join(srcDir, 'index.js')],
},
optimization: {
moduleIds: 'named',
chunkIds: 'named',
minimize: true,
nodeEnv: 'production',
mangleWasmImports: true,
removeEmptyChunks: false,
mergeDuplicateChunks: false,
flagIncludedChunks: true,
minimizer: [`...`,
new CssMinimizerPlugin({
minimizerOptions: {
preset: [
"default",
{
calc: false,
discardComments: { removeAll: true },
},
],
},
}),
'...'],
emitOnErrors: true,
splitChunks: {
chunks: 'all',
minSize: 20000,
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
output: {
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
path: distDir,
publicPath: BASE_PATH,
},
resolve: {
fallback: { "querystring": false },
modules: ['node_modules', config.srcDir],
alias: {
path: require.resolve('path-browserify'),
Components: path.resolve(__dirname, '../app/components'),
},
},
plugins: [
new CircularDependencyPlugin({
exclude: /a\.js|node_modules/,
failOnError: true,
allowAsyncCycles: false,
cwd: process.cwd(),
}),
new HtmlWebpackPlugin({
template: config.srcHtmlLayout,
inject: 'body',
title: 'Example',
}),
new MiniCssExtractPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
BASE_PATH: JSON.stringify(BASE_PATH),
},
}),
new BundleAnalyzerPlugin({ analyzerMode: 'disabled' }), //* switch mode to server to active BundleAnalyzerPlugin
new PurgeCSSPlugin({ paths: glob.sync(`${path.resolve(__dirname, '../app')}/**/*`, { nodir: true }) }),
],
module: {
rules: [
{
test: /\.js$/,
include: [config.srcDir, config.pluginsDir],
exclude: /(node_modules|\.test\.js$)/,
use: 'babel-loader',
sideEffects: false,
},
{
test: /\.test\.js$/,
include: [config.srcDir, config.pluginsDir],
use: 'ignore-loader',
},
// Modular Styles
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "style-loader", "css-loader", "postcss-loader"]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: true,
},
},
{ loader: 'postcss-loader' },
'sass-loader',
],
exclude: [path.resolve(config.srcDir, 'styles')],
include: [config.srcDir],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader' },
{ loader: 'postcss-loader' },
{
loader: 'sass-loader',
options: {},
},
],
include: [path.resolve(config.srcDir, 'styles')],
},
// Fonts
{
test: /\.(ttf|eot|woff|woff2)$/,
loader: 'file-loader',
options: {
name: '/fonts/[name].[ext]',
esModule: false,
},
},
// Files
{
test: /\.(jpg|jpeg|png|gif|svg|ico)$/,
loader: 'file-loader',
options: {
name: '/static/[name].[ext]',
esModule: false,
},
},
],
},
devServer: {
hot: false,
//contentBase: config.distDir,
compress: true,
historyApiFallback: {
index: BASE_PATH,
},
host: '0.0.0.0',
port: 4100,
},
}